Live data from Hacker News

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

charlesleifer.com

11–20 of 34 posts

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

#11
post #4

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

just wanted to say that your book is what got me interested in data science and python. I would be in a very different place today if it wasn't for your book! So, thank you!

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

#12

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…

[deleted]

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

#15
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 throw out very-light/dark pixels. * Dribbble does a very good job of this - look at any of their individual photo pages. * For production use, C would be much more appropriate at solving this problem (but python is more fun to use).

Another cool application of color analysis like this:

http://www.vijayp.ca/blog/2012/06/colours-in-movie-posters-s...

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

#17
I made my own implementation of this a while ago as well. One thing to keep in mind though is that the human eye doesn't perceive all colors uniformly. This means you can't treat the R,G,B spectrum as a 3-dimensional space, as the distance between two points in this space doesn't take the characteristics of the human eye into account.

The solution to this is to convert your RGB colors to the CIELab color space (http://en.wikipedia.org/wiki/Lab_color_space) and use the Lab values for your 3-dimensional space.

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

#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. (The same might happen for more complex images with a a lot of contrast).

2. When randomly initializing k-means there is a good chance you'll find one of the local optima, so running it more than once will return different colours. In general it is good practice to run it several times and choose the outcome with the lowest cost.

3. K-means can take a long time to converge; limit the amount of iterations it can do.

These things aside, very cool usage of k-means on image data!

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

#19
K-means clustering is super cool. Myself and a friend used it a few months ago to filter moving things out of webcam video in JavaScript. It takes samples of video and puts the color of each pixel across all the recent samples into clusters, and discards the smaller cluster (for each pixel) because it was probably some object (like a person) moving across the frame without sticking around.

It’s not super performant, but the end result is awesome — after a few seconds, you and up with a picture what your webcam sees minus the moving things. For the curious:

http://sidnicious.github.com/longcamera/k-means/longcamera.h...

(I haven’t touched the code in a while, save for fixing a bug just now)

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

#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.
Post reply on HN