Sign in to follow this  

Algorithm to determine most common color of a picture?

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Quote:
Original post by Sfpiano
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.


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 Sfpiano
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.


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

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this