"Dominant" Color Palettes with k-means

November 27, 2012
By

(This article was first published on is.R(), and kindly contributed to R-bloggers)

The idea of deriving color palettes from images has long been an interest of mine. There are places you can do this online, but it’s at least plausible that you might want to identify dominant colors programatically, for a large number of images. Charles Leifer shows one approach to the problem with python, and today’s Gist illustrates how to do the same in R, with a couple of additions.

For example, starting with this impressive image from the National Geographic Photo Contest (The Big Picture is an excellent source for images to test), the code below will generate an n-color palette around the centroids of n clusters in RGB color space, allowing you to plot palette color frequencies:

The next part of the code shows how to re-draw the image with the new, limited palette. This often produces a really stunning effect, as you can see:

To leave a comment for the author, please follow the link and comment on his blog: is.R().

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.