Resampling a Numpy array means changing the size of the matrix. The most efficient way to resample a numpy array representing an image is using
scipy.ndimage.zoom(ndarray, zoom, output=None, order=3, mode='constant', prefilter=True, grid_mode=False)
- The ndarray is the array to resample.
- The zoom part accepts either a single number or sequence. Inputting a single number implies the image will get zoomed with the same value on all axes. On the other hand, a sequence changes the zooming in the x, y, and z order.
- The function creates an output of the same data type as the ndarray.
- Order represents the spline interpolation value ranging between 0 and 5.
- The mode determines how the interpolation will affect the output beyond boundary pixels. It takes nearest, mirror, reflect, constant, wrap values.
- prefilter confirms whether you want to apply spline filter on the ndarray before the interpolation.
Note: You must not include all of the above options when resampling a Numpy array representing an image. For instance, as the examples section shows, you can resample the array by specifying the interpolation order only.
Now that you understand the inputs and expected outputs when resampling a Numpy array, it would be best to find out the impact of the resampling function before applying it.
The Origin Of Image Resampling
A computer understands binary digits, 1s and 0s, often called bits. It can represent any object as long as you give it enough bits. Eight bits form a byte, a representation enabling you to handle more extensive data.
After knowing how to represent an image, the next challenge is transferring it. That is where conventions like ASCII and Unicode come in. Unlike ASCII, which handles English characters only, Unicode attaches a bunch of bytes to both English and non-English characters.
For example, most images are represented by 3 bytes in order of red, green, and blue (RGB). Apart from RGB, you can use other representations like grayscale.
The basic unit of an image is a pixel. Several pixels form a matrix. Hence, an image, in raw form, is a group of colours represented by numbers in a matrix.
It is also worth noting that a change in the value of the matrix transforms the original image. That is the motivation behind image processing.
Image processing is a multi-step conversion. It involves image display, filtering, cropping, rotating, flipping, segmentation, classification, registering, and resampling.
The Role Of Scipy.ndimage.zoom In Image Resampling
This section focuses on why you could prioritize
scipy.ndimage.zoom over other packages for image resampling. It starts by defining the challenge faced when processing images without
Traditional image processing entails 2D arrays of pixels. And maybe a third dimension for a colour channel; a fourth one for transparency information.
Scipy comes with packages such as
ndimage for image processing.
The scipy’s miscellaneous method resamples an array using the
misc module has specific built-in images, helping to kickstart data analysis without loading an image from another file. It also plays a critical role in opening an image.
The main drawback of
scipy.misc.imresize is that it wraps PIL’s resize function, which gives only four colour channels. The second option is
scipy.ndimage.map_coordinatesaccommodates spline interpolation for all kinds of resampling, including unstructured grids. However, it is slow for large arrays.
The ndimage (n-dimensional image) package comes with several image processing and analysis functions. One of the functions is zoom, which has implementations for 2D, 3D or more dimensions. Let’s use it to resample a Numpy array representing an image.
Resampling a Numpy Array Representing An Image Step-by-Step
🖊️Problem 1: Given a Numpy array with six elements in range, reshape the array to 2 by 3 dimensions then resample it using a zoom order 2 and bilinear interpolation.
Step~1: Choose The Right Package
We will pick
scipy.ndimage.zoom that we discussed in the previous section to resample the Numpy array representing an image.
import numpy as np import scipy.ndimage
Step~2: Get Ndarray
There are many ways to get an array of n-dimensions. You can read an image from an external file using image processing libraries or create one using NumPy’s methods such as
We can now resample the 2 by 3 array as follows.
# get ndarray ndarray = np.arange(6) # reshape the array for easier manipulation before_resampling = ndarray.reshape(2,3)
Step~3: Resample The Array
Let’s inspect the array before resampling.
# original array print("Before resampling: ") print(before_resampling)
We get a 2 by 3 Numpy array.
[[0 1 2] [3 4 5]]
We can now resample it.
# resampled array print("After resampling with a factor of 2 with the bilinear interpolation: ") after_resampling = scipy.ndimage.zoom(before_resampling, 2, order=1) print(after_resampling)
After resampling with a factor of 2 with the bilinear interpolation: [[0 0 1 1 2 2] [1 1 2 2 3 3] [2 2 3 3 4 4] [3 3 4 4 5 5]]
Let’s take a look at another example.
🖊️Problem 2: Given the following Numpy array representing an image,
[[10 11 12 13] [20 21 22 23] [30 31 32 33] [40 41 42 43]]
resample the array with zoom order 2 and spline interpolation.
Approach: We will store the array in a variable and manipulate it.
# Step~1: import the package import numpy as np import scipy.ndimage # Step~2: store the array print("before resampling: ") nd_array = np.array([[10, 11, 12, 13], [20, 21, 22, 23], [30, 31, 32, 33], [40, 41, 42, 43]])
then resample it.
# Step~3: resample the array print("After resampling with a factor of 2 with the spline interpolation: ") resampled_array = scipy.ndimage.zoom(nd_array, 2, order = 0) print(resampled_array)
After resampling with a factor of 2 with the spline interpolation: [[10 10 11 11 12 12 13 13] [10 10 11 11 12 12 13 13] [20 20 21 21 22 22 23 23] [20 20 21 21 22 22 23 23] [30 30 31 31 32 32 33 33] [30 30 31 31 32 32 33 33] [40 40 41 41 42 42 43 43] [40 40 41 41 42 42 43 43]]
Although there are many ways to resample a NumPy array representing an image, one of the most efficient packages is
scipy.ndimage.zoom. It accommodates image resizing using several interpolations, colour ranges and handles extensive array sizes.
Do you want to become a NumPy master? Check out our interactive puzzle book Coffee Break NumPy and boost your data science skills! (Amazon link opens in new tab.)