

# https://www.globalsino.com/ICs/
# Fill in closed curves


import cv2
import numpy as np


theImage = cv2.imread(r"C:\GlobalSino2\ICs\images\4367b.jpg", cv2.IMREAD_GRAYSCALE)
# cv2.Canny(image, threshold1, threshold2, apertureSize, L2gradient)
img_canny = cv2.Canny(theImage, 100,200, apertureSize = 3)
img_dilate = cv2.dilate(img_canny, None, iterations=1)
img_erode = cv2.erode(img_dilate, None, iterations=1)

FilledImage = np.full(theImage.shape, 255, "uint8")
contours, hierarchies = cv2.findContours(img_erode, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
for cnt in contours:
    cv2.drawContours(FilledImage, [cnt], -1, 0, -1)

# Invert the contrast of black and white images
FilledImage = (255-FilledImage)
cv2.imshow("Filled image", FilledImage)
cv2.imwrite(r"C:\GlobalSino2\ICs\images\4367c.jpg",FilledImage)
cv2.waitKey(0)
