"Dominant" Color Palettes with k-means

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:

By d-sparks

Tags: rstats ReadImages reshape ggplot2