# Algorithm to determine most common color of a picture?

This topic is 3996 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Does anyone know of a way to determine which color occurs most often in an image? For example if I had a white background with a bunch of black dots scattered everywhere it would give me white. My problem is that I have a text box with a background image and if the background color of the box doesn't match the color of the background image, I get weird artifacts whenever I select text.

##### Share on other sites
Quote:
 Original post by SfpianoDoes anyone know of a way to determine which color occurs most often in an image? For example if I had a white background with a bunch of black dots scattered everywhere it would give me white.

The problem can easily be solved by creating a histogram of the image and counting which color you get more often (median). A histogram is simply counting the number of occurrences of every colors in the image. You make a big array containing the colors, lets assume black and white for now and values from 0 to 255, so you could have a "int histogram[255]". You go through the image pixel by pixel, then get its color and increment the value for that color ("histogram[color]++"). For color image you do that but in 3D, each dimension is a "color".

All of this will raise two problems with color image:
• For example (100, 100, 99) and (100, 100, 100) will be different colors even if you don't see any difference between them and this might influence your results. You could have a count of 3 for (0, 0, 0), 2 at (100, 100, 99) and also 2 at (100, 100, 100) and the first one will be the color that will be the "most common".

• Also on 24 bit images you'll have 2^24 different values to parse in your histogram which makes for around 16 Mb of data if using chars to count the pixels and 64Mb is using integers.

There are many solutions to those problems, but the mostly used solution that I know is creating "buckets". You group range of colors together to reduce the quantity of data to process and reduces also the memory usage. Also, since the eye won't notice the difference between colors that are "close" like the one mentioned earlier the results tends to be better because they are grouped. I know that some systems that do that kind of stuff divides each "color" in 16 ([0..15], [16..31], ...) but you still get 4096 buckets. On a school project where we needed to compare similar images I always used 4 when I was testing to make it faster and I was getting correct results most of the time. For even better results some systems used grouping algorithms like K-mean to choose the best values for the "color ranges".

Quote:
 Original post by SfpianoMy problem is that I have a text box with a background image and if the background color of the box doesn't match the color of the background image, I get weird artifacts whenever I select text.

I'm not sure I understand your problem correctly and the use of images in that case. The algorithm I explained earlier might be overkill in that case.

JFF

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
16
5. 5

• 14
• 29
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002274
×