Array/Image zoom

Does anyone know if there is a package providing a function like scipy.ndimage.zoom https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.zoom.html?

1 Like

Sorry for necro-ing this older post, but since this is hasn’t been answered yet, I’m share my experience.

The closest thing I have found is imresize() from the Images package. However, I have been unable to achieve the exact same results. We are using

ndimage.zoom(data, scale, order=1)

in Python, and I thought

imresize(data, ratio=scale, method=BSpline(Linear()))

but AFAICS the results are not exactly the same.

imresize works very well.
To get the same result, you must call scipy.ndimage.zoom for each image channel and stack the corresponding outputs to get the zoomed image.
See this SO answer.

Thanks for the reply, but I’m afraid that’s not the issue I’m encountering. The image is single-channel.

FWIW imresize does work well. I’m just mentioning that the interpolation algorithm doesn’t seem to be the same as ndimage.zoom, so the resulting scaled array will not be the same (I’ve done some random sampling and I’m seeing differences even in the second significant decimal digit). I’m not saying the results are wrong, but it would be nice if there was an algorithm equivalent ot he scipy one (I’m not even sure what that is).

That’s often the thing with using algorithms from libraries. If you want to have control of exactly what happens or want to get the same results in different implementation/environments, you’re better off implementing them yourself. If you just want to have some valid result, using a library is of course fine, most of the time.

1 Like

The default order for ndimage.zoom is 3, i.e. Julia BSpline(Cubic()), while for imresize° is BSpline(Linear()). If you set the same interpolation method for both you get the same image.

1 Like

As I mentioned in my first reply, we are using order=1 for zoom, and BSpline(Linear()) for imresize, and the results are different.

I’ve done some additional testing, and I think I’ve found the hangup.

So, my first test was to use gdalwarp (via ArchGDAL) instead of imresize: the bilinear interpolation from GDAL gives essentially the same results as imresize.

At this point, I read the scipy.ndimage.zoom documentation, and found out that the default for it is grid_mode=False, which basically puts the matrix value at the pixel center. Using grid_mode=True (and mode='nearest') in zoom seems to give the same result as imresize().

2 Likes