How to resize N-d numpy image?
I don't just want to subsample it, but to interpolate / average the pixels.
For example, if I start with
array([[[3, 1, 3, 1],
[3, 1, 3, 1],
[3, 1, 3, 1],
[3, 1, 3, 1]],
[[3, 1, 3, 1],
[3, 1, 3, 1],
[3, 1, 3, 1],
[3, 1, 3, 1]]], dtype=uint8)
and shrink it by a factor of 2 in all dimensions, I want the output to be
array([[[2, 2],
[2, 2]]], dtype=uint8)
Attempted solutions:
A. SciPy ndimage:
>>> scipy.ndimage.interpolation.zoom(x, .5, mode='nearest')
array([[[3, 1],
[3, 1]]], dtype=uint8)
(The optional order
parameter makes no difference)
B. Looping over 2**3
possible offsets: ugly, slow, works only for integer zoom factors, and needs extra steps to avoid overflows.
C. OpenCV and PIL work only with 2D images.