简体   繁体   中英

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?

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

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. Since your points are OpenCV points, compute the distance using OpenCV like this:

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 . In MyPoint define copy constructors and conversation from and to cv::Point , boostPoint . In your code just use YourPoint everywhere. If you need help in implementing this just comment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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