save_recolorto replace pixels from the given clusters (
replacements) with new ones of equal intensity, as specified by the
rgb_factorsvector. 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_factorsdistributes 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
kby specifying the
> 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!