首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将vector<cv::Point2d>转换为vector<cv::Point>?

如何将vector<cv::Point2d>转换为vector<cv::Point>?
EN

Stack Overflow用户
提问于 2015-12-21 20:27:55
回答 2查看 9.2K关注 0票数 3

如何将vector<cv::Point2d>转换为vector<cv::Point>有什么简单的解决方案吗?像这样的东西,C++ convert vector to vector

这些类型是模板化的:

代码语言:javascript
复制
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
};
EN

回答 2

Stack Overflow用户

发布于 2015-12-21 20:50:34

您可以使用向量范围构造函数完全按照此处所述进行操作

代码语言:javascript
复制
#include <opencv2\opencv.hpp>
#include <vector>
using namespace cv;
using namespace std;

int main()
{
    vector<Point2d> vd{ { 1.1, 2.2 }, { 3.3, 4.4 }, {5.5, 6.6} };
    vector<Point> v(vd.begin(), vd.end());

    // Print for debug
    copy(vd.begin(), vd.end(), ostream_iterator<Point2d>(cout, " "));
    cout << endl;
    copy(v.begin(), v.end(), ostream_iterator<Point>(cout, " "));

    return 0;
}

这是可行的,因为您可以使用以下命令从Point2d构建Point

代码语言:javascript
复制
template<typename _Tp> template<typename _Tp2> inline Point_<_Tp>::operator Point_<_Tp2>() const
{ return Point_<_Tp2>(saturate_cast<_Tp2>(x), saturate_cast<_Tp2>(y)); }
票数 8
EN

Stack Overflow用户

发布于 2015-12-21 20:40:27

由于不存在从cv::Point2D到cv::Point的转换,我建议使用lambda (未测试):

代码语言:javascript
复制
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)) ;}) ;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34395705

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档