mrgloom
mrgloom

Reputation: 21632

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)

Upvotes: 2

Views: 645

Answers (2)

nn0p
nn0p

Reputation: 1209

Make sure you include the necessary header as indicated by the doc:

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

Upvotes: 1

Humam Helfawi
Humam Helfawi

Reputation: 20274

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.

Upvotes: 0

Related Questions