Just wanted to say thanks for mentioning my book! I'm so happy that people are still getting value from it.
Using python and k-means to find dominant colors in images
11–20 of 34 posts
Re: Using python and k-means to find dominant colors in images
#12Cool, 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…
Re: Using python and k-means to find dominant colors in images
#13Your next assignment: find similarly coloured photos using the right data structure. See my stack overflow ticket http://stackoverflow.com/questions/10555511/need-a-proper-da...
Re: Using python and k-means to find dominant colors in images
#14Re: Using python and k-means to find dominant colors in images
#15http://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
#16Re: Using python and k-means to find dominant colors in images
#17The 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
#181. 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
#19It’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
#20Cool, 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…