'How to connect broken lines into one continuous line?

I have this picture:

picture

As you can see, some lines are not connected. These lines were drawn using semantic segmentation. What I want is to connect these lines, because I cannot improve my model any more. I was looking through cv2 ways to deal with this but couldn't find a way. Any help will be appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source