This jupyter notebook walks you through the basic image morphological transformations. These are normally performed for binary images i.e, those having pixels that are white(255) or black(0). The two most important methods are erosion and dilation. They are used in the pre-processing stage to remove noise from images.
import cv2 import numpy as np
# we are reading the image in grayscale format as indicated by the 0. img = cv2.imread('image.jpg',0) cv2.imshow('image',img) cv2.waitKey(0)
# define the kernel required for transformations. This is a 5x5 numpy array with all elements being 1. kernel = np.ones((5,5),np.uint8)
First let us perform erosion operation. We slide the kernel through the image, and for each pixel in the image, we observe the region in the kernel. If all the pixels in this region are white, that pixel is considered as white, otherwise it is eroded (made to zero). For erosion process it is best to keep the foreground of the image white. Then, erosion will make the boundary pixels black and thus reduce foreground thickness.
# the eroded image is stored in the variable erosion erosion = cv2.erode(img,kernel,iterations = 1) cv2.imshow('erosion',erosion) cv2.waitKey(0)
Now let us perform dilation, which is the opposite of erosion. Here, a pixel element is made white if atleast one pixel under the kernel is white. So it increases the white region in the image(size of foreground object increases).
dilation = cv2.dilate(img,kernel,iterations = 1) cv2.imshow('dilation',dilation) cv2.waitKey(0)
Now let’s look at another processs called opening. This is basically erosion followed by dilation. This is useful in removing noise in the background.
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel) cv2.imshow('opening',opening) cv2.waitKey(0)
The opposite process of opening is closing (dilation followed by erosion). It is useful in removing noise in the foreground.
closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel) cv2.imshow('closing',closing) cv2.waitKey(0)
Another feature is the morphological gradient which is the difference between dilation and erosion of the image. It looks like the outline of the image.
gradient = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel) cv2.imshow('gradient',gradient) cv2.waitKey(0)
Top hat and Black hat
There are two other transformations called top hat and black hat. Top hat is the difference between the input image and its opening. Black hat is the difference between closing of the input image and the input image.
tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel) blackhat = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel) cv2.imshow('Tophat',tophat) cv2.imshow('Blackhat',blackhat) cv2.waitKey(0)