2011-09-16 161 views
12

我正在寻找一个帮助函数来计算OpenCV中的两条线的交点。我搜索了API文档,但找不到有用的资源。OpenCV二维线相交帮助函数

OpenCV中是否有基本的几何辅助函数用于线/线段上的相交/距离计算?

回答

38

有在OpenCV的API没有函数来计算线的交点,但距离是:

cv::Point2f start, end; 
double length = cv::norm(end - start); 

如果您需要一段代码,然后在这里计算的直线相交处是:

// 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; 
} 
+0

只是为了澄清。您的示例中的线条是由两个点定义的,还是作为点和方向矢量定义的? – tisch

+0

它们由两点来定义。函数内计算的d1和d2是方向向量。 –

+0

感谢帮手功能! – tisch

1

这是我对EmguCV(C#)的实现。

static PointF GetIntersection(LineSegment2D line1, LineSegment2D line2) 
{ 

    double a1 = (line1.P1.Y - line1.P2.Y)/(double)(line1.P1.X - line1.P2.X); 
    double b1 = line1.P1.Y - a1 * line1.P1.X; 

    double a2 = (line2.P1.Y - line2.P2.Y)/(double)(line2.P1.X - line2.P2.X); 
    double b2 = line2.P1.Y - a2 * line2.P1.X; 

    if (Math.Abs(a1 - a2) < double.Epsilon) 
     throw new InvalidOperationException(); 

    double x = (b2 - b1)/(a1 - a2); 
    double y = a1 * x + b1; 
    return new PointF((float)x, (float)y); 
} 
0

我在Python实现(使用numpy的阵列) 与第1行= [[X1,Y1],[X2,Y2]] & LINE2 = [[X1,Y1],[X2,Y2]]

def getIntersection(line1, line2): 
    s1 = numpy.array(line1[0]) 
    e1 = numpy.array(line1[1]) 

    s2 = numpy.array(line2[0]) 
    e2 = numpy.array(line2[1]) 

    a1 = (s1[1] - e1[1])/(s1[0] - e1[0]) 
    b1 = s1[1] - (a1 * s1[0]) 

    a2 = (s2[1] - e2[1])/(s2[0] - e2[0]) 
    b2 = s2[1] - (a2 * s2[0]) 

    if abs(a1 - a2) < sys.float_info.epsilon: 
     return False 

    x = (b2 - b1)/(a1 - a2) 
    y = a1 * x + b1 
    return (x, y) 
+0

x1,y1&x2,y2必须像float(0.0,1.0)和(2.0,1.0)一样浮动 – afiah

+0

由于(si [0] - ei [0]) –

2

使用齐次坐标,使您的生活更轻松:

cv::Mat intersectionPoint(const cv::Mat& line1, const cv::Mat& line2) 
{ 
    // Assume we receive lines as l=(a,b,c)^T 
    assert(line1.rows == 3 && line1.cols = 1 
      && line2.rows == 3 && line2.cols == 1); 

    // Point is p=(x,y,w)^T 
    cv::Mat point = line1.cross(line2); 

    // Normalize so it is p'=(x',y',1)^T 
    if(point.at<double>(2,0) != 0) 
    point = point * (1.0/point.at<double>(2,0)); 
} 

注意,如果第三个坐标为0的线是平行的,没有解决方案,R 2,但P中^ 2,然后POIN t表示2D中的方向。

+0

“点”的形状是什么?这个函数没有返回任何东西。 – Geoff

1

查找线相交的算法在后How do you detect where two line segments intersect?

以下是我的OpenCV C++实现被描述得非常好。它使用相同的符号,如上面的帖子

bool getIntersectionPoint(Point a1, Point a2, Point b1, Point b2, Point & intPnt){ 
    Point p = a1; 
    Point q = b1; 
    Point r(a2-a1); 
    Point s(b2-b1); 

    if(cross(r,s) == 0) {return false;} 

    double t = cross(q-p,s)/cross(r,s); 

    intPnt = p + t*r; 
    return true; 
} 

double cross(Point v1,Point v2){ 
    return v1.x*v2.y - v1.y*v2.x; 
}