Live data from Hacker News

Using python and k-means to find dominant colors in images

charlesleifer.com

21–30 of 34 posts

Re: Using python and k-means to find dominant colors in images

#21
Anyone 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

#22
Nice. I'm not sure though that the geometric distance between two RGB vectors is always a good measure of their perceived similarity. Converting to a device independent space and using a different metric could improve the results. See the following links for standard metrics:

http://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

#24
post #4

Just wanted to say thanks for mentioning my book! I'm so happy that people are still getting value from it.

/metoo! devoured it and learned a lot. I recently dumped a bunch of technical books, but that is one that I kept and plan on keeping for a long time. It's got a timeless nature to it as a "working-reference" compared to only theoretical descriptions or any one specific implementation in "real-world" code.

Re: Using python and k-means to find dominant colors in images

#25

I'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…

A lot depends on what you are trying to achieve. In my case, for example, I wanted to group images that may have perceptually similar colours either as a dominant component or as an accent (say, a red shirt and a white shirt with red stitching).

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

#26
A fun project might be to use a dominant color-finding algorithm on each frame of a movie in order to create a color time series (then do it on a corpus of movies). Then you could attempt to create a "Shazam for movies" that could be used to identify movies just by pointing your iPhone at a wall (in a dark room). That would hopefully use the changing brightness of the movies to read a time series and allow you to match it up. Easier said than done of course :)

Re: Using python and k-means to find dominant colors in images

#27
post #20

Cool, 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.

I think this would work well for a lot of things, but I forgot to mention one strange caveat - there were a lot of bracelet/necklace type items, where the important colors might actually be at the corners and not in the center.

Re: Using python and k-means to find dominant colors in images

#28
post #10

Awesome, 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…

I like this solution much better, here is why:

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

#29
post #21

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

There are density based clustering algorithms which I believe help with this.

Re: Using python and k-means to find dominant colors in images

#30
post #18

There 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.…

IRT #1 good point. Doing a "quick" nearest neighbor to the centroids would help with that I'd bet.
Post reply on HN