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:
- create array of pixels with an R, G, B value.
- choose 5 random pixels as clusters (c)
- assign each pixel to the closest c in 3 dimensional space using 3d distance equation
- change cluster locations (c) to the average of the r, g, and b values of it's assigned points
- repeat steps 3&4 for a while