OpenCV Image Morphology Operations

Image morphological operations are an important technique in image processing, mainly used for processing binary images (i.e., black-and-white images).

Image morphological operations in OpenCV are important tools in image processing. Through operations such as erosion, dilation, opening, closing, and morphological gradient, effects like 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 in OpenCV and their functions:

OperationFunctionDescriptionApplication Scenario
Erosioncv2.erode()Use a structuring element to scan the image. If the area covered by the structuring element is entirely foreground, the center pixel is retained.Remove noise, separate objects.
Dilationcv2.dilate()Use a structuring element to scan the image. If the area covered by the structuring element contains foreground, the center pixel is retained.Connect broken objects, fill holes.
Opening Operationcv2.morphologyEx()Erosion followed by dilation.Remove small objects, smooth object boundaries.
Closing Operationcv2.morphologyEx()Dilation followed by erosion.Fill small holes, connect adjacent objects.
Morphological Gradientcv2.morphologyEx()Dilation image minus erosion image.Extract object edges.
Top Hat Operationcv2.morphologyEx()Original image minus the opening result.Extract small objects brighter than the background.
Black Hat Operationcv2.morphologyEx()Closing result minus the original image.Extract small objects darker than the background.

1. Erosion (cv2.erode())

Erosion is a process that shrinks foreground objects in an image.

The erosion operation convolves the structuring element with the image. Only when the structuring element completely covers the foreground pixels in the image is the center pixel retained; 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 usedcv2.getStructuringElement()Generated.
  • iterations: Number of erosion operations, default is 1.

Example

import cv2
import numpy as np

# Read image
image = cv2.imread('image.png', 0)

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

# Erosion operation
eroded_image = cv2.erode(image, kernel, iterations=1)

# Display result
cv2.imshow('Eroded Image', eroded_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

The erosion operation makes foreground objects in the image smaller and erodes away edges. It is often used to remove noise or separate connected objects.

2. Dilation (cv2.dilate())

Dilation is the opposite of erosion; it is a process that expands foreground objects in an image.

The dilation operation convolves 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 usedcv2.getStructuringElement()Generated.
  • iterations: Number of dilation operations, default is 1.

Example

import cv2
import numpy as np

# Read image
image = cv2.imread('image.png', 0)

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

# Dilation operation
dilated_image = cv2.dilate(image, kernel, iterations=1)

# Display result
cv2.imshow('Dilated Image', dilated_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

The dilation operation makes foreground objects in the image larger and expands edges. It is often used to fill holes in foreground objects or connect broken objects.

3. Opening Operation (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 usedcv2.getStructuringElement()Generated.

Example

import cv2
import numpy as np

# Read image
image = cv2.imread('image.png', 0)

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

# Opening operation
opened_image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)

# Display result
cv2.imshow('Opened Image', opened_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Opening can remove small noise in an image while retaining the main foreground objects.

4. Closing Operation (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 usedcv2.getStructuringElement()Generated.

Example

import cv2
import numpy as np

# Read image
image = cv2.imread('image.png', 0)

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

# Closing operation
closed_image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)

# Display result
cv2.imshow('Closed Image', closed_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Closing can fill small holes in foreground objects while retaining 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 usedcv2.getStructuringElement()Generated.

Example

import cv2
import numpy as np

# Read image
image = cv2.imread('image.png', 0)

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

# Morphological gradient
gradient_image = cv2.morphologyEx(image, cv2.MORPH_GRADIENT, kernel)

# Display result
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 often used for edge detection.

Other Extensions