如何使用openCV按颜色检测对象

问题描述 投票:0回答:1

我必须制作一个宏,它应该在屏幕图片的某个区域上按颜色检测对象。此外,如果检测到对象,我需要继续执行代码。

    Image<Bgr, Byte> imgWindowScreen;
    Image<Gray, Byte> imgWindowScreenProcessed;
    Bitmap bmpScreenshot;
    System.Drawing.Graphics gfxScreenshot;
    Size size = new Size(400, 120);

bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                           Screen.PrimaryScreen.Bounds.Height,
                           PixelFormat.Format32bppArgb);

gfxScreenshot = Graphics.FromImage(bmpScreenshot);

gfxScreenshot.CopyFromScreen(700, 347, 0, 0, size);

imgWindowScreen = new Image<Bgr, Byte>(bmpScreenshot);

imgWindowScreenProcessed = imgWindowScreen.InRange(new Bgr(3, 25, 82),
                                         new Bgr(50, 98, 200));

我选择了这个区域是对的吗?我有一个左上角,我需要得到类似的东西:qazxsw poi

如何定义imgWindowScreenProcessed有一个搜索颜色的像素?我需要构建以下结构:

enter image description here
c# opencv
1个回答
1
投票

if (picture has pixels of searched color) {...} else {...} 你可能会发现HSV阈值的Python实现。我知道你需要用另一种语言,但是你可能会对实现的逻辑有所了解,因为它显示了一个简单的例子。 Here你可以找到更深入的C ++实现

获得二进制图像后,其中所需颜色的像素为1,其他像素为0,您可以使用形态学来增强图像Here,并获得适当的感兴趣区域。

然后,当您有图像时,您可能想要查看对象的位置。您可以使用here函数来获取对象的位置。

我通常在Python中使用OpenCV,但希望这也可能有用! :)

© www.soinside.com 2019 - 2024. All rights reserved.