Ask Your Question

Improving line extraction from HoughLinesP

asked 2015-03-04 08:55:57 -0500

bvbdort gravatar image

updated 2015-03-04 09:14:45 -0500


I am using HoughLinesP to extract lines from grayscale image.

Below is my psudo code.

Canny(bin_img, canny_img, 50, 255, 3);
vector<Vec4i> tmp_lines;
HoughLinesP(canny_img,tmp_lines, 1, CV_PI/360,10, 10, 1);

As seen in below image single edge (i.e single line in image) is returning more than one line. 16,22 and 0,28 and 10,11 and 23,18,24 lines correspond to single line.

How can i get single line for single edge, any suggestions ?

Input image:

Input image

Extracted lines

image description

Zoomed image

image description


edit retag flag offensive close merge delete


You should increase the last parameter: "maxLineGap – Maximum allowed gap between points on the same line to link them."

kbarni gravatar imagekbarni ( 2015-03-04 09:38:14 -0500 )edit

i tried it and didnt solve, here the issue i guess due to width of the edge

bvbdort gravatar imagebvbdort ( 2015-03-04 11:52:08 -0500 )edit

I see that you are using a large angular accumulator (CV_PI/360=0.5°). That means that if there is a minimal difference between the line directions, they will be considered as separate lines. try to use different values for this parameter ( as your lines are a bit "noisy", you can go up to CV_PI/36 = 5°). It will eventually detect more lines, too.

kbarni gravatar imagekbarni ( 2015-03-05 02:50:59 -0500 )edit

i tried with 1° and 5° but the result was same.

bvbdort gravatar imagebvbdort ( 2015-03-05 04:13:18 -0500 )edit

1 answer

Sort by » oldest newest most voted

answered 2015-03-05 08:29:43 -0500

kbarni gravatar image

updated 2015-03-05 08:32:13 -0500

I checked your code; the first problem is that you already have an edge image. If you run a Canny filter, it will encircle your walls, giving you a lot of false edges. That's why you got generally two lines on both sides of your original line (you can see it on the magnified image).

So use just the thresholded image (just convert it to grayscale):

cvtColor(bin_img, bin_img, COLOR_BGR2GRAY);

Then, you should play with the parameters of the Hough transform. As the image is noisy (the lines aren't straight), increment the rho and theta resolutions. I got quite good results with the following parameters:

HoughLinesP(bin_img,tmp_lines, 2, CV_PI/36, 10, 10, 1);

Here is my result (37 lines detected):


For better results, you must use more sophisticated algorithms (e.g. marked point processes with a linear model - but you won't find these in OpenCV).

edit flag offensive delete link more


thanks for the answer. Yes this result was better. But using 5° is not good for my case. Since i was using line angle for further calculation. Also i can see that the line 38 from a edge because of line. I am using it with 1° and you answer is helpful.

bvbdort gravatar imagebvbdort ( 2015-03-05 15:14:44 -0500 )edit

Question Tools

1 follower


Asked: 2015-03-04 08:55:57 -0500

Seen: 1,460 times

Last updated: Mar 05 '15