Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use HoughLinesP to detect horizontal lines in OpenCV?

Tags:

opencv

I am currently trying to detect horizontal-like lines in an image using HoughLinesP function in opencv, using these parameters:

HoughLinesP(linMat[i], lines, 1, CV_PI/180, 80, linMat[i].cols*(0.3), 3);

where linMat[i] is the input image. The result is like this

http://postimg.org/image/49b8wzlgz/

While this is OK, what I want is to have lines in horizontal direction, as shown by the yellow line in this image (the line is manually drawn)

http://postimg.org/image/rh9wlueo7/

I've tried to change the CV_PI/180 parameter to smaller value (such as CV_PI/45) and also try to use various values for other parameters, but the horizontal line cannot be generated.

What values shall be used in the parameters to generate such yellow line using HoughLinesP function in opencv?

Thanks!

Update

Following the suggestions (thanks all!) I've tried even very extreme values like

HoughLinesP(linMat[i], lines, 1, CV_PI/360, 80, 1, 1);

nevertheless, the resulted lines have slope in near vertical direction as this : postimg.org/image/y5w6vm7lx/ (please copy-paste the link...). I don't use edge detection filter such as canny since it failed to detect thicker lines, the edges tend to be discontinued lines.

Update 2

Just for clarification, the lines drawn in the result are generated as follows (after the HoughLinesP)

for all lines

{

line( linMat[i], Point(lines[j][0], lines[j][1]), Point(lines[j][2], lines[j][3]), Scalar(0,0,255), 1, 8 );

}

like image 431
Houari Avatar asked Dec 25 '22 13:12

Houari


2 Answers

Simply use the equation

double Angle = atan2(y2 - y1, x2 - x1) * 180.0 / CV_PI;

Check angle for each line detected by HoughLinesP(), and consider the lines which has angle 0 or 180.

like image 156
Haris Avatar answered Dec 28 '22 21:12

Haris


Try applying Hough on edge-output images of canny or any other algorithm than running it on a thresholded image.

enter image description here

It could be seen that higher theta values will only represent the horizontal values. Filter out the results obtained in lines by eliminating values with low theta values.

like image 42
Anoop K. Prabhu Avatar answered Dec 28 '22 19:12

Anoop K. Prabhu