OpenCV安卓绿色检测[英] OpenCV Android Green Color Detection

本文是小编为大家收集整理的关于OpenCV安卓绿色检测的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

目前我正在制作一个应用程序,用户可以在其中检测绿色.我使用这张照片进行测试:在此处输入图片说明

我的问题是我无法检测到任何绿色像素.在我使用蓝色之前,一切正常.虽然我尝试了 RGB 的不同组合,但现在我无法检测到任何东西.我想知道是绿色问题还是我的检测范围有问题,所以我使用 (0, 255, 0) 在油漆中制作了一个图像并且它起作用了.为什么它看不到这个圆圈呢?我使用此代码进行检测:

Core.inRange(hsv_image, new Scalar([I change this value]), new Scalar(60, 255, 255), ultimate_blue);

可能是我设置了错误的范围,但我使用 Photoshop 获取绿色像素之一的颜色并将其 RGB 值转换为 HSV.然而它不起作用.它甚至没有检测到我采样的像素.怎么了?提前致谢.

使用 Miki 的回答:

在此处输入图片描述

推荐答案

绿色是 HSV 空间的 H = 120 并且在 [0, 360] 范围内.

OpenCV 将 H 值减半以适应范围 [0,255],因此 H 值不在 [0, 360] 范围内,而是在 [0, 180] 范围内.S 和 V 仍在 [0, 255] 范围内.

因此,绿色的 H 值为 60 = 120/2.

你的上下限应该是:

// sensitivity is a int, typically set to 15 - 20 
[60 - sensitivity, 100, 100]
[60 + sensitivity, 255, 255]
<小时>

更新

由于您的图像很暗,您需要使用 V 的下限.使用这些值:

sensitivity = 15;
[60 - sensitivity, 100, 50]  // lower bound
[60 + sensitivity, 255, 255] // upper bound

生成的掩码如下:

在此处输入图片描述

您可以参考这个答案了解详情.

本文地址:https://www.itbaoku.cn/post/102511.html

问题描述

currently I'm making an app where user will detect green colors. I use this photo for testing: enter image description here

My problem is that I can not detect any green pixel. Before I worked with blue color and everything worked fine. Now I can't detect anything though I tried different combinations of RGB. I wanted to know whether it's problem with green or my detection range, so I made an image in paint using (0, 255, 0) and it worked. Why it can't see this circle then? I use this code for detection:

Core.inRange(hsv_image, new Scalar([I change this value]), new Scalar(60, 255, 255), ultimate_blue);

It could have been that I set wrong Range, but I use Photoshop to get color of one of green pixels and convert RGB value of it into HSV. Yet it doesn't work. It don't detect even pixel that I've sampled. What's wrong? Thanks in advance.

Using Miki's answer:

enter image description here

推荐答案

Green color is HSV space has H = 120 and it's in range [0, 360].

OpenCV halves the H values to fit the range [0,255], so H value instead of being in range [0, 360], is in range [0, 180]. S and V are still in range [0, 255].

As a consequence, the value of H for green is 60 = 120 / 2.

You upper and lower bound should be:

// sensitivity is a int, typically set to 15 - 20 
[60 - sensitivity, 100, 100]
[60 + sensitivity, 255, 255]

UPDATE

Since your image is quite dark, you need to use a lower bound for V. With these values:

sensitivity = 15;
[60 - sensitivity, 100, 50]  // lower bound
[60 + sensitivity, 255, 255] // upper bound

the resulting mask would be like:

enter image description here

You can refer to this answer for the details.