In this blog, we will discuss various morphological operators such as morphological gradient, white and black top-hat transform, etc. All these operators can be easily obtained by combining erosion and dilation operations. So, let’s discuss each of these methods in detail.
Morphological Gradient
This is the difference between the dilation and erosion of an image. Because dilation and erosion mostly affect the pixels that are close to the boundary between the foreground and background, their difference generally yields the boundary and thus this is used for edge detection and segmentation tasks. Now, let’s discuss how to implement this using OpenCV-Python.
One approach is to use OpenCV cv2.dilate() and cv2.erode() functions and then subtract these two. Another approach is to use OpenCV cv2.morphologyEx() function with cv2.MORPH_GRADIENT flag as discussed in the previous blog. Both of these approaches are shown below.
1 2 3 4 5 6 7 8 9 10 |
# Structuring element kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(3,3)) # Approach-1: Perform erosion and dilation separately and then subtract erosion = cv2.erode(img, kernel, iterations = 1) dilation = cv2.dilate(img, kernel, iterations=1) gradient1 = dilation - erosion # Approach-2: Use cv2.morphologyEx() gradient = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel) |
Top Hat Transform
This is mainly used to extract small details from the images. There are two types of top hat operators namely
- White top-hat transform: This is the difference between the image and its opening.
- Black top-hat transform: Difference between the closing and the input image.
As we already know that opening eliminates thin protrusions and salt noise, thus the white top hat transform will return these elements. Similarly, the black hat transform will return the pepper noise and gaps/holes that are filled. That’s why the name “White” and “Black”. Thus depending upon the structuring element used, these can be useful for feature extraction.
Now, let’s discuss how to implement this using OpenCV-Python. This can be implemented either manually for instance first finding the image opening and then subtracting it from the original image. The second approach is to pass the corresponding flag in the cv2.morphologyEx() function as shown below.
1 2 3 4 5 6 7 8 |
# Load the image img1 = cv2.imread("D:/downloads/adap1.jpg",0) # Structuring element kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(25,25)) # Apply the top hat transform tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel) # Apply the black hat transform blackhat = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel) |
This can also be used for tackling non-uniform illumination in images and thus can be a useful pre-processing tool for image segmentation tasks. The main idea is that you select the structuring element large enough that it erodes all the object region. Then the only thing remaining will be the background or the shading pattern. Subtracting this background from the image (or top-hat transform, in other words) will produce an image that reduces the non-uniform illumination effect. Thus the image can now be reasonably segmented. Below is an example of the image corrupted by non-uniform illumination and the result of applying white top-hat transform on that image. The image is thresholded using Otsu’s method.
That’s all for this blog. Hope you enjoy reading.
If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.