gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 17:31:26 29 4
gpt4 key购买 nike

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

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

我选择了区域,对吗?我有一个左上角,我需要得到这样的东西:
enter image description here

如何定义imgWindowScreenProcessed具有搜索颜色的像素?我需要建立以下结构:
if (picture has pixels of searched color) {...}
else {...}

最佳答案

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

获得所需颜色像素为1且其他像素为0的二进制图像后,您可以玩形态学来增强图像here,并获得适当的关注区域。

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

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

关于c# - 如何使用openCV按颜色检测对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55441140/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com