简体   繁体   English

如何在 opencv cv::Point 中使用 boost::geometry::distance?

[英]How to use boost::geometry::distance with opencv cv::Point?

typedef boost::geometry::model::d2::point_xy<double> boostPoint;

How to use boost::geometry::distance with opencv cv::Point without convertion to boostPoint?如何在不转换为 boostPoint 的情况下将 boost::geometry::distance 与 opencv cv::Point 一起使用?

double EuclideanDistance(const cv::Point2d &pt1, const cv::Point2d &pt2)
{
    boostPoint boostPt1(pt1.x, pt1.y);
    boostPoint boostPt2(pt2.x, pt2.y);

    double distance= boost::geometry::distance(boostPt1, boostPt2);

    return distance;
}

Update:更新:

I tried this code, but it complaines to x error: 'x' has not been declared我试过这段代码,但它抱怨x error: 'x' has not been declared

BOOST_GEOMETRY_REGISTER_POINT_2D(cv::Point2d, double, boost::geometry::cs::cartesian, x, y)

确保包含文档指示的必要标题:

#include <boost/geometry/geometries/register/point.hpp>

If you want to make the code more elegant, just use the proper way.如果你想让代码更优雅,只需使用正确的方法。 Boost is not a advantage if you added to your code.如果您添加到代码中,Boost 就不是优势。 Since your points are OpenCV points, compute the distance using OpenCV like this:由于您的点是 OpenCV 点,因此使用 OpenCV 计算距离,如下所示:

double EuclideanDistance(const cv::Point2d &pt1, const cv::Point2d &pt2)
{
    return cv::norm(pt1-pt2,cv::NORM_L2);
}

EDIT:编辑:

Since the OP need to do it in this way, I may suggest this solution: Create your own Point Class let us call it MyPoint .由于 OP 需要以这种方式执行此操作,我可能会建议此解决方案:创建您自己的 Point Class 让我们将其称为MyPoint In MyPoint define copy constructors and conversation from and to cv::Point , boostPoint .MyPoint定义复制构造函数和对话从和到cv::PointboostPoint In your code just use YourPoint everywhere.在您的代码中,只需在任何地方使用YourPoint If you need help in implementing this just comment.如果您需要帮助来实现这个只是评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM