使用 OpenCV 對影像執行白帽黑帽操作
在本程式中,我們將使用 OpenCV 對影像執行黑帽操作。黑帽變換用於增強亮背景中的黑暗目標。我們將使用形態學 ex(影像,cv2.MORPH_BLACKHAT,核心) 函式。
原影像
演算法
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.
示例程式碼
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)
輸出
廣告