ในโปรแกรมนี้ เราจะทำการกัดเซาะรูปภาพโดยใช้ฟังก์ชัน OpenCV erode() การพังทลายของภาพหมายถึงการย่อขนาดภาพ หากพิกเซลใดๆ ในเคอร์เนลเป็น 0 พิกเซลทั้งหมดในเคอร์เนลจะถูกตั้งค่าเป็น 0 เงื่อนไขหนึ่งก่อนที่จะใช้ฟังก์ชันการกัดเซาะบนรูปภาพคือ รูปภาพควรเป็นรูปภาพระดับสีเทา
ภาพต้นฉบับ
อัลกอริทึม
Step 1: Import cv2 Step 2: Import numpy. Step 3: Read the image using imread(). Step 4: Define the kernel size using numpy ones. Step 5: Pass the image and kernel to the erode function. Step 6: Display the output.
โค้ดตัวอย่าง
import cv2 import numpy as np image = cv2.imread('testimage.jpg') kernel = np.ones((7,7), np.uint8) image = cv2.erode(image, kernel) cv2.imshow('Eroded Image', image)
ผลลัพธ์