Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect broken lines in a binary image using Python/Opencv

How can I make these lines connect at the target points? The image is a result of a skeletonization process.

Resulting Image From Skeletonization

Points That I Need To Connect

I'm trying to segment each line as a region using Watershed Transform.

like image 705
Thiago Resende Avatar asked May 09 '17 01:05

Thiago Resende


People also ask

How do I blend images in OpenCV?

The process of blending s done through the cv2. addWeighted() function from OpenCV. This function uses both the input images, assigns certain weights to each image pixel, adds them together and outputs the result into a new pixel. In that way we will get the us an impression of transparency.

What algorithm is used to detect lines in OpenCV?

The Hough Transform is a method that is used in image processing to detect any shape, if that shape can be represented in mathematical form. It can detect the shape even if it is broken or distorted a little bit.

What does .read do OpenCV?

. read() in OpenCV returns 2 things, boolean and data. If there are not 2 variables, a tuple will be assigned to one variable. The boolean is mostly used for error catching.


2 Answers

MikeE's answer is quite good: using dilation and erosion morphological operations can help a lot in this context.
I want to suggest a little improvement, taking advantage of the specific structure of the image at hand. Instead of using dilation/erosion with a general kernel, I suggest using a horizontal kernel that will connect the endpoints of the horizontal lines, but will not connect adjacent lines to one another.

Here's a sketch of code (assuming the input image is stored in bw numpy 2D array):

import cv2, numpy as np

kernel = np.ones((1,20), np.uint8)  # note this is a horizontal kernel
d_im = cv2.dilate(bw, kernel, iterations=1)
e_im = cv2.erode(d_im, kernel, iterations=1) 

What you get is the dilated image:
enter image description here

Note how the gaps are closed, while maintaining the distinct horizontal lines

And the eroded image:
enter image description here

To remove artifacts created by dilate/erode, I suggest to extract the skeleton again.
If you further apply skeleton morphological operation to the eroded image you can get this result:
enter image description here

Once you have the curves connected you do not need to use watershed segmentation, but rather use connected components to label each curve.

like image 70
Shai Avatar answered Sep 22 '22 12:09

Shai


Since the image is already monochromatic, you can use morphological transformations to close broken lines.

In case you need an example, you can find it in the documentation here: http://docs.opencv.org/2.4/doc/tutorials/imgproc/opening_closing_hats/opening_closing_hats.html#closing

It works by first dilating the white areas in the image and then eroding back by the same amount. Effectively closing any holes in the white areas. More details and examples can be found here: http://docs.opencv.org/2.4/doc/tutorials/imgproc/erosion_dilatation/erosion_dilatation.html

This strategy requires that the gap in the broken line is smaller than the distance between neighboring lines.

It will not work, if the lines cross, or if the lines are too close to each other. However I think it will work quite nicely in your example.

You can also remove the artifacts below the third line using the erode function.

like image 30
micchr Avatar answered Sep 22 '22 12:09

micchr