Performing white BlackHat operation on images using OpenCV


In this program, we will perform the Blackhat operation on an image using OpenCV. BlackHat transform is used to enhance dark objects of interest in a bright background. We will use the morphologyEx(image, cv2.MORPH_BLACKHAT, 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('image_test.jpg')
filter_size = (5,5)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, filter_size)
image = cv2.morphologyEx(image, cv2.MORPH_BLACKHAT, kernel)

cv2.imshow('BlackHat', image)

Output

Updated on: 17-Mar-2021

269 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements