OpenCV 2d线相交辅助功能 [英] OpenCV 2d line intersection helper function

查看:83
本文介绍了OpenCV 2d线相交辅助功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一个辅助函数来计算OpenCV中两条线的交点.我已经搜索了API文档,但是找不到有用的资源.

I was looking for a helper function to calculate the intersection of two lines in OpenCV. I have searched the API Documentation, but couldn't find a useful resource.

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

Are there basic geometric helper functions for intersection/distance calculations on lines/line segments in OpenCV?

推荐答案

OpenCV API中没有用于计算线相交的函数,但距离为:

There are no function in OpenCV API to calculate lines intersection, but distance is:

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

如果您需要一段代码来计算线的交点,则为:

If you need a piece of code to calculate line intersections then here it is:

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

这篇关于OpenCV 2d线相交辅助功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆