I have always loved programming - its like Lego without gravity.

Basic on my ZX81 graduating to assembler and Turbo Pascal during my teens.

Developed phone OS software - engineer, architect, product manager - but got made irrelevant by the iPhone and redundant by Android.

These days I mostly work with data, big data and fitting big data onto small boxes.

SSIM vs MSE for Mosaics

(On the left, a target image.  On the right, a mosaic made from thumbnails of all the Ludum Dare 32 games, positioned using an MSE score metric.)

Making mosaics is in two steps:

  1. score each tile in each position
  2. determine the optimal positioning of all tiles to maximize the total likeness

The second part is easy (although I tried imprecise approximations at first, and only discovered it fairly recently): use the Hungarian Algorithm.

Its that first step that’s tricky.

I have been using Mean Square Error (MSE) and have been fairly happy with the result.  But readers have pondered if there aren’t better metrics that better account for shape and contrast and such…

So to please some Ludum Darers (and because I was curious), I tried out some algorithms.

First, Peak Signal to Noise Ratio (PSNR) is just a rephrasing of MSE and the results are much the same.

Second, I tried Structural Similarity (SSIM) index.  This was a promising avenue to explore.  My mosaic maker is Python and there’s a scikit helper, but scikit doesn’t work with pypy (and neither does a stock numpy; trying to get the mosaic maker to work happily in pypy with these kinds of dependencies has been a nightmare).  In the end I went with pyssim which turned out to not work in pypy either due to dependencies but I stuck with it on cpython anyway.  Its about 4x slower than MSE and most of the cost seems to be comparison overhead and not really affected by how big the patches I compared were.

SSIM is actually working in luma (eh fancy term for grayscale) and trying to account for shape not colour.

The results:

image

(On the left, the original.  In the middle, MSE.  And on the right, SSIM.  Tiles are the thumbnails of 2819 Ludum Dare 32 games (omitting 2 games with broken thumbnails!))

So I tried blending the MSE and SSIM scores (suitably scaled of course) but couldn’t get even a little bit of SSIM to help:

image

(Here 70% MSE and 30% SSIM.  I even tried 8% SSIM and it looked much the same.)

I also tested using the YCbCr colour-space too; results were much the same (as all the tiles are screenshots they are invariably natively RGB; if the tiles were made from photos there’s a good chance that YUV makes much more sense).

Now I am making a novice interpretation of this but my guess is that in mosaics, which are best viewed from a great distance where the tiles are increasingly pixel-sized, tile colour dominates over shapes within the tile?

jump to ↓



performance
Faster searches with non-prefix fields in composite indices
Compressing MySQL databases
What highscalability.com says about Scaling my Server
Scaling my Server: follow-up
old classics
The kid's computer
Making the History of Worlds Religions map
If you defend those involved in the OpenGL ES specification, you are an idiot
Stackoverflow unwinding?
general
Why Swift?
Python annotations and type checking
pycon 2014 Sweden: the bad bits
Table-based Template Translation in C++
recreation
games programming
Perlin Noise
Perlin Noise
Drawing RTS maps fast
WillCity update
ludum-dare
Ludum Dare #35 Mosaic
LudumDare 33 wallpapers
SSIM vs MSE for Mosaics
Ludum Dare 30 results are in!