Algorithm to determine most common color of a picture?

Started by
0 comments, last by jffortin 16 years, 9 months ago
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.
//------------------------------------------------------------------------------------------------------The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you're the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."
Advertisement
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++"). For color image you do that but in 3D, each dimension is a "color".<br><br>All of this will raise two problems with color image:<br><ul><li>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 &#111;ne will be the color that will be the "most common".</li> <br><li>Also &#111;n 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.</li></ul><br><br>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 &#111;ne 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. &#79;n 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".<br><br><br><!–QUOTE–><BLOCKQUOTE><span class="smallfont">Quote:</span><table border=0 cellpadding=4 cellspacing=0 width="95%"><tr><td class=quote><!–/QUOTE–><!–STARTQUOTE–><i>Original post by Sfpiano</i><br>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.<!–QUOTE–></td></tr></table></BLOCKQUOTE><!–/QUOTE–><!–ENDQUOTE–><br><br>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.<br><br>JFF

This topic is closed to new replies.

Advertisement