SciPy Ndimage – Image Manipulation and Processing in SciPy
Free NumPy course with real-time projects Start Now!!
Scipy is a basic scientific library and contains many sub-packages. There are many general sub-packages. Some other packages are more specific and are key elements in a variety of fields.
One such package is the n-dimensional image package. The ndimage package has vast applications in the field of data science and machine learning.
SciPy Ndimage
SciPy contains the ndimage (n-dimensional image) package. It is basically useful for image processing and for image analysis. Its main focus is on image processing.
It includes many image processing features like – input and output image, classification of images, extraction, manipulations of the image, image segmentation, and filter.
These are some basic operations that we can perform on the images while processing it. There are many more special functions available. We can use the package by scipy.ndimage function.
Opening and Writing Image Files
The scipy.ndimage module consists of a misc package. This package consists of a few custom images which we can use for demonstrating the module operations.We plot the image on a graphical scale.
from scipy import misc import matplotlib.pyplot as plt f = misc.face() plt.imshow(f) plt.show()
Output
The matrix format is for the representation of the images and their color combinations. The changes are made by manipulating these numbers. The color representations can be done either in grayscale or RGB.
We can also scale the images and rotate the images. We can turn the image on any side or make it upside down.
import numpy as np from scipy import misc import matplotlib.pyplot as plt face = misc.face() #flip function flip_face = np.flip(face) plt.imshow(flip_face) plt.show()
Output
We can also rotate the image at a specific angle. We can pass the degree of rotation as an argument.
rom scipy import misc,ndimage import matplotlib.pyplot as plt face = misc.face() #rotate functiom rotate_face = ndimage.rotate(face, 45) plt.imshow(rotate_face) plt.show()
Output
SciPy Image Filters
We can also modify the images by applying different filters over the images in SciPy. We highlight some sections of the image and implement these filters along with other concepts. The functions apply pixel algorithms to apply filters on the images.
Blurring image in SciPy
It is a process of the noise and color reduction of the image. We apply it using the image filter. The sigma argument is to determine the level of blur on the image.
from scipy import misc from scipy import ndimage import matplotlib.pyplot as plt face = misc.face() blurred_img = ndimage.gaussian_filter(face, sigma=4) plt.imshow(blurred_img) plt.show()
Output
Edge Detection in SciPy
We have the concept of edge detection in image processing. It is meant to determine the boundaries of the objects inside the image. The output can be then used for image segmentation and data extraction.
First, we generate an image for performing edge detection.
import scipy.ndimage as nd import numpy as np import matplotlib.pyplot as plt im = np.ones((256, 256)) im[64:-64, 64:-64] = 2 im[90:-90,90:-90] = 3 im = ndimage.gaussian_filter(im, 10) plt.imshow(im) plt.show()
Output
We have a square to which we will apply the functions of edge detection. We use the sobel() function.
import scipy.ndimage as nd import matplotlib.pyplot as plt im = np.zeros((256, 256)) im[64:-64, 64:-64] = 1 im[90:-90,90:-90] = 2 im = ndimage.gaussian_filter(im, 8) zx = ndimage.sobel(im, axis = 0, mode = 'constant') zy = ndimage.sobel(im, axis = 1, mode = 'constant') sobl = np.hypot(zx, zy) plt.imshow(sobl) plt.show()
Output
Summary
Image processing is a vast field to work with. The SciPy library consists of the ndimage module for processing images. This module consists of a number of functions for image manipulation.
It consists of rotation functions. It can also apply filters using pixel algorithms. Also, it consists of operations for edge detection that is useful for data segmentation.
Did we exceed your expectations?
If Yes, share your valuable feedback on Google