Building mosaics from an image set
02-11-2025
Categorising images
The first thing is which methodology to use: average image colour does not result in a good enough result. An image that is mostly red with bits of green, for instance, would have an average colour of brown. Therefore, we don't want the average colour, we want the "dominant colour", or, the colour (and attached colours with mild variations in tone) that occupies the largest portion of the image.
The simplest way to do this is through k-means clustering, in which we will turn all the pixels in the image into points in a tridimensional space. Then, we measure the euclidian distance between each point to form up our clusters (by dividing up the points into groups with minimal distance).
Most importantly, the number of clusters needs to be carefully selected. 1 cluster means we're still getting the average colour, so that won't do. 2 colours is good enough, but if you have a background you want to eliminate from the equation, that is also insufficient. I settled for 4, as it provides a good balance between granularity, how generic I can make my "model" and how many colours I can pick. One for the background, one "dominant" active colour, and all others can be discarded.
Then, we get the centre of each cluster - that is the mean colour in each cluster, and therefore, the actual "dominating colour" in each dominating colour. By measuring cluster sizes, we should be able to determine which colour is most present in the image, and there we go! We categorised an image!
Categorising mosaic divisions
Mosaics are subdivided into an arbitrary number of equally-sized fragments, and we'll use the same k-means method to categorise each subdivision's colour. Then, since we've already categorised our whole image set, we can measure the distance between the dominant colour in each subdivision, and the dominant colours of each image.
Once we get the "closest" image, we append it to the bidimensional array of mosaic components, and eliminate it from the selectable image dataset (if we don't want repeat images).