Top Hat and Black Hat Transform using OpenCV Python

Top Hat and Black Hat Transforms are morphological operations used for feature extraction and enhancement in image processing. These operations help extract small details from images by analyzing structural differences between the original image and its morphological transformations.

Top Hat Transform extracts bright small elements by computing the difference between the original image and its opening (top hat = image - opening). It highlights small bright features that are smaller than the structuring element.

Black Hat Transform extracts dark small elements by computing the difference between the image's closing and the original image (black hat = closing - image). It highlights small dark features smaller than the structuring element.

OpenCV provides the cv2.morphologyEx() function to perform these transformations directly.

The cv2.morphologyEx() Function

This function performs advanced morphological transformations using erosion and dilation operations.

Syntax

cv2.morphologyEx(src, op, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]])

Parameters

  • src: Source image with any number of channels. Depth should be CV_8U, CV_16U, CV_16S, CV_32F, or CV_64F.

  • op: Type of morphological operation:

    • cv2.MORPH_OPEN - opening operation

    • cv2.MORPH_CLOSE - closing operation

    • cv2.MORPH_GRADIENT - morphological gradient

    • cv2.MORPH_TOPHAT - top hat transform

    • cv2.MORPH_BLACKHAT - black hat transform

  • kernel: Structuring element created using getStructuringElement()

  • anchor: Anchor position within kernel, default (-1,-1) centers the anchor

  • iterations: Number of times to apply the operation

  • borderType and borderValue: Handle boundary pixels

Top Hat Transform

The top hat transform extracts small bright objects by finding the difference between the original image and its opening ?

import cv2
import numpy as np

# Load and convert image to grayscale
img = cv2.imread('/path/to/sample_image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Create rectangular kernel
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (13, 5))

# Apply top hat transform
tophat = cv2.morphologyEx(gray, cv2.MORPH_TOPHAT, kernel)

# Display results
cv2.imshow("Original", gray)
cv2.imshow("Top Hat", tophat)
cv2.waitKey(0)
cv2.destroyAllWindows()

Manual Implementation

You can implement top hat transform manually by performing opening first, then subtracting ?

import cv2
import numpy as np

# Load and convert image
img = cv2.imread('/path/to/sample_image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Create kernel
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (13, 5))

# Manual top hat: original - opening
opening = cv2.morphologyEx(gray, cv2.MORPH_OPEN, kernel)
tophat_manual = cv2.subtract(gray, opening)

cv2.imshow("Manual Top Hat", tophat_manual)
cv2.waitKey(0)
cv2.destroyAllWindows()

Black Hat Transform

The black hat transform extracts small dark objects by finding the difference between the image's closing and the original image ?

import cv2
import numpy as np

# Load and convert image
img = cv2.imread('/path/to/sample_image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Create kernel
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (13, 5))

# Apply black hat transform
blackhat = cv2.morphologyEx(gray, cv2.MORPH_BLACKHAT, kernel)

cv2.imshow("Original", gray)
cv2.imshow("Black Hat", blackhat)
cv2.waitKey(0)
cv2.destroyAllWindows()

Manual Implementation

Manual black hat transform by performing closing first, then subtracting the original ?

import cv2
import numpy as np

# Load and convert image
img = cv2.imread('/path/to/sample_image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Create kernel
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (13, 5))

# Manual black hat: closing - original
closing = cv2.morphologyEx(gray, cv2.MORPH_CLOSE, kernel)
blackhat_manual = cv2.subtract(closing, gray)

cv2.imshow("Manual Black Hat", blackhat_manual)
cv2.waitKey(0)
cv2.destroyAllWindows()

Comparison

Transform Formula Extracts Best For
Top Hat Image - Opening Small bright features Text detection, noise removal
Black Hat Closing - Image Small dark features Shadow detection, dark spots

Conclusion

Top Hat and Black Hat transforms are powerful morphological operations for feature extraction. Top Hat extracts bright details while Black Hat extracts dark details, making them useful for various computer vision applications like text detection and noise analysis.

Updated on: 2026-03-27T06:49:54+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements