【问题标题】:How to detect object by color with openCV如何使用openCV通过颜色检测对象
【发布时间】:2019-04-07 20:31:18
【问题描述】:

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

    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));

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

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

if (picture has pixels of searched color) {...}
else {...}

【问题讨论】:

  • 能否请您澄清一下,您只需要检测特定颜色的对象吗?
  • @БолатТлеубаев 我需要检测颜色,如果检测到颜色,我需要做点什么

标签: c# opencv


【解决方案1】:

Here 你可能会发现 HSV 阈值的 Python 实现。我知道您需要使用另一种语言,但是您可能会对实现的逻辑有所了解,因为它显示了一个简单的案例。 Here你可以找到更深层次的C++实现

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

然后,当您有图像时,您可能希望查看对象的位置。您可以使用findContours() 函数来获取对象的位置。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-31
    • 2021-08-29
    • 2013-05-05
    • 2012-07-12
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多