I am new to openCV and searching from past two weeks but all the answers are either in python or not that accurate. I am just looking to find maximum count of a colour in any image. I have tried converting to HSV plane and then by looping over image and extracting hue channel and then by increasing count of each colour which lies in the range. but this method was not accurate at all. So looking for a better solution.
Asked
Active
Viewed 648 times
0
-
2The answer to this question depends how you seperate one color from another. If every bit change means a different color, you don't even need to convert the color space. Otherwise, your method should be accurate if the threshold of the ranges are as desired. – Burak Jan 30 '20 at 07:22
-
if black pixel is formed by(0,0,0) then in HSV plane it will be H = 0 S = V =0. But if the pixel value is (20,21,20) it still is black but when it will be converted to HSV H becomes 60 S = 12 ans V is maximum value of these three value that is 21. And hue 60 is for green color so that pixel is counted in green color, which should not be the case. – Amit Kumar Jan 30 '20 at 10:05
-
`(20,21,20)` is not "black". It may look black (or dark gray) to a human eye. But how far that is the case depends on a lot of things. You need to clarify when you consider colors as "equal". – chtz Jan 30 '20 at 16:12
-
I did, and can tell you this: you can’t, with OpenCV. – Константин Ван Jan 27 '21 at 23:26
1 Answers
0
By looking at the color conversion from RGB to HSV, I think you can do the following:
Black: V
is below 25
and S
is below 100
White: V
is above 230
and S
is below 30
Red: H
below 15
or above 165
Yellow: H
between 15-45
Green: H
between 45-75
Cyan: H
between 75-105
Blue: H
between 105-135
Magenta: H
between 135-165
You can add more colors by narrowing down the H
channel, or by specifying shades to colors, i.e. if 45 < H < 75
, dark green for V < 128
, and light green for 128 < V
. You need to treat black and white differently in any case. These S
and V
values are only examples, you can even have functions for them. Other colors are checked after the shades of gray.

Burak
- 2,251
- 1
- 16
- 33