Turning an Image Into a Collage of Cat Pictures

by NickG74 in Circuits > Art

1485 Views, 11 Favorites, 0 Comments

Turning an Image Into a Collage of Cat Pictures

56f358a245bceb0f5800010e.jpeg

Modern data structures and image processing techniques can be combined to solve a wide variety of problems. This instructable will walk you through the process of using these techniques to turn an image into a collage of cat pictures. Turning your friends, family, and pets into images composed of cats can provide hours of entertainment. The time taken to write the program may take several hours but creating an individual collage can be done in less than a second. This process requires intermediate programming knowledge, familiarity with data structures, and knowledge of basic image processing techniques.

Gather Cat Pictures

Collect.png

Many cat pictures are needed to create an accurate collage. These images can be collected from online sources such as Reddit or imgur as well as any personal collection of cat images you may possess. For best results, try to collect images of varied colors and lighting conditions.

TIP: Imgur has an API that allows users to find images posted to a subreddit. Using this API it is possible to quickly find thousands of cat pictures.

Process the Images

resize.png

In order to create the collage, the cat pictures must be small squares. Tools such as ImageMagick or OpenCV can be used to crop and resize the image. The size of the image is dependent on the desired size of the collage. For example, a collage of 128x128 images that are each 64x64 would be 8192x8192.

NOTE: The cat may be cropped out unless care is taken when cropping images. A tool should try to identify the cat before cropping takes place.

Index the Cat Pictures

Index.png

Find the average color of the cat picture. This color may be represented as a point in three dimensional space. For best results, the color should be converted from the original color space to LAB color. LAB color has a property where the Euclidean distance between two colors is equivalent to the perceived difference between them. This reduces the problem of finding the closest cat picture to a color to finding the nearest neighbor of the point in space. Accordingly, the data structure used to index the image should be capable of doing nearest neighbor or radius based searches to find these colors. Insert the image into your chosen data structure.

TIP: If you are unaware of data structures that may be used for this step, a KD Tree is a good fit.

Resize the Source Image

56f34a8515be4dc27400088c.jpeg

Resize the image that you are turning into a collage. The image should be resized such that every pixel of the resized image represents a single cat picture. The image should maintain its original aspect ratio and does not need to be square.

TIP: For optimal quality, use a high quality filter such as Lanczos interpolation to resize the image.

Search Through Your Palette of Cat Images

Search.png

For each pixel in the resized image, find images with a similar color. This is done by converting the pixel to LAB color space then using the data structure to find close colors. A radius search may be used to find all images close to the desired color within a certain distance. If no colors exist after doing this, a nearest neighbor search can be done to find the closest. To avoid repetition, chose a random image from these candidates to represent the pixel.

TIP: You may want to experiment with different radiuses for your search. A small radius will lead to a collage with more accurate color but less diversity of images.

Paint the Cat Pictures to a Canvas

Screen Shot 2016-03-23 at 9.14.13 PM.png

Using a graphics library, create a blank canvas the size of the entire collage. Paint each of the palette images found in the previous search to their respective position in the collage.

Congratulations, you now have a collage of cat pictures! For more fun, try creating collages of different types of images or palettes. Examples of this could be creating a park out of insects or a politician out of genitalia.