使用 OpenCV 對影像執行開操作
本教程將對影像執行開操作。開操作會刪掉影像前景中的小物體,並將它們置於背景中。這種技術還可以用於在影像中查詢特定的形狀。開操作可以稱為蝕刻後跟膨脹。用於此任務的函式是 cv2.morphologyEx(image, cv2.MORPH_OPEN, 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_OPEN, kernel) cv2.imshow('Opening', image)
輸出
廣告