Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

detect the most used colour in an image using python

I want to find the most used colour in an image using python. for example detect the colour of the object in the following image

http://www.shopcrazy.com.ph/wp-content/images/2007/02/shiny-bags-01.jpg.

how to detect the base colour from the RGB codes(example - red in the above image).

like image 988
Sreejith Sasidharan Avatar asked Feb 03 '23 05:02

Sreejith Sasidharan


2 Answers

Since you will most likely not want a histogram of all the million colors that are possible using a 24-bit color space, I suggest transforming the image into HSV space instead. Then you can partition the Hue part of that space into a number of bins that describe the hues you want to find ("dark red", "orange red", or whatever). Then make a histogram of these bins and find which is the dominant hue, which is the "color".

The wikipedia article http://en.wikipedia.org/wiki/HSL_and_HSV should get you started. IF you are using an image processing library chances are that a rgb-to-hsv/hsl function exists.

Also, if the images are large and speed is an issue, you might consider downsampling the image to a smaller size before histogramming.

like image 147
Hannes Ovrén Avatar answered Feb 06 '23 14:02

Hannes Ovrén


The brute force approach is to loop over all pixels in the image and keep count of R, G, B values. A more refined approach is to use Python Image Library histogram function and calculate the average of all colors.

like image 42
Martin Wickman Avatar answered Feb 06 '23 15:02

Martin Wickman