7

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

Oleksandr Firsov
  • 1,428
  • 1
  • 21
  • 48
  • have a look [here](http://stackoverflow.com/a/31465462/5008845) – Miki Jul 23 '15 at 14:38
  • I think I use HSV right. I use http://colorizer.org/ to find out corresponding HSV for RGB and then convert it, so it can be used in OpenCV. – Oleksandr Firsov Jul 23 '15 at 14:43
  • I updated the answer, you need to use a lower bound for V, since your original image is quite dark (i.e. low V). See the result – Miki Jul 23 '15 at 14:56
  • https://stackoverflow.com/questions/47483951/how-to-define-a-threshold-value-to-detect-only-green-colour-objects-in-an-image – Jeru Luke Sep 12 '22 at 12:38

1 Answers1

22

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.

Community
  • 1
  • 1
Miki
  • 40,887
  • 13
  • 123
  • 202
  • I've used your values and made sensitivity to 60. I've uploaded image of what I get. Lighter pixels are those that I detect. – Oleksandr Firsov Jul 23 '15 at 14:56
  • OMG! Thanks. You don't even realize how useful this answer is. When I struggled to detect blue circles I used up to 10 inRange to filter enough pixels to detect color. And here you do it with one function. I feel so stupid. – Oleksandr Firsov Jul 23 '15 at 15:01