Top Hat and Black Hat Transform using OpenCV Python


Top Hat and Black Hat Transforms are the Morphological transformations operations on the binary images. Generally, the morphological operations process the image based on the morphology, structure, or shape.

Top-hat transform is an operation that opens an image and then subtracts it from the original image (top hat = image - opening). It is used to extract small elements and details from given images which is nothing but feature extraction, image enhancement, and other operations.

Whereas the Black Hat Transform operation is nothing but the difference between an image closing and the input image itself (black hat = closing - image). It closes the image before subtracting it from the original image. The resulting image contains darker and smaller objects than their surroundings and the structuring element.

OpenCV provides a function cv2.morphologyEx(), it directly implements these transform operations on an image.

The cv2.morphologyEx() function

The function performs these advanced morphological transformations using basic erosion and dilation operations.

Syntax

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

Parameters

  • Src: Source image, with any number of channels. The depth should be one of CV_8U, CV_16U, CV_16S, CV_32F, or CV_64F.

  • Op: It specifies which type of morphological operation has to be applied.

    • MORPH_OPEN – an opening operation

    • MORPH_CLOSE – a closing operation

    • MORPH_GRADIENT – a morphological gradient

    • MORPH_TOPHAT: “top hat”

    • MORPH_BLACKHAT: “black hat”

  • Kernel: Structuring element whose origin is defined by the anchor. It can be created using getStructuringElement.

  • Anchor: Anchor position with the kernel, by default (-1,-1). Negative values specify that the anchor is at the kernel centre.

  • Dst: Destination image.

  • Iterations: Specifies the Number of times erosion and dilation are applied.

  • borderType and borderValue: These are useful to pad the image to account for the boundary pixels or if the image is of non-regular shape.

Top-hat transform

Top-hat transform is nothing but difference between the original image and its opening.

Example

Let’s take an image, and do the top hat transform using the cv2.morphologyEx() method.

import cv2
import numpy as np
img = cv2.imread('Images/Dog.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (13, 5))
tophat = cv2.morphologyEx(gray, cv2.MORPH_TOPHAT, Kernel)
cv2.imshow("Original", img)
cv2.imshow("TopHat", tophat)
cv2.waitKey(0)

Input Image

Output Image

Example

In this example, we will implement the Top Hat operation manually by first finding the image opening and then subtracting it from the original image.

import cv2
import numpy as np
img = cv2.imread('Images/Dog.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (13, 5))
opening = cv2.morphologyEx(gray, cv2.MORPH_OPEN, Kernel)
tophat_manual = np.subtract(gray, opening)

cv2.imshow("Original", img)
cv2.imshow("TopHat_manual approach", tophat_manual)
cv2.waitKey(0)

Output

Following are input and output images of the above program –

Black-hat transform

Black-hat transform is nothing but the difference between the closing image and the original image.

Example

Let’s do the black-hat transform operation by specifying the MorphType cv2.MORPH_BLACKHAT parameter to the cv2.morphologyEx() method.

import cv2
import numpy as np
img = cv2.imread('Images/Dog.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (13, 5))
blackhat = cv2.morphologyEx(gray, cv2.MORPH_BLACKHAT, Kernel)
cv2.imshow("Original", img)
cv2.imshow("BlackHat", blackhat)
cv2.waitKey(0)

Input Image

Output Image

Example

In this example, we will implement the Black Hat transform operation manually by subtracting the original image from its closing.

import cv2
import numpy as np
img = cv2.imread('Images/Dog.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (13, 5))
closing = cv2.morphologyEx(gray, cv2.MORPH_CLOSE, Kernel)
blackhat_manual = np.subtract(closing, gray)

cv2.imshow("Original", img)
cv2.imshow("BlackHat_manual approach", blackhat_manual)
cv2.waitKey(0)

Output

Here we have discussed the Top-hat and Black hat Transform operation using the openCV python.

Updated on: 30-May-2023

750 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements