我正在尝试进行边缘检测。但它仅适用于一张图像

时间:2019-07-19 18:32:19

标签: c# image-processing

我正在尝试创建一个简单的边缘检测过滤器。正如我所说的,它仅适用于一张图像。我正在尝试通过2个步骤创建此过滤器。

  1. 模糊图像(带有meanfilter)
  2. 计算(原始图像模糊图像)

第一步工作良好。第二个代码就像第一个一样简单。但我看到一条错误消息:

  

System.ArgumentOutOfRangeException:'参数必须为正数且<高度。   参数名称:y'

工作图像:https://i.hizliresim.com/dLXkbn.png

我的代码:

    public void edgedetectionfilter( )
    {
            Bitmap InputPicture,BlurredPicture, OutputPicture;
            InputPicture = new Bitmap(pBox_SOURCE.Image);
            BlurredPicture = new Bitmap(pBox_PROCESSED.Image);
            int PicWidth = InputPicture.Width;
            int PicHeight= InputPicture.Height;
            OutputPicture = new Bitmap(PicWidth, PicHeight);
            OutputPicture = InputPicture;

            int x, y, difR, difG, difB;

            Color OrgPicColoValue,BluredPicColorValue;

            for (x = 0; x < PicWidth; x++)
            {
                for (y = 0; y < PicWidth; y++)
                {
                    BluredPicColorValue = BlurredPicture.GetPixel(x, y);
                    OrgPicColoValue = InputPicture.GetPixel(x, y); //ERROR LINE
                    difR = Convert.ToInt16(OrgPicColoValue.R -BluredPicColorValue.R);
                    difG = Convert.ToInt16(OrgPicColoValue.G- BluredPicColorValue.G );
                    difB = Convert.ToInt16(OrgPicColoValue.B- BluredPicColorValue.B);

                    if (difR > 255) difR = 255;
                    if (difG > 255) difG = 255;
                    if (difB > 255) difB = 255;
                    if (difR < 0) difR = 0;
                    if (difG < 0) difG = 0;
                    if (difB < 0) difB = 0;

                    OutputPicture.SetPixel(x, y, Color.FromArgb(difR, difG, difB));
                }
            }

            pBoxMedian.Image = OutputPicture;
    }





public void meanfilter(int p)
 //KERNELSIZE=P
        {

            if (sliderKernel.Value % 2 == 0)
            {
                MessageBox.Show("Enter an odd number");
                return;
            }

            Color ColorValue;
            Bitmap InputPicture, OutputPicture;
            InputPicture = new Bitmap(pBox_SOURCE.Image);
            int PicWidth = InputPicture.Width;
            int PicHeight= InputPicture.Height;
            OutputPicture = new Bitmap(PicWidth, PicHeight);
            OutputPicture = InputPicture;


            int x, y, i, j, sumR, sumG, sumB, avgR, avgG, avgB;




            for (x = (KernelSize - 1) / 2; x < PicWidth - (KernelSize - 1) / 2; x++)
            {


                for (y = (KernelSize - 1) / 2; y < PicHeight - (KernelSize- 1) / 2; y++)
                {


                    toplamR = 0;
                    toplamG = 0;
                    toplamB = 0;
                    for (i = -((KernelSize - 1) / 2); i <= (KernelSize - 1) / 2; i++)
                    {
                        for (j = -((KernelSize - 1) / 2); j <= (KernelSize - 1) / 2; j++)
                        {
                            ColorValue= InputPicture.GetPixel(x + i, y + j);
                            sumR = sumR + ColorValue.R;
                            sumG = sumG + ColorValue.G;
                            sumB = sumB + ColorValue.B;
                        }
                    }
                    avgR = sumR / (KernelSize  * KernelSize );
                    avgG = sumG / (KernelSize *KernelSize );
                    avgB = sumB / (KernelSize  * KernelSize );
                    OutputPicture.SetPixel(x, y, Color.FromArgb(avgR, avgG, avgB));


                }
            }


            pBox_PROCESSED.Image = OutputPicture;


        }

1 个答案:

答案 0 :(得分:0)

您比较y < PicWidth,而您可能想要y < PicHeight。它的图像是否偶然出现在正方形上?