In morphology and digital image processing, top-hat and black-hat transform are operations that are used to extract small elements and details from given images. These two types of transforms in which, the top-hat transform is defined as the difference between the input image and its opening by some structuring element, while the black-hat transform is defined as the difference between the closing and the input image. These transforms are used for various image processing tasks, such as feature extraction, background equalization, image enhancement, and others.
Here we will use Opening and Closing morphological operations.
Difference between Top-Hat and Black-Hat
The top-hat filter is used to enhance bright objects of interest in a dark background. The black-hat operation is used to do the opposite, enhance dark objects of interest in a bright background.
Example 1: Top-Hat Transform
Image used:
# Importing OpenCV import cv2 # Getting the kernel to be used in Top-Hat filterSize =(3, 3) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, filterSize) # Reading the image named 'input.jpg' input_image = cv2.imread("testing.jpg") input_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY) # Applying the Top-Hat operation tophat_img = cv2.morphologyEx(input_image, cv2.MORPH_TOPHAT, kernel) cv2.imshow("original", input_image) cv2.imshow("tophat", tophat_img) cv2.waitKey(5000) |
Output:
As you can observe in the above image, the very small details are enhanced and taken out using the Top-Hat operation. Hence, it is useful in observing the minor details of the inputs when are present as light pixels on a dark background.
Example 2: Black Hat transform
Input Image:

# Importing OpenCV and numpy import cv2 # Defining the kernel to be used in Top-Hat filterSize =(3, 3) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, filterSize) # Reading the image named 'input.jpg' input_image = cv2.imread("testing.jpg") input_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY) # Applying the Black-Hat operation tophat_img = cv2.morphologyEx(input_image, cv2.MORPH_BLACKHAT, kernel) cv2.imshow("original", input_image) cv2.imshow("tophat", tophat_img) cv2.waitKey(5000) |
Output:

Here in this image, all the objects which are white on a dark background are highlighted due to the Black Hat transformation applied to the input image.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- White and black dot detection using OpenCV | Python
- Python code formatting using Black
- Draw Black Spiral Pattern Using Turtle in Python
- Highlight the negative values red and positive values black in Pandas Dataframe
- Python | Pandas Series.transform()
- Python | Pandas DataFrame.transform
- Python PIL | Image.transform() method
- Python | Fast Walsh Hadamard Transform
- Wand transform() function in Python
- Mahotas - Reversing Haar Transform
- Mahotas - Haar Transform
- Mahotas - Hit & Miss transform
- PyQtGraph – Getting Scene Transform Object of Bar Graph
- PyQtGraph – View Transform Change Event for Bar Graph
- Fetching top news using News API
- Top 10 Python IDE and Code Editors in 2020
- Fetch top 10 starred repositories of user on GitHub | Python
- PyQt5 ComboBox - User entered items store at top
- Add a row at top in pandas DataFrame
- Python | Find top K frequent elements from a list of tuples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

