0

I'm trying to match all green colors in HLS (Hue Lightness Saturation), but without much success. I have to create an image containing only green peppers. Source image:

https://i.stack.imgur.com/ELNKV.png

All I need is specific section/dependencies between H, L and S. I have searched for this in the Internet but i failed :(.

I'm using OpenCV in Java for this. My current code (part):

private double lsRatio(HLS hls) {
    return hls.getL() / hls.getS();
}

private boolean isCondition(HLS hls) {
    return hls.getS() >= 0 && lsRatio(hls) > 0.8 && lsRatio(hls) < 5 && (hls.getH() <= 120 || hls.getH() >= 80);
}

And for this condition I get:

https://i.stack.imgur.com/NPvKw.png

As you can see there's still some red color.

Thanks for any help.

aergistal
  • 29,947
  • 5
  • 70
  • 92
Horbo
  • 1
  • Your HLS calculation seems to be wrong. Red is clearly not in the 80 - 120 hue range. Or it's in actually in 0-2pi range or 0-1 or so, – zapl Nov 26 '15 at 21:13
  • Do you mean I have a problem with convert RGB to HLS? – Horbo Nov 26 '15 at 21:38
  • Yes. That's my guess. Those red colors don't match your condition at all. They are around 0 hue. – zapl Nov 26 '15 at 21:39
  • Ok, but I use built-in function of OpenCV Imgproc.cvtColor(source, hlsMat, Imgproc.COLOR_BGR2HLS); Notice that at the end of condition I'm using alternative, not conjunction. – Horbo Nov 26 '15 at 21:42
  • @camickr thanks for help, but I found an answer [here](http://stackoverflow.com/questions/31590499/opencv-android-green-color-detection) :) – Horbo Nov 26 '15 at 22:21
  • @Horbo, post your code here if your found an answer. That code is for HSV. I'm curious what the code would be. Does just he Hue matter or do you also need to consider the saturation and luminance. – camickr Nov 26 '15 at 22:37
  • @camickr `private boolean isCondition(HSV hsv) { return isBetween(hsv.getH(), 60 - sensitivity, 60 + sensitivity) && isBetween(hsv.getS(), 100, 255) && isBetween(hsv.getV(), 50, 255); }` and: `boolean isBetween(value, min max)` returns true when value is in [min,max]. Also by trial and error -> sensitivity = 45. As you can see, hue is not the only one parameter to consider (in regarding to HSV at least). – Horbo Nov 27 '15 at 21:27

1 Answers1

0

I think the conversion to HLS is fine. I tried using the HSL Color conversion and get the same results.

(hls.getH() <= 120 || hls.getH() >= 80)

This will include all colors. (including red - that is 0 is less than 120). Your other conditions are filtering out most of the read tones/shades.

I tried something simpler:

private boolean isCondition(HLS hls)
{
    if (hls.getH() < 60 || hls.getH() > 140)
        return false;

    return true;
}

I initially tried the range of 80 - 120 but it missed a lot of the color. Maybe 60 -140 includes too many non green colors, in which case you could try applying your other conditions to exclude more colors.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • ouch, yes h <= 120 || h >= 80 means "any hue", maybe changing that to `&&` is the secret trick we've been overlooking. – zapl Nov 26 '15 at 22:23
  • @zapl, That is what I suggested in my answer. If you do use an `&&` condition then the other conditions are meaningless. – camickr Nov 26 '15 at 22:41