使用 OpenCV 對影像執行閉運算
在此程式中,我們將使用 cv2.morphologyEx() 函式執行閉運算。閉運算會消除前景中的小孔,將背景的小孔變為前景。此技術還可以用於在影像中查詢特定形狀。我們將用於此任務的函式為 cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)。
原始影像
演算法
Step 1: Import cv2 and numpy. Step 2: Read the image. Step 3: Define the kernel. Step 4: Pass the image and kernel to the cv2.morphologyex() function. Step 4: Display the output.
示例程式碼
import cv2 import numpy as np image = cv2.imread('testimage.jpg') kernel = np.ones((5,5), np.uint8) image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel) cv2.imshow('Closing', image)
輸出
廣告