How to resample a NumPy array representing an image?


Resampling a Numpy array representing an image is the process of changing the size of the array while maintaining the quality of the image. We can resample an array using interpolation, decimation, and upsampling techniques in Python. We can use the ndimage.zoom() function for Scipy library in Python to resample a Numpy array representing an image. In this article, we will understand how to resample a Numpy array representing an image using the Scipy module in Python.

Interpolation

Interpolation is a technique used to estimate values between existing data points. In Numpy we have several interpolation methods like liner, cubic, and nearest neighbor. The interpolation technique is used when the new array is of higher resolution than the original array.

Decimation

Decimation is a technique that is used to decrease the size of the array by discarding some of the already present data points. The decimation technique is used when the new array has a lower resolution than the original array.

Upsampling

Upsampling is a technique used to increase the size of the array by inserting new data points between the existing data points. This technique is used when the new array has a higher resolution than the original array.

Resampling Numpy array representing an image using scipy module

To Resample the Numpy array we will use the scipy library and the ndimage.zoom() function in it to zoom the image and increase its size without affecting the quality of the image. You can follow the below steps to resample a numpy array representing an image.

Algorithm

The Numpy array representing an image can be resampled using the scipy library by following the below Algorithm −

  • Import the required libraries − NumPy, Scipy, and Matplotlib.

  • Load an image as a NumPy array using the imread() function from the Scipy library.

  • Display the original image using the imshow() function from the Matplotlib library.

  • Calculate the zoom factor for resampling the image. The zoom factor is the ratio of the new image size to the original image size.

  • Resample the image using the ndimage.zoom() function from the Scipy library. Pass the image array and the zoom factor as parameters to the function.

  • Display the resampled image using the imshow() function from the Matplotlib library.

  • Set the titles for both the original and resampled images using the set_title() function.

  • Show the figure using the show() function from the Matplotlib library.

  • Run the code and observe the output to check if the resampling has been successful.

  • Adjust the zoom factor as needed to increase or decrease the resolution of the image.

We can easily resample a NumPy array representing an image using the Scipy library and the ndimage.zoom() function.

Syntax

scipy.ndimage.zoom(input, zoom, output=None, order=3, mode='reflect', cval=0.0, prefilter=True)

Here, the parameters used in the ndimage.zoom() function are −

  • input − This is the input array that needs to be resampled. It should be a NumPy array.

  • zoom − This parameter is the zoom factor to use for resampling the image. It can be a scalar or a sequence of scalars, specifying the zoom factor along each dimension of the input array.

  • output − This parameter is an optional output array in which to place the resampled result. It should be a NumPy array of the same shape as the desired output.

  • order − This parameter is the order of the spline interpolation to use. It should be an integer, and its default value is 3.

  • mode − This parameter is the mode to use for handling points outside the input boundaries. It should be one of the following strings: 'reflect', 'constant', 'nearest', 'mirror', or 'wrap'. The default value is 'reflect'.

  • cval − This parameter is the value to use for points outside the input boundaries when mode='constant'. Its default value is 0.0.

  • prefilter − This parameter is a Boolean flag indicating whether to apply a pre-filter to the input data before resampling. Its default value is True.

Step 1: Import the necessary library

We will import numpy for array operation, and scipy for ndimage.zoom() function and matplotlib for image visualization.

import numpy as np
from scipy import ndimage
import matplotlib.pyplot as plt

Step 2: Loading the image

We will create a simple 3*3 image in the form of a numpy array and will scale this image in the next step.

image = np.array([[1, 2, 3],
               [4, 5, 6],
               [7, 8, 9]])

Step 3: Resampling the image

To zoom the image we will use the ndimage.zoom() function. The ndimage.zoom() function takes two arguments - the input array to be zoomed and the zoom factor. The zoom factor is a tuple that tells the scaling factor for each dimension of the input array. We will use the zoom factor of (2,2) to double the size of the image in both dimensions.

zoom_factor = (2, 2)
resampled_image = ndimage.zoom(image, zoom_factor)

Step 4: Visualizing the original and resampled image

We can visualize the original and the resampled image using the matplotlib plt.show() function. We create two subplots to display the original image in the first subplot and the resampled image in the second subplot.

# Create a figure with two subplots
fig, (ax1, ax2) = plt.subplots(1, 2)

# Display the original image in the first subplot
ax1.imshow(image, cmap='gray')
ax1.set_title('Original Image')

# Display the resampled image in the second subplot
ax2.imshow(resampled_image, cmap='gray')
ax2.set_title('Resampled Image')

# Show the figure
plt.show()

Example

The full code for resampling the image is given below −

import numpy as np
from scipy import ndimage
import matplotlib.pyplot as plt

# Load the image as a NumPy array
image = np.array([[1, 2, 3],
                 [4, 5, 6],
                 [7, 8, 9]])

# Define the zoom factor
zoom_factor = (2, 2)

# Resample the image using the ndimage.zoom() function
resampled_image = ndimage.zoom(image, zoom_factor)

# Create a figure with two subplots
fig, (ax1, ax2) = plt.subplots(1, 2)

# Display the original image in the first subplot
ax1.imshow(image, cmap='gray')
ax1.set_title('Original Image')

# Display the resampled image in the second subplot

ax2.imshow(resampled_image, cmap='gray')
ax2.set_title('Resampled Image')

# show the figure
plt.show()

Output

Conclusion

In this article, we discussed the ways in which we can resample the numpy array representing an image. Interpolation, decimation, and upsampling techniques are used to resample images. Scipy library of Python is used to resample the image using its ndimage.zoom() function.By changing the zoom factor, we can increase or decrease the resolution of the image as needed.

Updated on: 11-Jul-2023

977 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements