I N F O A R Y A N

OPENCV – PYTHON | Morphological Operations | Dilation, Erosion, Opening, Closing, Top-Black Hat

Morphological operations in OpenCV are fundamental techniques used in image processing to manipulate the shape and structure of objects within an image. These operations, including dilation, erosion, opening, closing, and top-hat/Black-hat transformations, play a crucial role in various applications such as image segmentation, noise removal, and feature extraction. In this article, we’ll delve into the concepts of these morphological operations, providing mathematical insights along with code examples for each.

 

Flow of Article:

  1. Understanding different operations
  2. Understanding the maths behind them
  3. Coding with help of Open-CV Python
  4. Understanding the uses of these morphological operations 

 

Video Explanation:

 

Dilation:

Dilation is a morphological operation that expands the boundaries of objects in an image. It involves convolving the image with a structuring element (kernel), where the value of the output pixel is the maximum value of all the pixels in the neighborhood defined by the kernel. Dilation is particularly useful in filling gaps, joining broken structures, and enlarging objects.

Mathematical Explanation:

Mathematically, dilation of an image A by a structuring element B is defined as:

OpenCV Python Code:

import cv2
import numpy as np

# Read the input image
img = cv2.imread(‘image.jpg’, 0)

# Define the structuring element (kernel)
kernel = np.ones((5, 5), np.uint8)

# Perform dilation
dilated_img = cv2.dilate(img, kernel, iterations=1)

# Display the results
cv2.imshow(‘Original Image’, img)
cv2.imshow(‘Dilated Image’, dilated_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Erosion:

Erosion is the opposite of dilation, where it erodes away the boundaries of objects in an image. It involves convolving the image with a structuring element, and the value of the output pixel is the minimum value of all the pixels in the neighborhood defined by the kernel. Erosion is useful in removing small islands of noise, separating connected objects, and shrinking objects.

Mathematical Explanation:

Mathematically, erosion E of an image A by a structuring element is defined as:

import cv2
import numpy as np

# Read the input image
img = cv2.imread(‘image.jpg’, 0)

# Define the structuring element (kernel)
kernel = np.ones((5, 5), np.uint8)

# Perform erosion
eroded_img = cv2.erode(img, kernel, iterations=1)

# Display the results
cv2.imshow(‘Original Image’, img)
cv2.imshow(‘Eroded Image’, eroded_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

OpenCV Python Code:

Opening:

Opening is a morphological operation that consists of an erosion followed by a dilation. It is useful in removing noise and small objects while preserving the shape and size of larger objects. Opening is typically applied to binary images.

Mathematical Explanation:

Mathematically, opening of an image A by a structuring element is defined as:

OpenCV Python Code:

import cv2
import numpy as np

# Read the input image
img = cv2.imread(‘image.jpg’, 0)

# Define the structuring element (kernel)
kernel = np.ones((5, 5), np.uint8)

# Perform opening
opened_img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)

# Display the results
cv2.imshow(‘Original Image’, img)
cv2.imshow(‘Opened Image’, opened_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Closing:

Closing is the opposite of opening, consisting of a dilation followed by an erosion. It is effective in closing small holes and gaps in objects while preserving the overall shape and size of the objects. Like opening, closing is typically applied to binary images.

Mathematical Explanation:

Mathematically, closing of an image A by a structuring element is defined as:

OpenCV Python Code:

import cv2
import numpy as np

# Read the input image
img = cv2.imread(‘image.jpg’, 0)

# Define the structuring element (kernel)
kernel = np.ones((5, 5), np.uint8)

# Perform closing
closed_img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)

# Display the results
cv2.imshow(‘Original Image’, img)
cv2.imshow(‘Closed Image’, closed_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Top-Hat/Black-Hat:

Top-hat and black-hat transformations are morphological operations that highlight bright or dark regions in an image relative to the surrounding areas. Top-hat transformation is the difference between the original image and its opening, while black-hat transformation is the difference between the closing and the original image.

OpenCV Code for Top-Hat and Black-Hat Transformations:

import cv2
import numpy as np

# Read the input image
img = cv2.imread(‘image.jpg’, 0)

# Define the structuring element (kernel)
kernel = np.ones((5, 5), np.uint8)

# Perform top-hat transformation
tophat_img = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)

# Perform black-hat transformation
blackhat_img = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel)

# Display the results
cv2.imshow(‘Original Image’, img)
cv2.imshow(‘Top-Hat Transformation’, tophat_img)
cv2.imshow(‘Black-Hat Transformation’, blackhat_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Applications of Morphological Operations in Image Processing:

Morphological operations play a vital role in various image processing tasks, offering versatile tools for shape manipulation, noise removal, and feature extraction. Here are some key applications where morphological operations are extensively utilized:

1. Image Segmentation:

  • Morphological operations are used to separate objects of interest from the background in an image, facilitating image segmentation tasks. Techniques like dilation and erosion help in delineating object boundaries and removing extraneous details.

2. Noise Removal:

  • Erosion and dilation operations are effective in eliminating noise from images, particularly salt-and-pepper noise or small isolated regions. Opening and closing operations further refine the denoising process by preserving the main features while removing unwanted artifacts.

3. Edge Detection:

  • Morphological gradient, obtained by subtracting the erosion of an image from its dilation, highlights edges and boundaries in the image. This technique is useful in edge detection applications, enabling the detection of object boundaries and contours.

4. Feature Extraction:

  • Morphological operations aid in extracting meaningful features from images, such as blobs, corners, and texture patterns. These features are crucial for tasks like object recognition, classification, and tracking in computer vision applications.

5. Medical Imaging:

  • In medical imaging, morphological operations are utilized for tasks such as tumor detection, blood vessel segmentation, and tissue analysis. These operations help in isolating specific anatomical structures and enhancing their visibility for diagnostic purposes.

6. Document Analysis:

  • Morphological operations find applications in document analysis tasks such as text extraction, character recognition, and document layout analysis. These operations assist in segmenting text regions, removing noise, and enhancing document clarity.

7. Remote Sensing:

  • In remote sensing applications, morphological operations are employed for land cover classification, change detection, and terrain analysis. These operations aid in processing satellite and aerial imagery, extracting meaningful information about the Earth’s surface.

 

Infoaryan, aryanverma, aryan verma, infoaryan blog, python opencv, opencv projects, python projects, Image processing, Morphology, image processing projects, OpenCV Projects