4
$\begingroup$

I am trying to write an implementation of a color quantization algorithm in order to find an image's dominant colors (say 5) and then find contrasting colors based on the colors found in the image.

I've tried implementing a k-means algorithm but the result ends up differently every time.

What do you suggest? I need help mostly with the color segmentation. What algorithm/method do you suggest? I can play with picking contrasting colors on my own.

I am able to iterate through an image's individual rgb values on a per-pixel basis.

Side note: The language I am using is objective C, so there are no libraries (that I know of) that can perform this task.

How I used the K-Means algorithm:

  1. create array of pixels with an R, G, B value.
  2. choose 5 random pixels as clusters (c)
  3. assign each pixel to the closest c in 3 dimensional space using 3d distance equation
  4. change cluster locations (c) to the average of the r, g, and b values of it's assigned points
  5. repeat steps 3&4 for a while
$\endgroup$
  • 1
    $\begingroup$ Hi NickPlace and welcome to Scicomp! Could you add more information about the k-means algorithm you used into your question. This may help to understand why the algorithm produces very different results. $\endgroup$ – Paul Jul 25 '12 at 19:59
  • $\begingroup$ Okay, I added it. $\endgroup$ – Nick Jul 25 '12 at 21:05
  • $\begingroup$ Your different results each time are due to your random point choice in step 2. In step 5, are you re-choosing your cluster points as well? Also, how many iterations are you running of the algorithm? At the very least you can seed your random number generator with a constant until you get it "working" to avoid the varying results. $\endgroup$ – Godric Seer Jul 25 '12 at 23:31
  • $\begingroup$ @Nick: it sounds like you are looking to do image segmentation: en.wikipedia.org/wiki/Segmentation_(image_processing). If not, how do the results you seek differ from the results that one obtains with image segmentation? $\endgroup$ – dranxo Aug 5 '12 at 4:31
4
$\begingroup$

RGB distance is not at all close to Color difference (but don't be put off by the formulas there).
Basically you want to transform your RGB triples to Lab color space, and use distances there:

def rgb_deltae76( rgb, rgb2 ):
    return distance( rgb_lab(rgb), rgb_lab(rgb2) )

rgb_lab is a couple of pages of Python, somebody must have code in objective C;
try https://stackoverflow.com/questions/tagged/colors.

Added: this note gives the approximation $\sqrt { 3 (R - R')^2 + 4 (G - G')^2 + 2 (B - B')^2 }$
and another better one.
See also Munsell color system and triplecode.com/munsell:
"The Munsell system is different because it is based on how people perceive colors."

$\endgroup$
0
$\begingroup$

Can you transform your RGB colors to some other color notation, like HSV ? The H would be the color, be it a radial measure ( 0 -360 degrees) or some value. Then use the value that maximizes the distance to the colors you have.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.