Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

Image Processing in Python – Edge Detection, Resizing, Erosion, and Dilation

Image Processing

Image processing is a field in computer science that is picking up rapidly. It is finding its applications in more and more upcoming technologies. Image processing in Python also provides room for more advanced fields like computer vision and artificial intelligence.

It is a collection of operations that you can perform on an image. Often these operations are needed to transform the image into a format that is easier to train on. Some of these operations are converting to grayscale, rotating, cropping and edge detection.

A proper definition can be given as :

Image processing involves performing some operations on an image, in order to get an enhanced image or to extract some useful information from it.

We will look at some of the important image processing operations in this tutorial.

In this tutorial, we are using OpenCV to achieve the task of image processing. This is a followup tutorial on our previous one on reading images in Python.

So let’s start by learning how to import an image into python using OpenCV. Before we do that, we need to install OpenCV on to our system.

Installing Python’s Image Processing Library – OpenCV

You can install OpenCV using the pip command given below :

pip install opencv-python
Install Opencv
Install Opencv

After you are done with the installation you can start using it once you import it.

The line of code to import OpenCV onto your python notebook is :

import cv2

Edge Detection using Python

OpenCV also provides you with the option to detect edges in your image. Edge detection is widely used in feature description, image segmentation, image enhancement, image restoration, pattern recognition, and image compression.

We will perform edge detection using the canny edge detector. Canny Edge detection requires a maximum value and a minimum value to carry out edge detection.

Any edges with intensity gradient more than maxVal are sure to be edges and those below minVal are sure to be non-edges and are hence discarded.

To carry out edge detection use the following line of code :

edges = cv2.Canny(image,50,300)

The first argument is the variable name of the image.

The complete code to save the resulting image is :

import cv2
image = cv2.imread("sample.jpg")
edges = cv2.Canny(image,50,300)
cv2.imwrite('sample_edges.jpg',edges)

The resulting image looks like:

Edge Detection
Edge Detection

Resizing an Image in Python

Resizing is another important operation that you will need to perform while dealing with images.

OpenCV provides you with a method to resize your images. To resize your images, use the following line of code:

res = cv2.resize(img,None,fx=2, fy=2, interpolation = cv2.INTER_CUBIC)

Here fx is the scaling factor along the horizontal axis and fy along the vertical axis.

Different interpolation methods are available for different functionalities.

You can use cv2.INTER_AREA for shrinking and cv2.INTER_CUBICcv2.INTER_LINEAR for zooming. Cubic interpolation is slower as compared to linear interpolation.

Morphological Image Processing Operations

OpenCV also gives you the option to perform morphological operations such as Erosion, Dilation, Opening, Closing on your image.

Morphological operations are based on shapes. To apply a morphological operation on an image you need a structuring element. A structuring element is a 2D binary matrix.

1. Image Erosion

The basic concept of erosion in image processing is like that of soil erosion. It erodes away the boundaries of the foreground object.

Erosion decreases white regions in your image.

To apply erosion on your images, use the following lines of code.

kernel = np.ones((5,5),np.uint8)
erosion = cv2.erode(image,kernel,iterations = 30)

The first line here declares the kernel, the second line uses the kernel to perform erosion.

The complete code to save the resulting image is :

import cv2
import numpy as np
image = cv2.imread("sample.jpg")
kernel = np.ones((5,5),np.uint8)
erosion = cv2.erode(image,kernel,iterations = 30)
cv2.imwrite('sample_erode.jpg',erosion)

The resulting image looks like:

Erosion
Erosion

2. Image Dilation

Dilation is the opposite of erosion. It increases the white region in your image.

To apply dilation on your image, use the following lines of code :

kernel = np.ones((5,5),np.uint8)
dilation = cv2.dilate(image,kernel,iterations = 30)

The complete code that saves the resulting image is as follows:

import cv2
import numpy as np
image = cv2.imread("sample.jpg")
kernel = np.ones((5,5),np.uint8)
dilation = cv2.dilate(image,kernel,iterations = 30)
cv2.imwrite('sample_dilate.jpg',dilation)

The resulting image looks like:

Dilation
Dilation

For removing noise from your image you can perform erosion followed by dilation.

Conclusion

This tutorial was about image processing in python. We discussed some basic image processing operations provided by OpenCV. To learn more about OpenCV, you can refer to their official tutorials.