Site icon R-bloggers

"Dominant" Color Palettes with k-means

[This article was first published on is.R(), and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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:
https://gist.github.com/3980277

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

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.