Different Morphological Operations in Image Processing
Last Updated :
30 Jun, 2024
Morphological operations are image-processing techniques used to analyze and process geometric structures in binary and grayscale images. These operations focus on the shape and structure of objects within an image. They are particularly useful in image segmentation, object detection, and noise removal tasks. Morphological operations aim to probe and transform an image based on its shape, enhancing features or removing imperfections.
What is Morphological Operations? Morphological operations are techniques used in image processing that focus on the structure and form of objects within an image. These operations process images based on their shapes and are primarily applied to binary images, but can also be extended to grayscale images. The core idea is to probe an image with a structuring element and modify the pixel values based on their spatial arrangement and the shape of the structuring element. Key morphological operations include erosion, dilation, opening, closing, and others, each serving distinct purposes in enhancing and analyzing images.
Morphological operations rely on two key elements:
The Input Image: Usually a binary image, where the objects of interest are represented by foreground pixels (typically white) and the background by background pixels (typically black). Grayscale images can also be processed using morphological operations. The Structuring Element: A small matrix or kernel that defines the neighborhood of pixels over which the operation is performed. The shape and size of the structuring element can greatly influence the outcome of the morphological operation. Different Morphological Operations 1. Erosion Erosion is a fundamental morphological operation that reduces the size of objects in a binary image. It works by removing pixels from the boundaries of objects.
Purpose: To remove small noise, detach connected objects, and erode boundaries. How it Works: The structuring element slides over the image, and for each position, if all the pixels under the structuring element match the foreground, the pixel in the output image is set to the foreground. Otherwise, it is set to the background. 2. Dilation Dilation is the opposite of erosion and is used to increase the size of objects in an image.
Purpose: To join adjacent objects, fill small holes, and enhance features. How it Works: The structuring element slides over the image, and for each position, if any pixel under the structuring element matches the foreground, the pixel in the output image is set to the foreground. 3. Opening Opening is a compound operation that involves erosion followed by dilation.
Purpose: To remove small objects or noise from the image while preserving the shape and size of larger objects. How it Works: First, the image undergoes erosion, which removes small objects and noise. Then, dilation is applied to restore the size of the remaining objects to their original dimensions. 4. Closing Closing is another compound operation that consists of dilation followed by erosion.
Purpose: To fill small holes and gaps in objects while preserving their overall shape. How it Works: First, dilation is applied to the image, filling small holes and gaps. Then, erosion is applied to restore the original size of the objects. 5. Hit-or-Miss Transform The hit-or-miss transform is used to find specific patterns or shapes in a binary image.
Purpose: To detect specific configurations or shapes in the image. How it Works: It uses a pair of structuring elements: one for the foreground and one for the background. The operation looks for places where the foreground structuring element matches the foreground in the image, and the background structuring element matches the background in the image. 6. Morphological Gradient The morphological gradient is the difference between the dilation and erosion of an image.
Purpose: To highlight the boundaries or edges of objects in the image. How it Works: By subtracting the eroded image from the dilated image, the boundaries of the objects are emphasized. 7. Top-Hat Transform The top-hat transform is used to extract small elements and details from an image.
Purpose: To enhance bright objects on a dark background or vice versa. How it Works: There are two types: white top-hat (original image minus the result of opening) and black top-hat (result of closing minus the original image). 8. Skeletonization Skeletonization reduces objects in a binary image to their skeletal form.
Purpose: To simplify objects to their essential structure while preserving their connectivity. How it Works: It iteratively erodes the image until the objects are reduced to a minimal, skeletal form. 9. Pruning Pruning removes small spurs or extraneous branches from the skeleton of an image.
Purpose: To clean up the skeleton by removing unwanted artifacts. How it Works: It identifies and removes small, irrelevant branches from the skeletonized image. Implementing Morphological Operations in Python
Python
import cv2
import numpy as np
from matplotlib import pyplot as plt
# Load an image
image = cv2 . imread ( '/content/Screenshot-2024-06-11-150857.webp' , 0 ) # Change 'input_image.jpg' to your image file path
# Define a kernel (structuring element)
kernel = np . ones (( 5 , 5 ), np . uint8 )
# Erosion
erosion = cv2 . erode ( image , kernel , iterations = 1 )
# Dilation
dilation = cv2 . dilate ( image , kernel , iterations = 1 )
# Opening (erosion followed by dilation)
opening = cv2 . morphologyEx ( image , cv2 . MORPH_OPEN , kernel )
# Closing (dilation followed by erosion)
closing = cv2 . morphologyEx ( image , cv2 . MORPH_CLOSE , kernel )
# Display the results
titles = [ 'Original Image' , 'Erosion' , 'Dilation' , 'Opening' , 'Closing' ]
images = [ image , erosion , dilation , opening , closing ]
for i in range ( 5 ):
plt . subplot ( 2 , 3 , i + 1 )
plt . imshow ( images [ i ], cmap = 'gray' )
plt . title ( titles [ i ])
plt . xticks ([]), plt . yticks ([])
plt . show ()
Output:
Output of the Morphological Operations for Image Processing Conclusion Morphological operations are powerful tools in image processing, allowing for the manipulation and analysis of shapes and structures within images. By applying these techniques, one can enhance image features, remove noise, and detect specific patterns, making them invaluable in various applications such as medical imaging, computer vision, and pattern recognition.
Similar Reads
Machine Learning Tutorial Machine learning is a branch of Artificial Intelligence that focuses on developing models and algorithms that let computers learn from data without being explicitly programmed for every task. In simple words, ML teaches the systems to think and understand like humans by learning from the data.Machin
5 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
SQL Interview Questions Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970's, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Linear Regression in Machine learning Linear regression is a type of supervised machine-learning algorithm that learns from the labelled datasets and maps the data points with most optimized linear functions which can be used for prediction on new datasets. It assumes that there is a linear relationship between the input and output, mea
15+ min read
Support Vector Machine (SVM) Algorithm Support Vector Machine (SVM) is a supervised machine learning algorithm used for classification and regression tasks. It tries to find the best boundary known as hyperplane that separates different classes in the data. It is useful when you want to do binary classification like spam vs. not spam or
9 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
Top 100 Data Structure and Algorithms DSA Interview Questions Topic-wise DSA has been one of the most popular go-to topics for any interview, be it college placements, software developer roles, or any other technical roles for freshers and experienced to land a decent job. If you are among them, you already know that it is not easy to find the best DSA interview question
3 min read