使用 OpenCV 為影像執行白色禮帽操作
在本程式中,我將對影像執行禮帽操作。禮帽操作是一種形態學操作,用於從給定的影像中提取小元素和細節。禮帽用於在暗背景中增強明亮的物體。我們將使用 morphologyEx(image, cv2.MORPH_TOPHAT, kernel) 函式
原圖
演算法
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('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)
輸出
說明
如你所見,影像的小細節得到增強。禮帽變換可用於從輸入影像中獲取細微的細節。
廣告