venus
venus

Reputation: 87

HoughLines transform in opencv

I am working on image processing using opencv and Eclipse.

  vector<Vec2f> lines;  
  HoughLines(dst, lines, 1, CV_PI/180, 100, 0, 0 );

  for( size_t i = 0; i < lines.size(); i++ )  
  {  
     float rho = lines[i][0], theta = lines[i][1];  
     Point pt1, pt2;  
     double a = cos(theta), b = sin(theta);  
     double x0 = a*rho, y0 = b*rho;  
     pt1.x = cvRound(x0 + 1000*(-b));  
     pt1.y = cvRound(y0 + 1000*(a));  
     pt2.x = cvRound(x0 - 1000*(-b));  
     pt2.y = cvRound(y0 - 1000*(a));  
     line( cdst, pt1, pt2, Scalar(0,0,255), 3, CV_AA);  
  }  

Can anyone explain that how the points are being defined by this code. We are using

y=(-cos(theta)/sin(theta))x + r/(sin(theta))
rho=xo*cos(theta) + yo*sin(theta)

I am not able to understand why the multiplication of 1000 is being done in the line

pt1.x = cvRound(x0 + 1000*(-b));  

please try to explain this in simple terms. Thanks in advance

Upvotes: 3

Views: 7702

Answers (3)

wonderingdev
wonderingdev

Reputation: 1181

Here's a detailed explanation for this piece of code:

 pt1.x = cvRound(x0 + 1000*(-b));  
 pt1.y = cvRound(y0 + 1000*(a));  
 pt2.x = cvRound(x0 - 1000*(-b));  
 pt2.y = cvRound(y0 - 1000*(a));

(click on the picture to see it in full size)

enter image description here

In this case d1 = d2 = 1000.

Upvotes: 3

sietschie
sietschie

Reputation: 7553

The question has already been answered. But since I spend the last fifteen minutes drawing this diagram I might as well post it anyway. Maybe it helps:

enter image description here

So what you have is a Point p0 = (x0,y0) which is on the line. You then compute two other points on the line which are 1000 units away from p0 in each direction.

Upvotes: 17

Mozglubov
Mozglubov

Reputation: 433

The code appears to be trying to draw a line from the parameters returned by the Hough Transform function. The multiplication by 1000 makes it so that your points are moved along the line (in opposite directions, which is why pt1 adds and pt2 subtracts) from the starting position in order to actually draw the line. Different values of that number should give you different line segment lengths. If you are curious, try replacing the value with a variable (like line_length) and then vary the value of that variable to see how it affects the appearance of your output.

Upvotes: 1

Related Questions