# Gap Filling Contours / Lines

I have the following image:

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.

edit retag close merge delete

1

Try a 5x5 or 7x7 kernel

( 2015-01-22 06:10:25 -0600 )edit

I tried an 11x11 rectangular kernel and that also did not work.

( 2015-01-22 08:37:37 -0600 )edit

I am sorry, but this is doesnt seem impossible with erosion and dilation. Can you explain why this doesnt work?

( 2015-01-23 08:24:45 -0600 )edit

Sort by ยป oldest newest most voted

Faster method use connected componnet and find contours

1. Find all connected components by using find FindContour method
2. Store contour for every Region(Connected componnet CC)
3. calculate the Bounding Rect for every region (CC)
4. Loop in all region
5. For every region , find the nearest regions depending on the Bounding rect
6. now you have the suspected regions which can be connected to region, compare the two contours and find the mimimum distance between the two contours , if the minimum distance is smaller than threshould (say 10 pixel) then now you can draw line from Point1 in first contour and point2 in second contour , Point1 and Point2 are the points which satisfy the minmum distance calculated before.
7. repeat the proces for all regions

EDIT: you can find sample implemention code here

more

A simple solution could be to try a nearest neighbor approach, which may or may not work but will certainly be very slow. For every white pixel in the image find the nearest white pixels within a short distance range. If found, create a line between the two points. This could be speed up by skipping pixels;

An even simpler solution would be be to blur the image a few times, such that you would have a vague gray blur in the gap area. Then threshold the image such that anything not black becomes white.

more

Could you please elaborate on your nearest neighbour approach? How would you go about creating the algorithm so it was only involving white pixels locating close by white pixels in a binary image, and once the nearesr white pixel was located, how would you ensure the algorithm drew a line between the 2 points

( 2017-07-20 08:26:37 -0600 )edit

I think, that an erosion and dilatation will do the job. And, as I see, you are using binary image, so you can just check if there is a white/black pixel in the window. BUT I'm not sure that will preserve the edges clear and sharp...

more

Official site

GitHub

Wiki

Documentation