Open In App

How to resample NumPy array representing an image ?

Last Updated : 05 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will be Resampling a NumPy array representing an image. For this, we are using scipy package. Scipy package comes with ndimage.zoom() method which exactly does this for us by zooming into a NumPy array using spline interpolation of a given order. Default is order 3 (aka cubic).

For input containing imaginary components, scipy. ndimage.zoom, zooms real and imaginary components independently.

Syntax: scipy.ndimage.zoom(input, zoom, output=None, order=3, mode=’constant’, cval=0.0, prefilter=True, *, grid_mode=False)

Parameters:

  • Input: It defines the ndarray
  • zoom : It takes both a sequence or a single number , if a single number it means apply zoom with same value in on all axis , if a sequence is provided then apply in the given order to x,y,z…etc.
  • Output: By default the output of same dtype of input will be created.
  • Order: Spline interpolation value , it must range between [0,5] inclusive.
  • Mode**: One of the most important parameters which decides how the interpolation  must happen beyond for boundary pixels it can take values from this list  [‘reflect’, ‘constant’, ‘nearest’, ‘mirror’, ‘wrap’].
  • prefilter : Takes boolean value and determine if the input array should be prefiltered with spline filter before interpolation or not.

Returns: A ndarray zoomed input.

Examples

For doing our tasks of zooming we will first create a ndarray as given below:

Python3




import numpy as np
import scipy.ndimage
 
ndarray = np.array([[11, 12, 13, 14],
                    [21, 22, 23, 24],
                    [31, 32, 33, 34],
                    [41, 42, 43, 44]])
print(ndarray)


Output:

[[11 12 13 14]
 [21 22 23 24]
 [31 32 33 34]
 [41 42 43 44]]

Example 1: In this example, we will pass 

  • ndarray as the input array
  • zoom: 2 (zoom with the value)
  • order: 0 (spline interpolation)

as order  = 0 and zoom = 2 so, zooming is done at axis with the same value.

Python3




print(scipy.ndimage.zoom(
  ndarray, 2, order = 0))


 
 

Output:

[[11 11 12 12 13 13 14 14]
 [11 11 12 12 13 13 14 14]
 [21 21 22 22 23 23 24 24]
 [21 21 22 22 23 23 24 24]
 [31 31 32 32 33 33 34 34]
 [31 31 32 32 33 33 34 34]
 [41 41 42 42 43 43 44 44]
 [41 41 42 42 43 43 44 44]]

 

Example 2: In this example, we will pass 

  • ndarray as input array
  • zoom : 2 (zoom with the value)
  • order : 1 (spline interpolation)

 as order  = 1 and zoom = 2 so, zooming is done at axis with the value+axis ie; value+4.

Python3




print(scipy.ndimage.zoom(
  ndarray, 2, order = 1))


 
 

Output:

[[11 11 12 12 13 13 14 14]
 [15 16 16 17 17 17 18 18]
 [20 20 20 21 21 22 22 23]
 [24 24 25 25 26 26 26 27]
 [28 29 29 29 30 30 31 31]
 [32 33 33 34 34 35 35 35]
 [37 37 38 38 38 39 39 40]
 [41 41 42 42 43 43 44 44]]

 

Example 3: In the case of multi-band images, we usually don’t want to interpolate along the z-axis to create add new bands into the images and therefore we should pass a sequence instead of a single number for the zoom factor parameter. 

Python3




import numpy as np
import scipy.ndimage
 
ndarray = np.array([[[11, 12, 13, 14],
                     [21, 22, 23, 24]],
                    [[31, 32, 33, 34],
                     [41, 42, 43, 44]]])
print(ndarray)
 
print(scipy.ndimage.zoom(ndarray, 1).shape)


Output:

[[[11 12 13 14]
  [21 22 23 24]]

 [[31 32 33 34]
  [41 42 43 44]]]
(2, 2, 4)

Example 4:

Python3




import numpy as np
import scipy.ndimage
 
ndarray = np.array([[[11, 12, 13, 14],
                     [21, 22, 23, 24]],
                    [[31, 32, 33, 34],
                     [41, 42, 43, 44]]])
 
print(scipy.ndimage.zoom(ndarray, (2, 2, 4)))


Output:

[[[11 11 11 11 12 12 12 12 13 13 13 13 14 14 14 14]
  [14 14 14 14 14 15 15 15 15 15 16 16 16 16 17 17]
  [18 18 19 19 19 19 20 20 20 20 20 21 21 21 21 21]
  [21 21 21 21 22 22 22 22 23 23 23 23 24 24 24 24]]

 [[16 16 16 17 17 17 17 18 18 18 18 18 19 19 19 19]
  [19 19 19 19 20 20 20 20 20 21 21 21 21 22 22 22]
  [24 24 24 24 24 25 25 25 25 25 26 26 26 26 27 27]
  [26 26 26 27 27 27 27 28 28 28 28 28 29 29 29 29]]

 [[26 26 26 26 27 27 27 27 27 28 28 28 28 29 29 29]
  [28 28 29 29 29 29 30 30 30 30 30 31 31 31 31 31]
  [33 33 33 34 34 34 34 35 35 35 35 35 36 36 36 36]
  [36 36 36 36 37 37 37 37 37 38 38 38 38 39 39 39]]

 [[31 31 31 31 32 32 32 32 33 33 33 33 34 34 34 34]
  [34 34 34 34 34 35 35 35 35 35 36 36 36 36 37 37]
  [38 38 39 39 39 39 40 40 40 40 40 41 41 41 41 41]
  [41 41 41 41 42 42 42 42 43 43 43 43 44 44 44 44]]]


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads