使用aforge的C#图像处理 [英] C# image processing using aforge

查看:156
本文介绍了使用aforge的C#图像处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好...
我已经编写了用于在网络摄像头的实时视频中检测红色并在其周围绘制一个矩形的代码.当我移动红灯时,矩形也会移动.现在,我想执行此操作以仅检测来自红外灯的白光.我正在使用aforge和C#,我必须使用rgb values.I遇到问题.谁能帮我.

这是我的代码,用于检测红灯并在其周围绘制矩形.

Hello...
I have written code for detecting red colour in a live video of webcam and drawing a rectangle around that. As i move the red light, the rectangle also moves. now i want to do this to detect only white light coming from an IR led. I am using aforge and C# and i have to do it using rgb values.I am having problems with it. Can anyone help me.

Here is my code for detecting red light and drawing rectangle around that.

public unsafe int getFrameMaxRedRectangleIndex(Bitmap source, int rwidth, int rheight)
{
    //rwidth and rheight is width and height of smaller rectangles inside the image
    Color color = Color.Black;
    int maxRedIdx = 0;
    try
    {
        Rectangle sourceRect = new Rectangle(0, 0, source.Width, source.Height);
        BitmapData sourceData = source.LockBits(sourceRect, ImageLockMode.ReadOnly, source.PixelFormat);

        //Finding the total number of rectangles inside the image.
        int idx;
        int maxRed = 0;
                        UnmanagedImage unmangeSource = new UnmanagedImage(sourceData);

        // to next line by adding
        int pixelSize = 3; // As format is PixelFormat.Format24bppRgb
        int srcOffset = sourceData.Stride - unmangeSource.Width * pixelSize;
        byte* srcBytes = (byte*)unmangeSource.ImageData.ToPointer();
        int red;
        int blue;
        int green;

        for (int y = 0; y < unmangeSource.Height; y = y + 1)
        {
            // for each pixel
            for (int x = 0; x < unmangeSource.Width; x = x + 1, srcBytes += (pixelSize * 1))
            {
                red = srcBytes[RGB.R];
                blue = srcBytes[RGB.B];
                green = srcBytes[RGB.G];
                if (!((Math.Abs(red - green) < 65) || ((Math.Abs(red - blue) < 65)))) //ignoring the case of white color
                {
                    idx = (x / rwidth) + ((y / rwidth) * rectangleInLine);
                    if (idx < tRecatangles)
                    {
                       // if (idx == 22)
                        //{
                        //}
                        redValues[idx] = redValues[idx] + srcBytes[RGB.R];
                        if (redValues[idx] > maxRed)
                        {
                            maxRed = redValues[idx];
                            maxRedIdx = idx;
                        }

                    }
                }
            }
            srcBytes += srcOffset; // This will move the pointer to next line
        }
        source.UnlockBits(sourceData);
        return maxRedIdx;
        //return dst;
    }
    catch (Exception ex) { Console.WriteLine(ex.ToString()); }

    return maxRedIdx; ;

}


谢谢...

[edit]已添加代码块-OriginalGriff [/edit]


thank you...

[edit]Code block added - OriginalGriff[/edit]

推荐答案

简单的答案是,除非您拥有在红外光谱中记录的摄像机,否则您将无法做到这一点.那么,您当然仍然必须知道或找出视频中记录了来自IR LED的光的原因. BTW IR LED不会发出白光,而是人眼看不见的光.它位于红灯下方,因此如果没有一些设备来帮助我们,我们凡人就看不到它.




如您所说,它已经在为红色工作.我添加的只是每个像素的未加权灰度.然后必须将该值与您应凭经验找到的阈值进行比较.我认为数组redValues在您的方法之外,因此我提出了一个数组greyValues.我希望这会给您一个有关如何解决此问题的想法.

Simple answer is you can''t do it unless you have video camera that records in the IR spectrum. Then of course you would still have to know or find out in what coulor the light from the IR LED is recorded in the video. BTW IR LEDs don''t emit white light, but rather light invisible to the human eye. It''s way below the red light so us mere mortals can''t see it without some device to help us out.




As you said it was already working for red. All I added was the unweighted greyscaling of each pixel. That value must then be compared with a threshold value which you should find empirically. I think the array redValues is outside your method so I proposed an array greyValues. I hope this will give you an idea on how to get this solved.

public unsafe int getFrameMaxGreyRectangleIndex(Bitmap source, int rwidth, int rheight)
{
    //rwidth and rheight is width and height of smaller rectangles inside the image
    Color color = Color.Black;
    int maxGreyIdx = 0;
    try
    {
        Rectangle sourceRect = new Rectangle(0, 0, source.Width, source.Height);
        BitmapData sourceData = source.LockBits(sourceRect, ImageLockMode.ReadOnly, source.PixelFormat);

        //Finding the total number of rectangles inside the image.
        int idx;
        int maxGrey = 0;
        UnmanagedImage unmangeSource = new UnmanagedImage(sourceData);

        // to next line by adding
        int pixelSize = 3; // As format is PixelFormat.Format24bppRgb
        int srcOffset = sourceData.Stride - unmangeSource.Width * pixelSize;
        byte* srcBytes = (byte*)unmangeSource.ImageData.ToPointer();
        int red;
        int blue;
        int green;
        // MRB: You'll have to play around with that value until you get acceptable results
        int thresholdForWhiteDetection = 200; 
        // MRB: Added the grey variable
        int grey;
        for (int y = 0; y &lt; unmangeSource.Height; y = y + 1)
        {
            // for each pixel
            for (int x = 0; x &lt; unmangeSource.Width; x = x + 1, srcBytes += (pixelSize * 1))
            {
                red = srcBytes[RGB.R];
                blue = srcBytes[RGB.B];
                green = srcBytes[RGB.G];
                //This line was for red detection
                //if (!((Math.Abs(red - green) &lt; 65) || ((Math.Abs(red - blue) &lt; 65)))) //ignoring the case of white color
                // MRB: I use the unweighted grey scaling method here for performance reasons
                grey = (red + blue + green) \ 3; 
                if(grey > thresholdForWhiteDetection)
                {
                    idx = (x / rwidth) + ((y / rwidth) * rectangleInLine);
                    if (idx &lt; tRecatangles)
                    {
                        //if (idx == 22)
                        //{
                        //}
                        greyValues[idx] = greyValues[idx] + srcBytes[RGB.R];
                        if (greyValues[idx] &gt; maxGrey)
                        {
                            maxGrey = greyValues[idx];
                            maxGreyIdx = idx;
                        }

                    }
                }
            }
            srcBytes += srcOffset; // This will move the pointer to next line
        }
        source.UnlockBits(sourceData);
        return maxGreyIdx;
        //return dst;
    }
    catch (Exception ex) { Console.WriteLine(ex.ToString()); }

    return maxGreyIdx; ;

}



[/编辑]

干杯!

--MRB



[/Edit]

Cheers!

--MRB


这篇关于使用aforge的C#图像处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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