What color format should be used in the range parameters of the "cv2.inRange" function?

Asked

Viewed 174 times

-2

I want to create a mask for my image with Opencv using the function inRange, but I don’t know which color format to use. The documentation only says that the image (first parameter) must be in HSV, but does not speak the color format of the crease.

I found an example of his use in that site, which defines the following crease for the blue color:

blue_lower = np.array([94, 80, 2], np.uint8)
blue_upper = np.array([120, 255, 255], np.uint8)
blue_mask = cv2.inRange(hsvFrame, blue_lower, blue_upper)

The problem is that if the color format is RBG, the blue_lower is yellow in colour and the blue_upper is blue. If the color is BGR, it is reversed. And the format should certainly not be HSV because the last two values are greater than 100%.

What color format should I use? If it is BGR or RGB, this crease for the blue color is really correct?

  • 1

    Don’t use Opencv and much less C++ but the project repository has this test using BGR and this one also.. so I suppose you can assume that the color format is this same.

  • 1

    These two answers talk about this: > Different applications use different scales for HSV. For example Gimp uses H = 0-360, S = 0-100 and V = 0-100. But Opencv uses H: 0-179, S: 0-255, V: 0-255. 1- Choosing the correct upper and Lower HSV boundaries for color Detection with inRange (Opencv) 2- [How to Detect two Different Colors using cv2.inRange in Python-Opencv? ](https://stackoverflow.com/questions/48109650/how-to-detecttwo-different-color

  • @Lucaspedro thanks for your help!

  • 1

    Guys, can you please explain to me why so many negatives, both in the question and in the answer?

  • Jean, I also do not understand what happens to Stack in Portuguese. So I gave you an upvote to give a moral.

2 answers

2


The cv2.inRange() uses HSV (which you may already know, is used to be closer to what human eyes perceive) and seek in the documentation itself:

It would already have the explanation of the intervals that the API uses:

For HSV, Hue range is [0,179], saturation range is [0,255], and value range is [0,255]. Different software use Different Scales. So if you are Comparing Opencv values with them, you need to normalize These ranges.

Translating:

For HSV, the range of hue (Hue/H) is [0,179], the interval of saturation (saturation/h) is [0.255] and the interval of value (value/V) is [0,255]. Different software uses different scales, so if you’re comparing Opencv values with them, you’ll need to normalize those ranges.

However this you will use if you want to do on "hand" (of course it depends on what you want to do), but Opencv has functions to solve this, which can find HSV for a BGR value

green = np.uint8([[[0,255,0 ]]])
hsv_green = cv.cvtColor(green,cv.COLOR_BGR2HSV)
print( hsv_green ) # resulta em [[[ 60 255 255]]]

And to convert HSV to BGR or RGB (range from 0 to 180):

cv::COLOR_HSV2BGR = 54,
cv::COLOR_HSV2RGB = 55,

Or (range from 0 to 255, probably for image conversion issues this will be your case):

cv::COLOR_HSV2BGR_FULL = 70,
cv::COLOR_HSV2RGB_FULL = 71,

So yes Opencv can work with RGB, in addition to BGR, it depends on looking at the documentation, both to understand the patterns and other situations, and as stated in the note above, different software uses different scales.

0

The color format used in the parameters lowerb and upperb is HSV (Hue-Saturation-Value).

Just as Luke Peter commented, and it’s written in this reply in English, the reason for the values SV overtake 100, value that theoretically should be the limit, is that different applications can use different scales for the HSV color system.

For example, the GIMP uses for the H a value from zero to 360 and for S and V a value between zero and 100. In the case of Opencv, crease of the HSV colour components are as follows::

H (matriz)     |  0 - 179
S (saturação)  |  0 - 255
V (brilho)     |  0 - 255

And yes, the ranges of the question colors are correct. To verify this, just convert the Opencv HSV color to the normal HSV color, as in the code below:

blue_upper = [120, 255, 255]
int(360 / 179 * blue_upper[0]) # H: 241       HSV Normal: (241, 100, 100)
int(100 / 255 * blue_upper[1]) # S: 100%      RGB: (4, 0, 255)
int(100 / 255 * blue_upper[2]) # V: 100%      Hexadecimal: 0400FF

Browser other questions tagged

You are not signed in. Login or sign up in order to post.