OpenCV Image Morphological Operations
Morphological image operations are an important technique in image processing, mainly used for processing binary images (i.e., black-and-white images).
Morphological image operations in OpenCV are important tools in image processing. Through operations such as erosion, dilation, opening, closing, and morphological gradient, effects such as noise removal, object separation, and edge detection can be achieved. Mastering these operations helps to better process and analyze image data.
The following are commonly used morphological operations and their functions in OpenCV:
| Operation | function | Description | Application scenarios |
|---|---|---|---|
| Erosion | cv2.erode() |
Scan the image with a structuring element; if the area covered by the structuring element is all foreground, the center pixel is retained. | Remove noise, separate objects. |
| Dilation | cv2.dilate() |
Scan the image with a structuring element; if there is foreground in the area covered by the structuring element, the center pixel is retained. | Connect broken objects, fill holes. |
| Opening | cv2.morphologyEx() |
Erosion first, then dilation. | Remove small objects, smooth object boundaries. |
| Closing | cv2.morphologyEx() |
Dilation first, then erosion. | Fill small holes, connect neighboring objects. |
| Morphological Gradient | cv2.morphologyEx() |
Dilated image minus eroded image. | Extract object edges. |
| Top-hat operation | cv2.morphologyEx() |
Original image minus the opening result. | Extract small objects that are brighter than the background. |
| Black-hat operation | cv2.morphologyEx() |
Closing result minus the original image. | Extract small objects that are darker than the background. |
1. Erosion (cv2.erode())
Erosion is a process that shrinks foreground objects in an image.
Erosion works by convolving the structuring element with the image; the center pixel is retained only when the structuring element completely covers the foreground pixels in the image, otherwise it is eroded away.
Function prototype
cv2.erode(src, kernel, iterations=1)
src: input image, usually a binary image.kernel: structuring element, can be customized or generated usingcv2.getStructuringElement()to generate.iterations: number of erosion iterations, default is 1.
Example
import
cv2
import
numpy
as
np
Read an image
image
=
cv2.
imread
(
'image.png'
,
0
)
Define the structuring element
kernel
=
np.
ones
(
(
5
,
5
)
,
np.
uint8
)
Erosion operation
eroded_image
=
cv2.
erode
(
image
,
kernel
,
iterations
=
1
)
Display Results
cv2.
imshow
(
'Eroded Image'
,
eroded_image
)
cv2.
waitKey
(
0
)
cv2.
destroyAllWindows
(
)
Erosion makes foreground objects in the image smaller and erodes away edges; it is commonly used to remove noise or separate connected objects.
2. Dilation (cv2.dilate())
Dilation is the opposite of erosion; it is a process that enlarges foreground objects in an image.
Dilation works by convolving the structuring element with the image; as long as the structuring element overlaps with foreground pixels in the image, the center pixel is retained.
Function prototype
cv2.dilate(src, kernel, iterations=1)
src: input image, usually a binary image.kernel: structuring element, can be customized or generated usingcv2.getStructuringElement()to generate.iterations: number of dilation iterations, default is 1.
Example
import
cv2
import
numpy
as
np
Read an image
image
=
cv2.
imread
(
'image.png'
,
0
)
Define the structuring element
kernel
=
np.
ones
(
(
5
,
5
)
,
np.
uint8
)
Dilation operation
dilated_image
=
cv2.
dilate
(
image
,
kernel
,
iterations
=
1
)
Display Results
cv2.
imshow
(
'Dilated Image'
,
dilated_image
)
cv2.
waitKey
(
0
)
cv2.
destroyAllWindows
(
)
Dilation makes foreground objects in the image larger and expands edges; it is commonly used to fill holes in foreground objects or connect broken objects.
3. Opening (cv2.morphologyEx() with cv2.MORPH_OPEN)
Opening is a combined operation of erosion followed by dilation.
Opening is mainly used to remove small noise in an image or separate connected objects.
Function prototype
cv2.morphologyEx(src, op, kernel)
src: input image, usually a binary image.op: morphological operation type; opening usescv2.MORPH_OPEN。kernel: structuring element, can be customized or generated usingcv2.getStructuringElement()to generate.
Example
import
cv2
import
numpy
as
np
Read an image
image
=
cv2.
imread
(
'image.png'
,
0
)
Define the structuring element
kernel
=
np.
ones
(
(
5
,
5
)
,
np.
uint8
)
Opening
opened_image
=
cv2.
morphologyEx
(
image
,
cv2.
MORPH_OPEN
,
kernel
)
Display Results
cv2.
imshow
(
'Opened Image'
,
opened_image
)
cv2.
waitKey
(
0
)
cv2.
destroyAllWindows
(
)
Opening can remove small noise in an image while preserving the main foreground objects.
4. Closing (cv2.morphologyEx() with cv2.MORPH_CLOSE)
Closing is a combined operation of dilation followed by erosion.
Closing is mainly used to fill small holes in foreground objects or connect broken objects.
Function prototype
cv2.morphologyEx(src, op, kernel)
src: input image, usually a binary image.op: morphological operation type; closing usescv2.MORPH_CLOSE。kernel: structuring element, can be customized or generated usingcv2.getStructuringElement()to generate.
Example
import
cv2
import
numpy
as
np
Read an image
image
=
cv2.
imread
(
'image.png'
,
0
)
Define the structuring element
kernel
=
np.
ones
(
(
5
,
5
)
,
np.
uint8
)
Closing
closed_image
=
cv2.
morphologyEx
(
image
,
cv2.
MORPH_CLOSE
,
kernel
)
Display Results
cv2.
imshow
(
'Closed Image'
,
closed_image
)
cv2.
waitKey
(
0
)
cv2.
destroyAllWindows
(
)
Closing can fill small holes in foreground objects while preserving the main foreground objects.
5. Morphological Gradient (cv2.morphologyEx() with cv2.MORPH_GRADIENT)
The morphological gradient is the difference between the dilated image and the eroded image.
The morphological gradient is mainly used to extract the edges of foreground objects in an image.
Function prototype
cv2.morphologyEx(src, op, kernel)
src: input image, usually a binary image.op: morphological operation type; morphological gradient usescv2.MORPH_GRADIENT。kernel: structuring element, can be customized or generated usingcv2.getStructuringElement()to generate.
Example
import
cv2
import
numpy
as
np
Read an image
image
=
cv2.
imread
(
'image.png'
,
0
)
Define the structuring element
kernel
=
np.
ones
(
(
5
,
5
)
,
np.
uint8
)
Morphological Gradient
gradient_image
=
cv2.
morphologyEx
(
image
,
cv2.
MORPH_GRADIENT
,
kernel
)
Display Results
cv2.
imshow
(
'Gradient Image'
,
gradient_image
)
cv2.
waitKey
(
0
)
cv2.
destroyAllWindows
(
)
The morphological gradient can extract the edges of foreground objects in an image and is commonly used for edge detection.