找到图像中在C#中的颜色 [英] find a color in an image in c#

查看:165
本文介绍了找到图像中在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

编辑:我会需要遍历整个位图对象和查询的颜色每个像素?像这样: http://msdn.microsoft.com/ EN-US /库/ 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实际上是pretty缓慢。尝试这种方式,但是,如果您需要扫描很多比较大的图像,可能不适合你的工作。在这种情况下,使用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
{ 
    byte* ptrSrc = (byte*)data.Scan0;

    // 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 )
    {
        for( int x = 0; x < data.Width; ++x )
        {
            // windows stores images in BGR pixel order
            byte r = ptrSrc[2];
            byte g = ptrSrc[1];
            byte b = ptrSrc[0];

            // next pixel in the row
            ptrSrc += 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#中的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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