More K-Means Clustering Experiments on Images
I spent a little more time experimenting with k-means clustering on images and realized that I could use these clusters to recolor the image in interesting ways.
I wrote the function save_recolor to replace pixels from the given clusters (replacements) with new ones of equal intensity, as specified by the rgb_factors vector. For example, the following code will convert pixels of the first two clusters to greyscale.
> save_recolor("baby.jpeg", "baby_new.jpg", replacements=c(1,2),
rgb_factors=c(1/3, 1/3, 1/3))
It's greyscale because the rgb_factors distributes the pixel intensity evenly among the channels. A factor of c(20/100, 60/100, 20/100) would make pixels from the cluster 60% more green.
Let's get to some examples. Here's an unprocessed image, alongside its color clusters. I picked k=10. You can set k by specifying the palette_size parameter to save_recolor.
Here's what happens when I remove the red (the first cluster).
> save_recolor("baby.jpeg", "baby_new.jpg", replacements=1)
In the next image, I keep the red, and remove everything else.
> save_recolor("baby.jpeg", "baby_new.jpg", replacements=2:10)
Below, I replace the red cluster pixels, with green ones of corresponding intensity.
> save_recolor("baby.jpeg", "baby_new.jpg", replacements=1,
rgb_factors=c(10/100, 80/100, 10/100))
And this is a fun one: Get rid of everything, keep just the grass.
> save_recolor("baby.jpeg", "baby_new.jpg", replacements=c(1,3:10))
I tried this on various images, using different cluster sizes, replacements, and RGB factors, with lots of interesting results. Anyhow, you should experiment with this yourselves and let me know what you find.
I should point out that nothing here is novel or new -- it's all well known in image processing circles. It's still pretty impressive what you can do when you apply simple machine learning algorithms to other areas.
Okay, as in all my posts, the code is available in my GitHub repository:
https://github.com/0xfe/experiments/blob/master/r/recolor.rscript
Happy new year!





3 comments (imported from blogger)
By the way am noob in R!
NB: I manged to run your first sample on building color palettes ...
--hsm