Saturday, November 16, 2024
Google search engine
HomeLanguagesImage Processing with SciPy and NumPy in Python

Image Processing with SciPy and NumPy in Python

In this tutorial, we will discuss Image Processing in Python using the core scientific modules like NumPy and SciPy. The images are made up of NumPy ndarrays so we can process and manipulate images and SciPy provides the submodule scipy.ndimage that provides functions that can operate on the NumPy arrays.

Image Processing with SciPy and NumPy in Python

 We will discuss how to open and write to images, and will also cover different manipulation and filtering techniques. So before getting started let’s see how to install both modules.

Installation

Numpy: To install numpy type the below command in the terminal.

pip install numpy

Installing Numpy on Linux using PIP

SciPy: You can use the above command to install SciPy as well.

pip install scipy

pip install scipy

Opening and Writing Images

The misc package of SciPy comes with some preloaded images. We will use those images to learn about image processing. One such image is provided by the face() function. The face() function will get a colored image of a raccoon face.

Example: Saving image using SciPy

Python3




from scipy import misc
import imageio
import matplotlib.pyplot as plt
 
# reads a raccoon face
face = misc.face()
 
# save the image
imageio.imsave('raccoon.png', face)
 
plt.imshow(face)
plt.show()


Output:

scipy open image

Example: Creating NumPy array from the image

Here we will read the image using the imread() function.

Python3




from scipy import misc
import imageio
import matplotlib.pyplot as plt
 
img = imageio.imread('raccoon.png')
 
print(img.shape)
print(img.dtype)
 
plt.imshow(img)
plt.show()


Output:

(768, 1024, 3)
uint8

imread image scipy

Creating RAW file

A RAW file is a file containing minimally processed data from an image sensor. We can create this file using the tofile() method of the scipy package.

Example: Creating RAW file using SciPy

Python3




from scipy import misc
import imageio
import matplotlib.pyplot as plt
 
# reads a raccoon face
face = misc.face()
 
face.tofile("raccoon.raw")


This will create a .raw file in our current working directory.

Opening RAW File

For opening .raw file we will be needing the NumPy module that will use the fromfile() method. This function is an efficient way of reading binary data with known data type as well as parsing simply formatted text.

Example: Reading from RAW file using NumPy

Python3




import numpy as np
 
img = np.fromfile('raccoon.raw',
                  dtype=np.uint8)
 
print(img.shape)


Output:

(2359296,)

Getting Statistical Information

We can use the max() and min() functions to get the maximum and minimum along the given axis. And to find the mean we can use the mean() function.

Example: Getting min, max, and mean values

Python3




from scipy import misc
 
img = misc.face()
 
print(img.max())
print(img.min())
print(img.mean())


Output:

255
0
110.16274388631184

Cropping Image

As we know that images are represented by numbers in a matrix, so changing the value of the matrix will result in changing the original image. Let’s see how to use this idea for cropping the image.

Example: Cropping the image

Python3




from scipy import misc
import matplotlib.pyplot as plt
 
# for grascaling the image
img = misc.face(gray = True)
 
 
x, y = img.shape
 
# Cropping the image
crop = img[x//3: - x//8, y//3: - y//8]
 
plt.imshow(crop)
plt.show()


Output:

cropping image scipy and numpy

Flipping Images

We can use the flipud() function of the numpy module to flip that image. This function flips the array(entries in each column) in up-down direction, shape preserved.

Example: Flip Image using Scipy

Python3




from scipy import misc
import numpy as np
import matplotlib.pyplot as plt
 
img = misc.face()
 
flip = np.flipud(img)
 
plt.imshow(flip)
plt.show()


Output:

flip image using numpy and scipy

Rotating Images

To rotate the images we can use the ndarray.rotate() function. This function rotates the image at a specific angle.

Example: Rotate Image using SciPy and NumPy

Python3




from scipy import misc,ndimage
import matplotlib.pyplot as plt
 
 
img = misc.face()
 
rotate = ndimage.rotate(face, 30)
 
plt.imshow(rotate)
plt.show()


Output:

rotate image using numpy and scipy

Filtering Images

In simpler terms image filtering is a process for enhancing or modifying an image where we can increase sharpness, enhance edges, or blurs the image. In Image filtering, some algorithm is applied to the pixel value of the given image and that algorithm determines the value of the output image. Let’s see some image filtering operations that can be done using NumPy and SciPy.

Blurring Images

Blurring an image is a process of reducing the level of noise in the image. For this, we can either use a Gaussian filter or a unicorn filter.

Example: Blur Images using SciPy and NumPy

Python3




from scipy import misc,ndimage
import matplotlib.pyplot as plt
 
img = misc.face()
 
blur_G = ndimage.gaussian_filter(img,sigma=7)
 
plt.imshow(blur_G)
plt.show()


Output:

blur images using scipy and numpy

Sharpening Images

Sharpening refers to increase contrast b/w light and dark regions and make the image more defined and brings out image features. There are three main reasons to sharpen your image: to overcome blurring introduced by camera equipment, to draw attention to certain areas, and to increase legibility. If blurry text is present in the image it becomes easy to read.

Example: Sharpening images using NumPy and SciPy

Python3




from scipy import misc, ndimage
import matplotlib.pyplot as plt
 
img = misc.face(gray=True).astype(float)
blur = ndimage.gaussian_filter(img, 5)
 
# Showing Blur Image
plt.imshow(blur)
plt.show()
 
blur_G = ndimage.gaussian_filter(blur, 1)
alpha = 30
sharp = blur+alpha*(blur-blur_G)
 
# showing sharp images
plt.imshow(sharp)
plt.show()


Output:

Sharpening images using NumPy and SciPy

Denoising Images

Denoising of an image refers to the process of reconstruction of a signal from noisy images. Denoising is done to remove unwanted noise from image to analyze it in better form. At first, let’s create a noisy image – 

Example 1: Creating a noisy image

Python3




from scipy import misc,ndimage
import matplotlib.pyplot as plt
import numpy as np
 
img=misc.face(gray=True).astype(float)
img=img[40:100,30:100]
 
noise_img=img+0.9*img.std()*np.random.random(img.shape)
 
plt.imshow(noise_img)
plt.show()


Output:

noisy image using scipy and numpy

To smooth the edges and the noise we use the Gaussian filter.

Python3




denoised = ndimage.gaussian_filter(noise_img, 2.2)
 
plt.imshow(denoised)
plt.show()


Output:

denoise gaussian filter

We can also preserve the edges using the median filter.

Python3




denoised = ndimage.median_filter(noise_img, 4)
 
plt.imshow(denoised)
plt.show()


Output:

denoised scipy and numpy

Edge Detection

The process of image detection involves detecting edges in the image. It works by detecting discontinuities in the brightness. For high-intensity variations, we can use Sobel, a gradient operator-

Example: Edge detection using SciPy and NumPy

Python3




from scipy import misc, ndimage
import matplotlib.pyplot as plt
import numpy as np
 
img = np.zeros((300, 300))
img[64:-64, 64:-64] = 1
img = ndimage.rotate(im, 30, mode='constant')
img = ndimage.gaussian_filter(im, 7)
 
# Original image
plt.imshow(im)
plt.show()
 
# edge detection
x = ndimage.sobel(im, axis=0, mode='constant')
y = ndimage.sobel(im, axis=1, mode='constant')
Sobel = np.hypot(x, y)
 
plt.imshow(sob)
plt.show()


Output:

edge detection scipy and numpy

RELATED ARTICLES

Most Popular

Recent Comments