简体   繁体   English

将Rect转换为矢量<Point>

[英]convert Rect to vector<Point>

I am using OpenCV and need to convert a cv::Rect to a std::vector<cv::Point> of all the points in the Rect . 我使用OpenCV的,需要一个转换cv::Rectstd::vector<cv::Point>在所有点的Rect

I wonder if there is a method that can do this? 我想知道是否有一种方法可以做到这一点? If not, how can I populate the std::vector with my points? 如果没有,如何用我的点填充std::vector

I tried adding the points in the constructor, but it fails: 我尝试在构造函数中添加点,但失败:

cv::Rect rect
std::vector<cv::Point> _contour(rect.tl(),rect.br());

the error I'm getting is 我得到的错误是

no instance of constructor "std::vector<_Ty, _Alloc>::vector [with _Ty=cv::Point, _Alloc=std::allocator]" matches the argument list 没有构造函数“ std :: vector <_Ty,_Alloc> :: vector [with _Ty = cv :: Point,_Alloc = std :: allocator]”的实例与参数列表匹配

I tried to add the points using the insert function, but this fails too: 我尝试使用insert函数添加点,但这也失败了:

cv::Rect rect
std::vector<cv::Point> _contour;
_contour.insert(rect.tl());

the error I'm getting is: 我得到的错误是:

no instance of overloaded function "std::vector<_Ty, _Alloc>::insert [with _Ty=cv::Point, _Alloc=std::allocator]" matches the argument list 没有重载函数“ std :: vector <_Ty,_Alloc> :: insert [with _Ty = cv :: Point,_Alloc = std :: allocator]”的实例与参数列表匹配

Thanks 谢谢

As the return type of rect.tl() is a point, you can use an initialization list to construct the vector: 由于rect.tl()的返回类型是一个点,因此可以使用初始化列表构造向量:

std::vector<cv::Point> _contour { rect.tl(), rect.br() };

And as BoBTFish mentioned, you should use push_back instead of insert : 正如BoBTFish所述,您应该使用push_back而不是insert:

std::vector<cv::Point> _contour;
_contour.push_back(rect.tl());

An other way: you know the size of your vector and you change value: 另一种方式:您知道向量的大小,然后更改值:

std::vector<cv::Point> _contour(2);
_contour[0] = rect.tl();
_contour[1] = rect.br();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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