A mosaic image is a picture that has been divided into (usually equal sized) tiled sections, each of which is replaced with another photograph that matches the target photo. When viewed at low magnifications, the individual pixels appear as the primary image, while close examination reveals that the image is in fact made up of many hundreds or thousands of smaller images.
In this tutorial, We will use the k-nearest neighbors machine learning algorithm to build a mosaic image generator.
The big steps of the projects are described in the following picture:
We choose to use the CIFAR-100 dataset. The dataset consists of 60000 32×32 colour images in 100 classes, with 600 images per class. There are 500 training images and 100 testing images per class. In this project, we combine the training set and the test set together and ignore the labels. The dataset can be downloaded here.
The size of the images present in our dataset is 32x32x3. In order to generate a mosaic image very similar to the initial image, we choose to set the image tile size to 16x16x3. We will then resize the dataset images to the tile image size.
We define the following function to resize the dataset images.
3- Nearest Neighbor model
We use the implementation of the Nearest Neighbors algorithm present in SkLearn library.
We build the Nearest Neighbors model as follow:
We then use the joblib library to save the model for future use as follow:
4- Mosaic Image Generator
We follow the bellow steps to generate the mosaic image:
4.1- Resize input image to the nearest multiple of the tile image size
The first operation that we perform on the input image is to resize it to the nearest multiple of our tile image size.
From the original dimensions of the input image (width and height), we compute new dimensions that are multiple of our tile image size.
4.2- Generate the tiles images indexes
We now generate the tiles indexes of our resized image.
We store the tiles indexes in a Queue in order to process them with multiple threads.
4.3- Replace each tile image by its nearest neighbor in our dataset
For each tile index in our Queue, we use our pretrained model to lookup its nearest neighbor in our dataset. We then change the original image tile by its nearest neighbor in our dataset.
At the end of this steps, our newly created numpy array imgGen will reprepsent out mosaic image.
4.4- Save generated Numpy array as image
The last step is to save the numpy array filled in the previous step as an image.
5- Test mosaic image generator
We test our generator on multiples images.
Thanks for reading. Please leave feedback and questions in the comments!