Linking broken edges in images using python

Solution for Linking broken edges in images using python
is Given Below:

I am working on a project to predict contours using unet.

I got the end result as follows: enter image description here

It has got lot of broken edges especially in case of cluttered objects. I tried to dilate the image, but the result is like this, enter image description here The edges got thickened which is not a desired behaviour for my application.

Is there any other way by which I can close the edges and improve the results?