关于5 x 5滤镜问题 [英] About 5 x 5 Filter Problem

查看:87
本文介绍了关于5 x 5滤镜问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的
最近,我尝试对5 x 5中值滤镜进行编程.现在,我遇到了一个超出图像范围的问题(例如内部的错误),我无法弄清楚.这将在mask5 [7] = ImgData [x,y-1,0];
处停止 谁能告诉我我需要弄清楚哪一部分?
非常感谢
杰基

Dear
Recently I tried to program about 5 x 5 median filter. Now I got a problem about out of image range (like bugs inside) that I can''t figure it out. This will stop at mask5[7] = ImgData[x, y - 1, 0];
Could anyone tell me which part I need to figure it out?
Thanks a lot
Jacky

public void Fivefilter(int[, ,] ImgData)
{
    int Width = ImgData.GetLength(0);
    int Height = ImgData.GetLength(1);

    // 5 x 5 mask
    int[] mask5 = new int[25];
    for (int k = 0; k < 25; k++)
    {
       mask5[k] = 1;
    }

    // 5x5 for loop
    for (int x = 0; x < Width; x++)
    {
        for (int y = 0; y < Height; y++)
        {
            // If_Else
            if (x - 2 >= 0 && y - 2 >= 0)
            {
                mask5[0] = ImgData[x - 2, y - 2, 0];
            }
            else
            {
                mask5[0] = 0;
            }
            if (y - 2 >= 0)
            {
                mask5[1] = ImgData[x - 1, y - 2, 0];
            }
            else
            {
                mask5[1] = 0;
            }
            if (y - 2 >= 0)
            {
                mask5[2] = ImgData[x, y - 2, 0];
            }
            else
            {
                mask5[2] = 0;
            }
            if (y - 2 >= 0)
            {
                mask5[3] = ImgData[x + 1, y - 2, 0];
            }
            else
            {
                mask5[3] = 0;
            }
            if (x + 2 < Width && y - 2 >= 0)
            {
                mask5[4] = ImgData[x + 2, y - 2, 0];
            }
            else
            {
                mask5[4] = 0;
            }
            if (x - 2 >= 0)
            {
                mask5[5] = ImgData[x - 2, y - 1, 0];
            }
            else
            {
                mask5[5] = 0;
            }
            if (x - 1 >= 0)
            {
                mask5[6] = ImgData[x - 1, y - 1, 0];
            }
            else
            {
                mask5[6] = 0;
            }
            if (x >= 0)
            {
                mask5[7] = ImgData[x, y - 1, 0];
            }
            else
            {
                mask5[7] = 0;
            }
            if (x  + 1 >= 0)
            {
                mask5[8] = ImgData[x + 1, y - 1, 0];
            }
            else
            {
                mask5[8] = 0;
            }
            if (x + 2 < Width)
            {
                mask5[9] = ImgData[x + 2, y - 1, 0];
            }
            else
            {
                mask5[9] = 0;
            }
            if (x - 2 >= 0)
            {
                mask5[10] = ImgData[x - 2, y, 0];
            }
            else
            {
                mask5[10] = 0;
            }
            if (x - 1 >= 0)
            {
                mask5[11] = ImgData[x - 1, y, 0];
            }
            else
            {
                mask5[11] = 0;
            }
            if (x + 1 >= 0)
            {
                mask5[12] = ImgData[x + 1, y, 0];
            }
            else
            {
                mask5[12] = 0;
            }
            if (x + 2 < Width)
            {
                mask5[13] = ImgData[x + 2, y, 0];
            }
            else
            {
                mask5[13] = 0;
            }
            if (x - 2 >= 0)
            {
                mask5[14] = ImgData[x - 2, y + 1, 0];
            }
            else
            {
                mask5[14] = 0;
            }
            if (x - 1 >= 0)
            {
                mask5[15] = ImgData[x - 1, y + 1, 0];
            }
            else
            {
                mask5[15] = 0;
            }
            if (x >= 0)
            {
                mask5[16] = ImgData[x, y + 1, 0];
            }
            else
            {
                mask5[16] = 0;
            }
            if (x + 1 >= 0)
            {
                mask5[17] = ImgData[x + 1, y + 1, 0];
            }
            else
            {
                mask5[17] = 0;
            }
            if (x + 2 < Width)
            {
                mask5[18] = ImgData[x + 2, y + 1, 0];
            }
            else
            {
                mask5[18] = 0;
            }
            if (y + 2 < Height)
            {
                mask5[19] = ImgData[x - 2, y + 2, 0];
            }
            else
            {
                mask5[19] = 0;
            }
            if (y + 2 < Height)
            {
                mask5[20] = ImgData[x - 1, y + 2, 0];
            }
            else
            {
                mask5[20] = 0;
            }
            if (y + 2 < Height)
            {
                mask5[21] = ImgData[x, y + 2, 0];
            }
            else
            {
                mask5[21] = 0;
            }
            if (y + 2 < Height)
            {
                mask5[22] = ImgData[x + 1, y + 2, 0];
            }
            else
            {
                mask5[22] = 0;
            }
            if (x + 2 < Width && y + 2 < Height)
            {
                mask5[23] = ImgData[x + 2, y + 2, 0];
            }
            else
            {
                mask5[23] = 0;
            }

            // Center Mask
            mask5[24] = ImgData[x, y, 0];
            Array.Sort(mask5);
            // Middle integer Value
            int mid = mask5[13];
            ImgData[x, y, 0] = ImgData[x, y, 1] = ImgData[x, y, 2] = mid;
        }
    }
}

推荐答案

考虑当x = 0和y = 0时的情况,那么您提到的行将是:
mask5[7] = ImgData[0, -1, 0]会抛出IndexOutOfRange异常.

您需要更新if语句,以确保您没有负索引(或x> Width,y> Heigth).

对于这种情况,您需要更新:
if (x >= 0)

if (x >= 0 and y >= 1)
Consider case when x=0 and y=0, then the line you mentioned would be:
mask5[7] = ImgData[0, -1, 0] which will throw IndexOutOfRange exception.

you need to update if statements to make sure you don''t have negative indexes (or x>Width,y>Heigth).

For this case you need to update:
if (x >= 0)
to
if (x >= 0 and y >= 1)


这篇关于关于5 x 5滤镜问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆