Re-fetch numpy array representing image

I am looking for how to reselect a numpy array representing image data with a new size, preferably with a choice of interpolation method (closest, bilinear, etc.). I know that there is

scipy.misc.imresize 

which does just that by wrapping the resize function of the PIL. The only problem is that since it uses PIL, the numpy array must conform to image formats, giving me a maximum of 4 β€œcolor” channels.

I want to be able to resize arbitrary images with any number of "color" channels. I was wondering if there is an easy way to do this in scipy / numpy, or if I need to roll on my own.

I have two ideas on how to come up with my own:

  • function that runs scipy.misc.imresize for each channel separately
  • create your own scipy.ndimage.interpolation.affine_transform application

The first is likely to be slow for big data, and the second does not seem to offer another interpolation method other than splines.

+70
python numpy scipy image-processing python-imaging-library
Nov 05
source share
5 answers

Based on your description you want scipy.ndimage.zoom .

Bilinear interpolation will be order=1 , the closest - order=0 , and the cube - by default ( order=3 ).

zoom is specifically for fixed grid data that you want to convert to a new resolution.

As a quick example:

 import numpy as np import scipy.ndimage x = np.arange(9).reshape(3,3) print 'Original array:' print x print 'Resampled by a factor of 2 with nearest interpolation:' print scipy.ndimage.zoom(x, 2, order=0) print 'Resampled by a factor of 2 with bilinear interpolation:' print scipy.ndimage.zoom(x, 2, order=1) print 'Resampled by a factor of 2 with cubic interpolation:' print scipy.ndimage.zoom(x, 2, order=3) 

And the result:

 Original array: [[0 1 2] [3 4 5] [6 7 8]] Resampled by a factor of 2 with nearest interpolation: [[0 0 1 1 2 2] [0 0 1 1 2 2] [3 3 4 4 5 5] [3 3 4 4 5 5] [6 6 7 7 8 8] [6 6 7 7 8 8]] Resampled by a factor of 2 with bilinear interpolation: [[0 0 1 1 2 2] [1 2 2 2 3 3] [2 3 3 4 4 4] [4 4 4 5 5 6] [5 5 6 6 6 7] [6 6 7 7 8 8]] Resampled by a factor of 2 with cubic interpolation: [[0 0 1 1 2 2] [1 1 1 2 2 3] [2 2 3 3 4 4] [4 4 5 5 6 6] [5 6 6 7 7 7] [6 6 7 7 8 8]] 



Edit: As Matt S. pointed out, there are a few caveats for scaling multi-range images. I am copying the part below almost verbatim from one of my earlier answers :

Scaling also works for 3D (and nD) arrays. However, keep in mind that if you zoom in 2x, for example, you will scale along all axes.

 data = np.arange(27).reshape(3,3,3) print 'Original:\n', data print 'Zoomed by 2x gives an array of shape:', ndimage.zoom(data, 2).shape 

This gives:

 Original: [[[ 0 1 2] [ 3 4 5] [ 6 7 8]] [[ 9 10 11] [12 13 14] [15 16 17]] [[18 19 20] [21 22 23] [24 25 26]]] Zoomed by 2x gives an array of shape: (6, 6, 6) 

In the case of multi-band images, you usually do not want to interpolate along the z axis, creating new stripes.

If you have something like a 3-way RGB image that you want to enlarge, you can do this by specifying a sequence of tuples as a zoom factor:

 print 'Zoomed by 2x along the last two axes:' print ndimage.zoom(data, (1, 2, 2)) 

This gives:

 Zoomed by 2x along the last two axes: [[[ 0 0 1 1 2 2] [ 1 1 1 2 2 3] [ 2 2 3 3 4 4] [ 4 4 5 5 6 6] [ 5 6 6 7 7 7] [ 6 6 7 7 8 8]] [[ 9 9 10 10 11 11] [10 10 10 11 11 12] [11 11 12 12 13 13] [13 13 14 14 15 15] [14 15 15 16 16 16] [15 15 16 16 17 17]] [[18 18 19 19 20 20] [19 19 19 20 20 21] [20 20 21 21 22 22] [22 22 23 23 24 24] [23 24 24 25 25 25] [24 24 25 25 26 26]]] 
+102
May 12 '13 at 17:19
source share

If you want to reselect, you should look at the Scipy cookbook at rebinning . In particular, the congrid function defined at the end will support rebuilding or interpolation (equivalent to a function in IDL with the same name). This should be the fastest option if you do not want interpolation.

You can also directly use scipy.ndimage.map_coordinates , which will perform spline interpolation for any kind of scipy.ndimage.map_coordinates (including unstructured meshes). I find map_coordinates slow for large arrays (nx, ny> 200).

For interpolation on structured meshes, I usually use scipy.interpolate.RectBivariateSpline . You can choose the order of splines (linear, quadratic, cubic, etc.) and even independently for each axis. Example:

  import scipy.interpolate as interp f = interp.RectBivariateSpline(x, y, im, kx=1, ky=1) new_im = f(new_x, new_y) 

In this case, you perform two-line interpolation (kx = ky = 1) . The "closest" type of interpolation is not supported, since all this makes spline interpolation over a rectangular grid. This is also not the fastest method.

If you use bilinear or bi-cubic interpolation, then, as a rule, it is much faster to perform two 1D interpolations:

  f = interp.interp1d(y, im, kind='linear') temp = f(new_y) f = interp.interp1d(x, temp.T, kind='linear') new_im = f(new_x).T 

You can also use kind='nearest' , but in this case get rid of the transverse arrays.

+14
Nov 06 '12 at 4:10
source share

Have you watched a Scikit-image ? Its transform.pyramid_* functions may be useful to you.

+8
Nov 06
source share

I recently discovered a problem with scipy.ndimage.interpolation.zoom, which I sent as an error report: https://github.com/scipy/scipy/issues/3203

As an alternative (or at least for me), I found that scikit-image skimage.transform.resize works correctly: http://scikit-image.org/docs/dev/api/skimage.transform.html#skimage.transform .resize

However, it works differently for scipy interpolation.zoom - instead of specifying a multiplier, you specify the desired output form. This works for 2D and 3D images.

For two-dimensional images, you can use transform.rescale and specify a multiplier or scale, as if you were using interpolation.zoom.

+6
Jan 10 '14 at 16:13
source share

This solution scales the X and Y of the supplied image without affecting the RGB channels:

 import numpy as np import scipy.ndimage matplotlib.pyplot.imshow(scipy.ndimage.zoom(image_np_array, zoom = (7,7,1), order = 1)) 

Hope this is helpful.

0
May 24 '19 at 16:17
source share



All Articles