Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting dominance color opencv

I have a image which is multi colored.

enter image description here

I want to calculate the dominant color of the image. the dominant color is red, i want to filter the red out. i am doing the following code in opencv but its not performing.

inRange(input_image, Scalar(0, 0, 0), Scalar(0, 0, 255), output);

How can i get the dominant color otherwise? My final project should determine the maximum color of the object on its own. What is the best method for this?

like image 821
rish Avatar asked Jun 03 '13 10:06

rish


2 Answers

You should quantize (reduce number of colors) your image before searching the for the most frequent color.

Why? Imagine image that has 100 pixels of (0,0,255) (blue color int RGB), 100 pixels of (0,0,254) (almost blue - you even won't find the difference) and 150 pixels of (0,255,0) (green). What is the most frequent color here? Obviously, it's green. But after quantization you will got 200 pixels of blue and 150 pixels of green.

Read this discussion: How to reduce the number of colors in an image with OpenCV?. Here's simple example:

int coef = 200;
Mat quantized = img/coef;
quantized = quantized*coef;

And this is what I've got after applying it:

enter image description here

Also you can use k-means or mean-shift to do that (this is much efficient way).

like image 114
ArtemStorozhuk Avatar answered Oct 06 '22 06:10

ArtemStorozhuk


The best method is by analyzing histograms.

Your problem is a classical "find the peak and area under the peak". By having an image file (let's say we take only the third channel for simplicity):

histogram

You will have to find the highest peak in that histogram. The easiest method is to simply query the X for which Y is maximized. More advanced methods work with windows - they average the Y-values of 10 consecutive data points, etc.

Also, work in the HSV or YCrCb color space. HSV is good because the "Hue" channel translates very closely to what you mean by "Color". RGB is really not well suited for image analysis.

like image 44
Boyko Perfanov Avatar answered Oct 06 '22 06:10

Boyko Perfanov