如何创建加权平均滤波器?

时间:2019-07-17 11:02:13

标签: c# image-processing filter

我是C#和图像处理的新手。我正在尝试创建一个执行加权平均滤波器的滤波器。但是我不能。

int i, sumred, sumblue, sumgreen, a, b, j, avgred, avgblue, avggreen;
Color r;
Bitmap bmp = new Bitmap(pBox_SOURCE.Image);
int kernelsize = 3;

for (i = ((kernelsize - 1) / 2); i < bmp.Width - (kernelsize / 2); i++)
{
    for (j = ((kernelsize - 1) / 2); j < bmp.Height - (kernelsize / 2); j++)
    {
        sumred = 0;
        sumblue = 0;
        sumgreen = 0;

        for (a = -((kernelsize - 1) / 2); a <= (kernelsize - 1) / 2; a++)
        {
            for (b = -((kernelsize - 1) / 2); b <= (kernelsize - 1) / 2; b++)
            {
                r = bmp.GetPixel(i + a, j + b);

                if (a==0 & b==0) // this pixel should be weightened
                {
                    sumred = sumred*10; 
                    sumblue = sumblue*10;
                    sumgreen = sumgreen*10;       
                }
                else
                {
                    sumred = r.R + sumred; //r yi int yap
                    sumblue = r.B + sumblue;
                    sumgreen = r.G + sumgreen;
                }
            }
        }
        avgred = sumred / (kernelsize * kernelsize);
        avggreen = sumgreen / (kernelsize * kernelsize);
        avgblue = sumblue / (kernelsize * kernelsize);
        bmp.SetPixel(i, j, Color.FromArgb(avgred, avggreen, avgblue));

    }
}
pBox_PROCESSED.Image = bmp;

这是我的错误消息:

  

System.ArgumentException:'930的值对于'red'无效。 “红色”应大于或等于0且小于或等于255

0 个答案:

没有答案