Monday, 8 February 2016

Odd behaviour of imresize in Matlab

I stumbled across what I would classify as a bug in Matlab's imresize. Try out the following snipped of code:

little1 = imresize(IM, 0.2, 'nearest');
[M, N] = size(little1(:,:,1))
little2 = imresize(IM, [M, N], 'nearest');

I would expect little1 to be exactly the same as little2. If I wrote a routine to rescale images, I'd probably take as input the final dimensions [M,N], and if I had to handle a single scaling factor I'd use that to calculate the final width and height for the previous case.

It is not so in Matlab. I don't know exactly what's going on there, I don't have the time to debug imresize and see what happens. But if you try the snipped of code above for any image IM, you'll find that norm(little1(:)-little2(:)) is not 0, and in fact you can also imshow the absolute value of the difference and see where the resizing differs. Indeed, the picture below is exactly that.

This little quirk costed me probably about two days worth of work! I hope I've saved you some headaches by sharing this.