Ask Your Question

how to find coordinates of intersection of lines after using "houghlines"

asked 2016-04-07 05:04:23 -0600

arash gravatar image

updated 2016-04-07 06:15:02 -0600

kbarni gravatar image

Hello dears,

image description

As you can see,i would like to find the coordinates of lines,but unfortunately i could not do it. i used this code for drawing the lines:

Canny(result , can , 50 , 200 , 3);
vector<Vec2f> lines;
HoughLines(can,lines ,1,CV_PI/180,75,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( can, pt1, pt2, Scalar(255), 2, CV_AA);

in continue, i do not know how i can find the coordinate.Please help me,thank you.

Best, Arash

edit retag flag offensive close merge delete


take a look at this tutorial (it seems that the site is down web.archive link )

sturkmen gravatar imagesturkmen ( 2016-04-07 10:41:39 -0600 )edit

1 answer

Sort by » oldest newest most voted

answered 2016-04-07 06:36:57 -0600

kbarni gravatar image

updated 2016-04-07 06:38:55 -0600

Let's write the equation of the line y=ax+b. Then a1=1/atan(theta1), a2=1/atan(theta2), b1=rho1 and b2=rho2 (where theta1,rho1 and theta2,rho2 are the parameters of line1 and line2).

The intersection of line1 and line2 will be: x=(b2-b1)/(a1-a2) and y=(a1b2-a2b1)/(a1-a2).

[Edit] More information on line-line intersection and solution for the vectorial format here.

edit flag offensive delete link more


BTW, to check that line1 and line2 actually intersect, verify that the angle between them (theta1-theta2) is high enough (around 90° or 270°)

kbarni gravatar imagekbarni ( 2016-04-07 06:41:58 -0600 )edit

thank you for fast answering,but there is a critical problem and as you can see from the picture there are 7 lines not four lines.Hence, i hanged on it. i just need to know the exact coordinates of largest polygon with each method that you think its fast and easier to do. Indeed, i need the coordinates for going on this code. Thank you.

 Point2f   inputQuad[4];  
 Point2f   outputQaud[4];

outputQaud[0] = Point2f( 0,0 );
    outputQaud[1] = Point2f( cropped.cols,-10);
    outputQaud[2] = Point2f( cropped.cols,cropped.rows);
    outputQaud[3] = Point2f( 0,cropped.rows  );

lambda = getPerspectiveTransform(inputQuad, outputQaud );

     warpPerspective(cropped,output,lambda,output.size() );
arash gravatar imagearash ( 2016-04-07 10:40:35 -0600 )edit

To answer directly your question, I use the following procedure to get the 4 points around an object:

  1. get the center of the object (using image moments: xc=mom.m01/mom.m00; yc=mom.m10/mom.m00;
  2. recalculate rho/theta for every line according to this point
  3. create a vector of lines (rho/theta pairs). Add the first line.
  4. continue adding lines to the vector. Add a line only if the theta of the current line is different of all the thetas in the vector (abs(theta(current)-theta(j))>45° for every j). Stop once you have 4 lines in your vector.
  5. Sort the vector according to theta.
  6. Get the intersection of these lines using the procedure from my previous post. The intersection of line(4) and line(1) will give the top-left corner, line(1) and line(2) the top right corner and so on.
kbarni gravatar imagekbarni ( 2016-04-08 03:01:37 -0600 )edit

However there is a simpler solution: use contour detection and the enclosing convex hull to get the polygon that approximates the best your object. Tutorial here

Please tell me which solution helped you so I can update my answer.

kbarni gravatar imagekbarni ( 2016-04-08 03:03:48 -0600 )edit

thank you for your answers, to be quiet honest , i am new one in opencv and image processing. i would like to "wrapaffine" the following picture which i have attached as same as the "card image"

i think the first solution is better for me is it possible for you describe it more ? Thank you.

arash gravatar imagearash ( 2016-04-08 08:51:21 -0600 )edit

I propose to try the second solution, it's much easier to implement. If it doesn't work, you can still try the first one.

kbarni gravatar imagekbarni ( 2016-04-08 09:36:58 -0600 )edit

actually my main question is obtaining the coordinates from the contours,as you told me i used the second one,but unfortunately i do not know how can i solve my problem,i really appreciate. is it possible bring me one example according solution one?

arash gravatar imagearash ( 2016-04-08 11:54:27 -0600 )edit

Question Tools

1 follower


Asked: 2016-04-07 05:04:23 -0600

Seen: 12,498 times

Last updated: Apr 07 '16