I have the following image:

Image with gapped contours

and I would like to fill in its contours (i.e. I would like to gap fill the lines in this image).

I have tried a morphological closing, but using a rectangular kernel of size 3x3 with 10 iterations does not fill in the entire border.


1 Answer 1


I managed to find a solution of your problem by simply using a disk-like structuring element instead of a square/rectangular one for the closing operation. A comparison of both approaches' results is shown below.

enter image description here

I am not entirely sure why the disk-like structuring element outperforms the square/rectangular one in this particular case. I will leave it to you to decide whether you will dig into the theory behind it or not. Of course any pointers in the comment section are more than welcome.

Here is the complete code:

import cv2 as cv

img = cv.imread("WhGXN.png")

disk = cv.getStructuringElement(cv.MORPH_ELLIPSE,(13,13))
square = cv.getStructuringElement(cv.MORPH_RECT, (13,13))

disk_img = cv.morphologyEx(img, cv.MORPH_CLOSE, disk)
square_img = cv.morphologyEx(img, cv.MORPH_CLOSE, square)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.