SoFunction
Updated on 2025-04-14

Implementation of OpenCV image morphology

In the field of image processing, image morphology is a powerful tool for image analysis based on shapes, and is widely used in many aspects such as image segmentation, feature extraction, edge detection, image noise reduction, etc. With OpenCV, a powerful computer vision library, we can easily implement various image morphological operations. This article will explore the basic principles of image morphology in depth, and combine OpenCV code examples to introduce common morphological operations such as corrosion, expansion, open operation, closed operation, gradient operation, top hat operation and black hat operation.

1. Introduction to image morphology

Image morphology is based on mathematical morphology, and changes the shape and structure of objects in the image by performing specific set operations on pixels in the image. Its core operation is to use a small matrix called structural elements (also called cores) to slide on the image, operating on each pixel and its neighborhood, thereby achieving processing of the image. In OpenCV, a wealth of functions and tools are provided to help us perform various image morphological processing.

2. Corrosion (Erosion)

1. Principle

Corrosion is a basic image morphological operation that acts to "shrink" or "refine" objects in an image. Specifically, the corrosion operation uses structural elements as templates to check each pixel in the image. If all pixels covered by the structural element are 1 (for binary images), the center pixel remains the same, otherwise the center pixel is set to 0. In layman's terms, corrosion operations shrink the boundaries of the object into the interior, thereby removing some small noise points and glitches in the image. For example, for a binary image of a white object on a black background, the corrosion operation will cause the area of ​​the white object to be smaller.

2. OpenCV implementation

In OpenCV, the () function is used to implement the corrosion operation. The first parameter of this function is the input image, the second parameter is the structural element, and the third parameter is the number of iterations (optional, default to 1). Here is a simple example to show how to use() for corrosion operations:

import cv2
import numpy as np
import  as plt
# Read the image and convert it to grayscaleimg = ('', 0)
# Create structural elements, here use 5x5 rectangular structural elementskernel = ((5, 5), np.uint8)
# Perform corrosion operationseroded = (img, kernel, iterations = 1)
# Show results(121), (img, cmap = 'gray'), ('Original')
([]), ([])
(122), (eroded, cmap = 'gray'), ('Eroded')
([]), ([])
()

3. Dilation

1. Principle

Expansion is an operation opposite to corrosion, and its function is to "expand" or "bold" objects in an image. The expansion operation also uses structural elements as templates to check each pixel in the image. As long as one of the pixels covered by the structural element is 1 (for binary images), the center pixel is set to 1. The expansion operation can fill small holes in the image, connect broken objects, and expand the area of ​​the object. For example, when detecting object profiles, the expansion operation can help us obtain the object's profile more completely.

2. OpenCV implementation

In OpenCV, the expansion operation is implemented using the() function. The parameters of this function are similar to (). The first parameter is the input image, the second parameter is the structural element, and the third parameter is the number of iterations (optional, default is 1). Here is the sample code for bloating using ():

import cv2
import numpy as np
import  as plt
# Read the image and convert it to grayscaleimg = ('', 0)
# Create structural elements, here use 5x5 rectangular structural elementskernel = ((5, 5), np.uint8)
# Perform expansion operationdilated = (img, kernel, iterations = 1)
# Show results(121), (img, cmap = 'gray'), ('Original')
([]), ([])
(122), (dilated, cmap = 'gray'), ('Dilated')
([]), ([])
()

4. Opening

1. Principle

The operation is composed of two operations: corrosion and expansion. The image is first corroded and then the expansion operation. The on-operation can remove small noise points in the image, smooth out the outline of the object, and disconnect narrow connections without significantly changing the object's area. For binary images containing noise, the on-operation can effectively remove noise and retain the main object structure.

2. OpenCV implementation

In OpenCV, use the() function and specify the operation type to cv2.MORPH_OPEN to implement the operation. The first parameter of this function is the input image, the second parameter is the operation type, and the third parameter is the structural element. The following is an example code to implement the operation:

import cv2
import numpy as np
import  as plt
# Read the image and convert it to grayscaleimg = ('', 0)
# Create structural elements, here use 5x5 rectangular structural elementskernel = ((5, 5), np.uint8)
# Do operationopened = (img, cv2.MORPH_OPEN, kernel)
# Show results(121), (img, cmap = 'gray'), ('Original')
([]), ([])
(122), (opened, cmap = 'gray'), ('Opened')
([]), ([])
()

5. Closed operation (closing)

1. Principle

Closed operation also consists of two operations: corrosion and expansion, but the order is opposite to the opening operation, and the expansion operation is performed first, and then the corrosion operation is performed. Closed operations can fill small holes inside objects, connect adjacent objects, smooth out the outline of objects, while maintaining the overall shape and area of ​​objects. When processing an image of an object containing a hole, the closed operation can effectively fill the hole and make the shape of the object more complete.

2. OpenCV implementation

In OpenCV, use the() function and specify the operation type to cv2.MORPH_CLOSING to implement closed operations. The sample code is as follows:

import cv2
import numpy as np
import  as plt
# Read the image and convert it to grayscaleimg = ('', 0)
# Create structural elements, here use 5x5 rectangular structural elementskernel = ((5, 5), np.uint8)
# Perform closed operationsclosed = (img, cv2.MORPH_CLOSING, kernel)
# Show results(121), (img, cmap = 'gray'), ('Original')
([]), ([])
(122), (closed, cmap = 'gray'), ('Closed')
([]), ([])
()

6. Morphological Gradient

1. Principle

Morphological gradient operations obtain the outline of an object in the image by the difference between expansion and corrosion operations. Specifically, the result of the gradient operation is equal to the expanded image minus the corrosion image. This highlights the edges of the object in the image and makes the outline of the object more obvious. Morphological gradient operations are often used for edge detection and extraction of object shapes.

2. OpenCV implementation

In OpenCV, use the() function and specify the operation type to cv2.MORPH_GRADIENT to implement gradient operations. The sample code is as follows:

import cv2
import numpy as np
import  as plt
# Read the image and convert it to grayscaleimg = ('', 0)
# Create structural elements, here use 5x5 rectangular structural elementskernel = ((5, 5), np.uint8)
# perform gradient operationgradient = (img, cv2.MORPH_GRADIENT, kernel)
# Show results(121), (img, cmap = 'gray'), ('Original')
([]), ([])
(122), (gradient, cmap = 'gray'), ('Gradient')
([]), ([])
()

7. Top hat operation (Top - Hat)

1. Principle

Top hat operation, also known as top hat operation, is the difference between the original image and the image opening operation. That is, top hat image = original image - start operation image. Turning operations will smooth out the object profile and remove small noise, but will also cause lighter parts of the image to lose. The top hat operation can separate patches that are lighter than adjacent to highlight tiny details in the image, and is very useful in extracting bright areas or noise in the image. For example, in text detection, top hat operations can help highlight the details of the text.

2. OpenCV implementation

In OpenCV, top hat operation is implemented by the() function and specifying the operation type as cv2.MORPH_TOPHAT. Here is a code example that implements top hat operation:

import cv2
import numpy as np
import  as plt
# Read the image and convert it to grayscaleimg = ('', 0)
# Create structural elements, here use 5x5 rectangular structural elementskernel = ((5, 5), np.uint8)
# perform top hat operationtophat = (img, cv2.MORPH_TOPHAT, kernel)
# Show results(121), (img, cmap = 'gray'), ('Original')
([]), ([])
(122), (tophat, cmap = 'gray'), ('Top - Hat')
([]), ([])
()

8. Black Hat Operation (Black - Hat)

1. Principle

The black hat operation is opposite to the top hat operation, and the result is the difference between the image closed operation and the original image. That is, black hat image = closed operation image - original image. Closed operations can fill small holes inside objects and connect adjacent objects. Black hat operation helps to separate patches that are darker than adjacent points, highlight relatively darker areas in the image, and is very effective when detecting dark details or background features in the image. For example, when detecting dark objects, black hat operations can help enhance the characteristics of objects.

2. OpenCV implementation

In OpenCV, black hat operation is implemented by the() function and specifying the operation type as cv2.MORPH_BLACKHAT. The code example is as follows:

import cv2
import numpy as np
import  as plt
# Read the image and convert it to grayscaleimg = ('', 0)
# Create structural elements, here use 5x5 rectangular structural elementskernel = ((5, 5), np.uint8)
# perform black hat operationblackhat = (img, cv2.MORPH_BLACKHAT, kernel)
# Show results(121), (img, cmap = 'gray'), ('Original')
([]), ([])
(122), (blackhat, cmap = 'gray'), ('Black - Hat')
([]), ([])
()

9. Summary

This article introduces in detail the image morphology processing techniques in OpenCV, including corrosion, expansion, open operation, closed operation, gradient operation, top hat operation and black hat operation. Each operation has its own unique principles and applicable scenarios. Corrosion and expansion are basic operations. Open operation, closed operation, top hat operation and black hat operation are based on them for different image feature extraction and processing. Gradient operations focus on highlighting the edges of objects. In practical applications, we can flexibly combine these morphological operations based on the characteristics of the image and processing needs to achieve the best processing effect.

This is the end of this article about the implementation of OpenCV image morphology. For more related content on OpenCV image morphology, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!