Ask Your Question

How to find the intersection point of two lines

asked 2013-03-18 05:15:02 -0500

Heshan Sandeepa gravatar image

updated 2018-12-22 03:34:18 -0500

hi, i tried the HoughLines tutorial to detect the lines of a image. Now i want to find the intersection points between detected lines. Is there any in built function in opencv for this. If not can any one suggest a way how to get the line intersections in opencv? (with simple example if possible) plz help.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-03-18 05:55:44 -0500

rics gravatar image

Andrey Kamaev has answered a similar question on Stackoverflow. This is his helper function:

// Finds the intersection of two lines, or returns false.
// The lines are defined by (o1, p1) and (o2, p2).
bool intersection(Point2f o1, Point2f p1, Point2f o2, Point2f p2,
                      Point2f &r)
    Point2f x = o2 - o1;
    Point2f d1 = p1 - o1;
    Point2f d2 = p2 - o2;

    float cross = d1.x*d2.y - d1.y*d2.x;
    if (abs(cross) < /*EPS*/1e-8)
        return false;

    double t1 = (x.x * d2.y - x.y * d2.x)/cross;
    r = o1 + d1 * t1;
    return true;
edit flag offensive delete link more


It would be good to use std::abs or fabs or the epsilon check will fail if not using namespace std.

Lucas Walter gravatar imageLucas Walter ( 2014-02-12 15:10:34 -0500 )edit
sturkmen gravatar imagesturkmen ( 2016-01-16 13:42:09 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2013-03-18 05:15:02 -0500

Seen: 20,003 times

Last updated: Mar 18 '13