Composite image mosaics

We were sent the following through our Ask Us Anything page:

I really like you’re current series of image manipulation in R! Do you know how one might construct a collage image in R? A collage image (perhaps not the correct term?) is an image constructed of many small photos/images combined in a matrix with the overall color of each small image governed by some “master” image. When one looks at the image matrix from a distance it looks like the “master” image but up close you can still see all the smaller images. This seems very possible in R :)

We are very reader-friendly here at is.R(), so of course we hacked together a solution. Now, I don’t know exactly what algorithm the professionals use for this type of thing, so don’t expect professional results from the code in this Gist, but it will allow you to:

  1. Choose a set of images to serve as the mosaic tiles
  2. Choose a “master” image to re-build from small versions of the tile images
  3. Identify the most-similar tile images to each pixel (by proximity in RGB-space)
  4. Plot each tile image at the appropriate coordinates
Now, almost everything about this process could be improved, but the basics are all here. Upon close inspection, each of the individual component tile images can be seen, but from a distance, or when “squinting,” the master image is approximated pretty well. (Here are the original versions of the mosaics shown in this post — we chose small images because, well, R is sometimes a little slow big numbers.)

By d-sparks

Tags: png devtools RCurl ReadImages reshape