Schließen Sie kleine Löcher im binären Bild mit OpenCV
'''Closing small holes in Opencv can be done using morphological
operation method, cv2.morphologyEx() with cv2.MORPH_CLOSE'''
import cv2
img = cv2.imread("myimage.png")
'''Assuming image "myimage.png" has small holes in it, such as the
example in this Link: https://freesvg.org/june-25-black-white-circles;
the black and whites circles are the foreground and the rest is the
background.'''
kernelSize = (7,7) # as the kernetsize increase more holes are closed.
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, kernelSize)
'''kernelSize is the filter size (must be odd) and cv2.MORPH_RECT
specifies that the shape of the kernel should be a rectangle (other shapes
are available). The getStructuringElement() generate the kernel.'''
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # convert to gray scale
BinryImg = cv2.threshold(gray,20,255,cv2.THRESH_BINARY)[1]# convert to-
#binary image.
closing = cv2.morphologyEx(converToBinaryImg, cv2.MORPH_CLOSE, kernel)
cv2.imshow("outputmyimg",closing)
Coderunner