Apply Black Hat Operation to an Image using OpenCV

Apply Black Hat Operation to an Image using OpenCV

Black hat (also known as bottom hat) is a morphological image processing operation. It is the difference between image on which applied closing operation and input image. Black hat operation is commonly used for grayscale images, but it can be used for binary images as well. This operation is useful for isolating pixels that are darker than neighborhood pixels.

The morphologyEx function with MORPH_BLACKHAT parameter can be used to apply the black hat operation to an image.

Python
C++
C++ (CUDA)
Java
import cv2 import numpy as np inputImg = cv2.imread('test.jpg') inputImg = cv2.cvtColor(inputImg, cv2.COLOR_BGR2GRAY) kernel = np.ones((4, 4)) outputImg = cv2.morphologyEx(inputImg, cv2.MORPH_BLACKHAT, kernel) cv2.imshow('Input image', inputImg) cv2.imshow('Output image', outputImg) cv2.waitKey(0) cv2.destroyAllWindows()
Black hat operation applied to an image using OpenCV

Leave a Comment

Cancel reply

Your email address will not be published.