Using python and k-means to find dominant colors in images
21–30 of 34 posts
Re: Using python and k-means to find dominant colors in images
#22http://en.wikipedia.org/wiki/Color_difference
http://stackoverflow.com/questions/1313/followup-finding-an-...
Re: Using python and k-means to find dominant colors in images
#23Re: Using python and k-means to find dominant colors in images
#24Just wanted to say thanks for mentioning my book! I'm so happy that people are still getting value from it.
Re: Using python and k-means to find dominant colors in images
#25I'm guessing this is a solved-many-times problem :) Here is my own version of the same thing: http://tylerneylon.com/a/imghist/ (and source) https://github.com/tylerneylon/imghist/blob/master/imghist.p... That also includes color histograms. Thoughts on actually using this: * If a human is using the color output, it's fun to weigh the colors by cluster sizes. * To find human-perspective dominant colors, it helps to t…
To expand on your points, based on my own experience:
* Rather than using the centroid to represent the cluster, pick a representative peak from the hue histogram. This tends to make things less muddy.
* Not only do I throw away extreme S/L values, I aggressively weigh everything by S * (0.5 - |0.5 - L|), so bright, saturated colours dominate. Black and white are usually very thin slivers, and I almost never have grey.
* As a last step, I convert to Lab space and merge any colours that are perceptually similar (dE Here's my result for one of your images: https://dl.dropbox.com/s/azl2bag84riugg8/imghist.png, notice how the orange has a disproportionally large weight because it is so saturated.
Re: Using python and k-means to find dominant colors in images
#26Re: Using python and k-means to find dominant colors in images
#27Cool, I did something like this for an e-commerce store so that I could programmatically sort new products into color bins. Scipy handled the kmeans stuff for me though. One problem I ran into but never solved was ignoring the background color. For example in the second picture it might be more interesting to bring out the oranges of the tail-lights and the light-blues of the street lights, rather than just the dark…
I did this and have a set with either white or close to white backgrounds so I filtered them out. Depending on your images you could also sample some points near each corner to determine which cluster is likely background.
Re: Using python and k-means to find dominant colors in images
#28Awesome, I used this technique to find the dominant color in favicons so I could create a gradient in NewsBlur. See the effect here: http://cl.ly/KElb Here's the Python I use to do it: https://github.com/samuelclay/NewsBlur/blob/master/utils/Ima... from PIL import Image import scipy import scipy.cluster from pprint import pprint image = Image.open('logo.png') NUM_CLUSTERS = 5 # Convert image into array of values for…
k-means is not a mode seeking algorithm, I think. You are clustering your color space, but you're not even guaranteed to end up with colors that are very close to those in your image. With a high k you're getting actual colors in the image, but they're not really dominant anymore.
What about mean shift? It's based on one method of non-parametric density estimation. Another method is this: Some sort of histogramming, which is another method of non-parametric density estimation.
Also other colors spaces will pay off immensely.
Re: Using python and k-means to find dominant colors in images
#29Anyone know of a similar style article going over determining an optimal k value? I remember reading some papers on this in my honors days but when I was doing something similar recently I just used a max distance to determine whether something should join a current cluster or form a new one.
Re: Using python and k-means to find dominant colors in images
#30There are a few things you should take into account: 1. You determine the 'dominant' colours to be the centroids of your clusters. The centroid is the mean of the points within the cluster, this mean is not necessarily a colour that is in your image. If you, for example, take a picture divided into four different solid coloured squares, and use this to find the 3 dominant colours it will average 2 (or more) colours.…