ในโปรแกรมนี้ เราจะดำเนินการปิดโดยใช้ฟังก์ชัน cv2.morphologyEx() การปิดจะเป็นการลบรูเล็กๆ ในโฟร์กราวด์ โดยเปลี่ยนรูเล็กๆ ของแบ็คกราวด์ให้เป็นโฟร์กราวด์ เทคนิคนี้สามารถใช้เพื่อค้นหารูปร่างเฉพาะในภาพได้ ฟังก์ชันที่เราจะใช้สำหรับงานนี้คือ cv2.morphologyEx(image, cv2.MORPH_CLOSE, เคอร์เนล)
ภาพต้นฉบับ

อัลกอริทึม
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) ผลลัพธ์
