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

问题描述

我在这里偶然发现了这个 YouTube 视频 http://www.youtube.com/watch?v=Ha5LficiSJM 演示了某人使用 AForge.NET 框架进行颜色检测.我想复制那个作者所做的,但我不知道如何进行一些图像处理.

I stumbled across this youtube video here http://www.youtube.com/watch?v=Ha5LficiSJM that demonstrates someone doing color detection using the AForge.NET framework. I'd like to duplicate what that author has done but I'm not sure how to do some image processing.

看起来 AForge.NET 框架允许您从视频源中提取位图格式的图像.我的问题是有人能指出我的方向或提供一些关于如何询问 Bitmap 对象以在其中找到特定颜色的指导吗?(例如 - 如果图像中有 X 秒的红色"或紫色",我想引发一个事件ColorDetected"左右......)

It would appear that the AForge.NET framework allows you to pull down from the video source the image in a Bitmap format. My questions is could anyone point me in the direction or provide some guidance on how to interrogate a Bitmap object to find specific colors in it? (for example - if there is 'Red' or 'Purple' in the image for X seconds, I'd like to raise an event 'ColorDetected' or so...)

有人对从哪里开始有任何建议吗?

Does anyone have any suggestions on where to start?

谢谢,

-R.

我是否需要遍历整个 Bitmap 对象并查询每个像素的颜色?像这样:http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.getpixel.aspx

Would I need to walk the entire Bitmap object and interrogate each pixel for the color? Like so: http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.getpixel.aspx

推荐答案

好吧,您始终可以使用 GDI+ 方法.

Well, you can always use the GDI+ method.

Bitmap b = new Bitmap( "some path" );
Color x = b.GetPixel( x, y );

然而,GetPixel 实际上很慢.首先尝试这种方式,但如果您需要扫描许多相对较大的图像,它可能不适合您.在这种情况下,使用 LockBits 来获取指向连续内存块的指针.然后,您可以快速遍历图像,但您必须知道如何操作指针,尽管它并不是非常复杂.

However, GetPixel is actually pretty slow. Try it that way first, but if you need to scan many relatively large images it may not work for you. In that case, use LockBits to get a pointer to a contiguous memory chunk. You can then loop through the image quickly, but you have to know how to manipulate pointers, though it's not terribly complicated.

使用 LockBits 查看每个像素:

Using LockBits to look at each pixel:

Bitmap b = new Bitmap( "some path" );
BitmapData data = b.LockBits( new Rectangle( 0, 0, b.Width, b.Height ),
ImageLockMode.ReadOnly, b.PixelFormat );  // make sure you check the pixel format as you will be looking directly at memory

unsafe
{
    // example assumes 24bpp image.  You need to verify your pixel depth
    // loop by row for better data locality
    for( int y = 0; y < data.Height; ++y )
    {
        byte* pRow = (byte*)data.Scan0 + y * data.Stride;
        for( int x = 0; x < data.Width; ++x )
        {
            // windows stores images in BGR pixel order
            byte r = pRow[2];
            byte g = pRow[1];
            byte b = pRow[0];

            // next pixel in the row
            pRow += 3;
        }
    }
}

b.UnlockBits(data);

如果您的图像在最后被填充,您可以使用 BitmapData.Stride 属性到达每个新行的开头(否则您将阅读一些垃圾并且您的偏移量会很糟糕).

If your images are padded at the end you can use the BitmapData.Stride property to get to the start of each new row (otherwise you will be reading some junk and your offsets will be screwy).

这篇关于在 C# 中查找图像中的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 02:23
查看更多