自适应阈值技术从网络摄像头C#直播视频

时间:2018-04-19 22:25:57

标签: c# detection aforge light

我试图检测来自2个LED灯(红色和蓝色)的光线,我使用伯恩森阈值技术做到了这一点。但是,我将其应用于图像。现在我想将相同的技术应用于我的网络摄像头的实时视频。反正我是否可以在图像上编辑此技术的代码,使其适用于网络摄像头的视频?我将在下面添加我用于此阈值技术的代码。

private ArrayList getNeighbours(int xPos, int yPos, Bitmap bitmap)
        {
           //This goes around the image in windows of 5 

            ArrayList neighboursList = new ArrayList();

            int xStart, yStart, xFinish, yFinish;

            int pixel;

            xStart = xPos - 5;
            yStart = yPos - 5;


            xFinish = xPos + 5;
            yFinish = yPos + 5;


            for (int y = yStart; y <= yFinish; y++)
            {
                for (int x = xStart; x <= xFinish; x++)
                {

                    if (x < 0 || y < 0 || x > (bitmap.Width - 1) || y > (bitmap.Height - 1))
                    {
                        continue;
                    }
                    else
                    {
                        pixel = bitmap.GetPixel(x, y).R;

                        neighboursList.Add(pixel);
                    }
                }
            }

            return neighboursList;
        }


private void button5_Click_1(object sender, EventArgs e)
        {
             //The input image
            Bitmap image = new Bitmap(pictureBox2.Image);
            progressBar1.Minimum = 0;
            progressBar1.Maximum = image.Height - 1;
            progressBar1.Value = 0;
            Bitmap result = new Bitmap(pictureBox2.Image);

            int iMin, iMax, t, c, contrastThreshold, pixel;

            contrastThreshold = 180;

            ArrayList list = new ArrayList();

            for (int y = 0; y < image.Height; y++)
            {
                for (int x = 0; x < image.Width; x++)
                {
                    list.Clear();

                    pixel = image.GetPixel(x, y).R;
                    list = getNeighbours(x, y, image);
                    list.Sort();


                    iMin = Convert.ToByte(list[0]);

                    iMax = Convert.ToByte(list[list.Count - 1]);

                    // These are the calculations to test whether the 
                       current pixel is light or dark

                    t = ((iMax + iMin) / 2);


                    c = (iMax - iMin);


                    if (c < contrastThreshold)
                    {

                        pixel = ((t >= 160) ? 0 : 255);
                    }
                    else
                    {
                        pixel = ((pixel >= t) ? 0 : 255);
                    }

                    result.SetPixel(x, y, Color.FromArgb(pixel, pixel, pixel));                    
                }

                progressBar1.Value = y;


            }

            pictureBox3.Image =result;
        }

0 个答案:

没有答案