I want to find dominant colour in an Image in Opencv C++

670 Views Asked by At

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.

1

There are 1 best solutions below

0
Burak On

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.