2015-12-21 150 views
1

有沒有簡單的解決方案如何將vector<cv::Point2d>轉換爲vector<cv::Point>?像這裏的東西C++ convert vector<int> to vector<double>如何把矢量<cv::Point2d>轉換成矢量<cv::Point>?

此類型模板:

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 
}; 

回答

0

由於沒有從CV轉換:: Point2D從簡歷::點我建議一個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)) ;}) ; 
5

你可以做完全如上所述使用矢量範圍構造器

#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與:

template<typename _Tp> template<typename _Tp2> inline Point_<_Tp>::operator Point_<_Tp2>() const 
{ return Point_<_Tp2>(saturate_cast<_Tp2>(x), saturate_cast<_Tp2>(y)); } 
相關問題