Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opencv HoughLines Lines Theta?

I have a question about opencv libary of HoughLines. The format is below from the official website:

C++: void HoughLines(InputArray image, OutputArray lines, double rho, double theta, int threshold, double srn=0, double stn=0 )

And the explanation of output array parameter of lines, it says:

lines – Output vector of lines. Each line is represented by a two-element vector: rho and theta. and theta is 0 for vertical lines and pi/2 for horizontal lines(in radiance).

I'm wondering what's the principle of theta? It seems that theta is in the range of 0~1.57 (since pi/2 is 1.57), but I run the code and find out that theta can exceed 1.57 and become some strange value, like the figure below, red line is the detected line by HoughLines, and sita is the value in parameter lines? enter image description here.

Can anyone tell me what's regularity of theta that output from lines?

Thanks a lot !

like image 334
HenryChen Avatar asked Feb 08 '23 11:02

HenryChen


1 Answers

The docs for OpenCV, the other answer to this question, and wikipedia (as of writing this answer) are wrong. Its the first time in my life I have found a technical error in wikipedia.

cv2.HoughLines gives you pairs (r, theta) that map to lines. Theta is the counter clockwise angle between the x axis (of the image) and the line. It is between zero and pi. Rho is the "distance" from the upper left of the image to the line. As the OP points out, rho is often negative and thus not a proper distance. Here is what the authors of OpenCV meant:

If theta < pi/2, we are talking about a line that goes from the bottom left of the image to the top right. In this case, it cannot cross the x axis below 0, and we know exactly the line to draw (given rho). Up to this point the OpenCV docs are right.

If theta > pi/2, then we need to disambiguate between the two parallel lines that are equidistant from the origin. To deal with this, the authors of HoughLines made a rule: if theta > pi/2, lines that pass through the left side of the image have positive rho and lines that go through the top of the image have negative rho.

like image 128
Charles F Avatar answered Feb 16 '23 04:02

Charles F