如何将vector <cv :: point2d>转换为vector <cv :: point>?

时间:2015-12-21 12:27:55

标签: c++ templates opencv vector point

是否有任何简单的解决方案如何将vector<cv::Point2d>转换为vector<cv::Point>?像C++ convert vector<int> to vector<double>这样的东西?

这种类型是模板化的:

typedef Point_<double> Point2d;

typedef Point_<int> Point2i;
typedef Point2i Point;


/*!
  template 2D point class.

  The class defines a point in 2D space. Data type of the point coordinates is specified
  as a template parameter. There are a few shorter aliases available for user convenience.
  See cv::Point, cv::Point2i, cv::Point2f and cv::Point2d.
*/
template<typename _Tp> class Point_
{
public:
    typedef _Tp value_type;

    // various constructors
    Point_();
    Point_(_Tp _x, _Tp _y);
    Point_(const Point_& pt);
    Point_(const CvPoint& pt);
    Point_(const CvPoint2D32f& pt);
    Point_(const Size_<_Tp>& sz);
    Point_(const Vec<_Tp, 2>& v);

    Point_& operator = (const Point_& pt);
    //! conversion to another data type
    template<typename _Tp2> operator Point_<_Tp2>() const;

    //! conversion to the old-style C structures
    operator CvPoint() const;
    operator CvPoint2D32f() const;
    operator Vec<_Tp, 2>() const;

    //! dot product
    _Tp dot(const Point_& pt) const;
    //! dot product computed in double-precision arithmetics
    double ddot(const Point_& pt) const;
    //! cross-product
    double cross(const Point_& pt) const;
    //! checks whether the point is inside the specified rectangle
    bool inside(const Rect_<_Tp>& r) const;

    _Tp x, y; //< the point coordinates
};

1 个答案:

答案 0 :(得分:0)

由于没有从cv :: Point2D转换为cv :: Point,我建议使用lambda(未经测试):

vector<cv::Point2d> src ;
vector<cv::Point> dest ;

std::copy(src.begin(), 
          src.end(), 
          [&dest](const cv::Point2d &item) {
          dest.push_back(cv::Point(pt.x, pt.y)) ;}) ;