/  Technology   /  Image Processing using SciPy and Python
Image Processing using SciPy and Python

Image Processing using SciPy and Python

What is Image Processing?

Image processing is any form of processing for which the input is an image or a series of images or videos, such as photographs or frames of video. The output of image processing can be either an image or a set of characteristics or parameters related to the image.

SciPy

SciPy builds on the NumPy array object and is part of the NumPy stack which includes tools like Matplotlib, pandas and an expanding set of scientific computing libraries.

Let’s start with the basics.

First install SciPy library using command

pip install scipy

Let’s see how we can read an image and display an image using SciPy and python.

1) Reading and Displaying an Image

Let’s see the syntax for reading an image in our IDE. You just need scipy and matplotlib to display the image

Syntax

from scipy import misc
import matplotlib. pyplot as plt
image = misc. imread ("C:/Users/Desktop/cute-baby-animals-1558535060.jpg")
plt. imshow (image)
plt. show ()

Output

Image Processing using SciPy and Python

To read the image in a grayscale mode you just have to add one parameter while reading the image that is mode = “L” and then display the image

Output

Image Processing using SciPy and Python

2) Flip Image

Let’s see how we can flip the image upside down and left to right

Syntax

from scipy import misc
import matplotlib. pyplot as plt
import numpy as np
image = misc. imread ("C:/Users/Desktop/cute-baby-animals-1558535060.jpg”, mode = "L")
flip_image_LR = np. fliplr (image)
flip_image_UP = np. flipud (image)
plt. imshow (image)
plt. show ()
plt. imshow(flip_image_LR)
plt. show ()
plt. imshow(flip_image_UP)
plt. show ()

Output

Original Image

Image Processing using SciPy and Python

Left to right image

Image Processing using SciPy and Python

Upside down image

Image Processing using SciPy and Python

3) Blurring or Smoothing Effect

To give this effect we can use Gaussian filter.

Gaussian Filter

A Gaussian filter is a linear filter which is used to blur an image or to reduce its noise.

Let’s see an example

Syntax

from scipy import misc, ndimage
import matplotlib. pyplot as plt
import numpy as np
image = misc. imread("C:/Users/Desktop/cute-baby-animals-1558535060.jpg")
blurred=ndimage. gaussian_filter (image, sigma=6)
plt.imshow(image)
plt.show()
plt. imshow (blurred)
plt. show ()

Output

Image Processing using SciPy and Python

We can also use uniform filter for smoothing or blurring effect

Let’s see an example

Syntax

ndimage. uniform_filter (image, size = 1, mode = “reflect”)

There are different types of mode such as wrap, mirror, constant, nearest. By default, it is reflect.

Syntax

from scipy import misc, ndimage
import matplotlib. pyplot as plt
import numpy as np
image = misc. imread("C:/Users/suyog/Desktop/cute-baby-animals-1558535060.jpg")
uniform = ndimage. uniform_filter (image, size = 10, mode = "reflect")
plt. imshow(image)
plt. show ()
plt. imshow(uniform)
plt. show ()

Output

Image Processing using SciPy and Python

4) Geometrical Transformation

We can rotate, crop and flip the image using scipy

Syntax

from scipy import misc, ndimage
import matplotlib.pyplot as plt
import numpy as np
image = misc. imread("C:/Users/Desktop/cute-baby-animals-1558535060.jpg")
rotate_noshape = ndimage. rotate (image, 45, reshape = False)
rotate = ndimage. rotate(image,45)
lx, ly, lz = image. shape
crop_face = image [lx // 3: - lx // 3, ly // 5: - ly // 5]
plt. imshow(image)
plt. show ()
plt. imshow(rotate)
plt. show ()
plt. imshow(rotate_noshape)
plt. show ()
plt. imshow (crop_face)
plt. show ()

Output

Image Processing using SciPy and Python

 

In this way we have seen some basics of Image processing using scipy and python

Leave a comment