Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explanation of rho and theta parameters in HoughLines

Can you give me a quick definition of rho and theta parameters in OpenCV's HoughLines function

void cv::HoughLines (   InputArray  image,
    OutputArray     lines,
    double  rho,
    double  theta,
    int     threshold,
    double  srn = 0,
    double  stn = 0,
    double  min_theta = 0,
    double  max_theta = CV_PI 
)

The only thing I found in the doc is:

rho: Distance resolution of the accumulator in pixels.

theta: Angle resolution of the accumulator in radians.

Do this mean that if I set rho=2 then 1/2 of my image's pixels will be ignored ... a kind of stride=2 ?

like image 264
Ghilas BELHADJ Avatar asked Nov 10 '16 15:11

Ghilas BELHADJ


People also ask

What is rho and theta in HoughLines?

rho: Distance resolution of the accumulator in pixels. theta: Angle resolution of the accumulator in radians. Do this mean that if I set rho=2 then 1/2 of my image's pixels will be ignored ... a kind of stride=2 ? opencv image-processing hough-transform.

What is rho in HoughLines?

Rho — Rho value M is the number of specified lines. Each element that represents the distance of the corresponding line from the top-left coordinate origin of the image at the angle specified by the corresponding element of Theta.

What is threshold in HoughLinesP?

So, threshold means in the accumulator how many votes needed to consider this (rho, theta) as a line, this is the easy one.

What does Hough lines return?

HoughLines returns lines in Polar coordinate system. Converting them to Cartesian coordinate system helps in calculating points on the line and point of intersection between two lines. The Cartesian line equations can be used to find points on the edge of the image that lie on the line detected by cv2.


2 Answers

I have searched for this for hours and still haven't found a place where it is neatly explained. But picking up the pieces, I think I got it.

The algorithm goes over every edge pixel (result of Canny, for example) and calculates ρ using the equation ρ = x * cosθ + y * sinθ, for many values of θ.

The actual step of θ is defined by the function parameter, so if you use the usual math.pi / 180.0 value of theta, the algorithm will compute ρ 180 times in total for just one edge pixel in the image. If you would use a larger theta, there would be fewer calculations, fewer accumulator columns/buckets and therefore fewer lines found.

The other parameter ρ defines how "fat" a row of the accumulator is. With a value of 1, you are saying that you want the number of accumulator rows to be equal to the biggest ρ possible, which is the diagonal of the image you're processing. So if for some two values of θ you get close values for ρ, they will still go into separate accumulator buckets because you are going for precision. For a larger value of the parameter rho, those two values might end up in the same bucket, which will ultimately give you more lines because more buckets will have a large vote count and therefore exceed the threshold.

Some helpful resources:

http://docs.opencv.org/3.1.0/d6/d10/tutorial_py_houghlines.html

https://www.mathworks.com/help/vision/ref/houghtransform.html

https://www.youtube.com/watch?v=2oGYGXJfjzw

like image 60
mj3c Avatar answered Oct 30 '22 07:10

mj3c


To detect lines with Hough Transform, the best way is to represents lines with an equation of two parameters rho and theta as shown on this image. The equation is the following :

x cos⁡(θ)+y sin⁡(θ)=ρ

where (x,y) are line parameters.

This writing in (θ,ρ) parameters allow the detection to be less position-depending than a writing as y=a*x+b

(θ,ρ) in this context give the discretization for these two parameters

Hough explanation

like image 25
RaymoAisla Avatar answered Oct 30 '22 06:10

RaymoAisla