# Refining lines in an image with openCV

In my project I am extracting the lines of an image with openCV2 by firstly creating a skeleton: I convert it to a binary image, invert it (to make it work with morphological operations) and then perform a bunch of morphological operations. Then I apply the canny edge detector on the skeleton and perform a Hough transformation.

Now the important part: In order to remove the gaps I perform a closing operation. My problem now is that there are still some gaps and fragments and I can't apply the closing operation one more time because its idempotent (only applicable once).

I want to refine the lines by closing all gaps and possibly thicken the lines. How may I achieve this?

Here some images to illustrate my problem.

### Processed image

See those tiny lines which protrude from the edges? Or the interruptions in a straight line?
For the gaps in the lines I thought about looping through the matrix and if the points left and right of a pixel are 1 (drawn), then that particular pixel should also be set to 1. Would that be a good solution?

If you are trying to remove the gaps during hough transform, you can set minLineLength(Minimum length of line. Line segments shorter than this are rejected) and maxLineGap (Maximum allowed gap between line segments to treat them as single line) in cv2.HoughLinesP()

EDIT

Some results from the morphological operations:

• Actually I want to remove the gaps in the result of the hough transform. I also want the lines to be stronger/thicker, which I can't do during hough transform. – BlackMamba Feb 5 '14 at 16:11
• have you tried increasing the size of structure element to thicken the lines? – lennon310 Feb 5 '14 at 18:22
• Yes, it somehow improves the result but still there are some ugly gaps.<br> For instance: original image: tinypic.com/r/b3va88/8<br> Analysed image: tinypic.com/r/330twf9/8 – BlackMamba Feb 6 '14 at 11:02
• In fact I was wondering why you cannot use the binary image directly for hough line extraction. I uploaded some results on your raw image (i cannot access python currently, I used matlab instead to generate the results) for your reference. Thanks – lennon310 Feb 6 '14 at 15:45
• I'm applying the canny operator before the Houghline transformation because according to the openCV2 cookbook the input for houghlines(p) usually is an edgemap, which one gets from the canny operator. – BlackMamba Feb 6 '14 at 19:46

May be this would help.

1. Get the skeleton image.
2. Find those runs of consecutive foreground pixel in vertical direction in the image whose previous or next column has all background pixel within the run-length. This would give the location of the points, which needs to be processed.
3. For each point find the nearest steep point. This would be either a branch point or a point where an abrupt change in angle has occured. (Example point 15 in the image of previous answer). If there is no such point there is obviously another end-point. Call these as reference point. The vector between an endpoint and reference point would give the direction of extension.
4. Now there can be various ways to decide which point to join with. You can take the nearest foreground point in that direction. You can also pick some features depending on the angle and distance between an endpoint and a point of extension to use in a KNN classifier.

Hope it helps.