Performing white TopHat operation on images using OpenCV


In this program, we will perform the TopHat operation on images. TopHat operation is a morphological operation that is used to extract small elements and details from given images. TopHat is used to enhance bright objects in a dark background. We will use the morphologyEx(image, cv2.MORPH_TOPHAT, kernel) function

Original Image

Algorithm

Step 1: Import cv2.
Step 2: Read the image.
Step 3: Define the kernel size.
Step 4: Pass the image and kernel to the cv2.morphologyex() function.
Step 5: Display the output.

Example Code

import cv2

image = cv2.imread('tophat.jpg')
filter_size = (5,5)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, filter_size)
image = cv2.morphologyEx(image, cv2.MORPH_TOPHAT, kernel)
cv2.imshow('TopHat', image)

Output

Explanation

As you can see, the small details are enhanced. TopHat transform is useful in obtaining the minor details from the input image.

Updated on: 17-Mar-2021

393 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements