How to fill canny edges that are not fully connected?

Viewed 67

Hi All I have the following image after using canny to detect the edges of crack. However, I was trying to fill up the gap between the edges. However, since the gaps are not fully closed it fill up the whole image. The code I used as shown below:

# Read image as grayscale
img = cv2.imread('Output-Set/Edges.tif', cv2.IMREAD_GRAYSCALE)
hh, ww = img.shape[:2]

# threshold
thresh = cv2.threshold(img, 128, 255, cv2.THRESH_BINARY)[1]

# get the (largest) contour
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
big_contour = max(contours, key=cv2.contourArea)

# draw white filled contour on black background
result = np.zeros_like(img)
cv2.drawContours(result, [big_contour], 0, (255,255,255), cv2.FILLED)
plt.imshow(result)

plt.imshow(result)

# save results
#cv2.imwrite('knife_edge_result.jpg', result)

The Image I am trying to fill is shown below:

Edges Extraction Using Canny

The original image is shown below. I have used canny to extract the edges of the cracks in the original image but needed to fill them up.

Original Image

0 Answers
Related