amichai
amichai

Reputation: 728

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.

I wonder if there is a method that can do this? If not, how can I populate the std::vector with my points?

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

I tried to add the points using the insert function, but this fails too:

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

Thanks

Upvotes: 0

Views: 4009

Answers (1)

Schneider Lo&#239;c
Schneider Lo&#239;c

Reputation: 38

As the return type of rect.tl() is a point, you can use an initialization list to construct the vector:

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

And as BoBTFish mentioned, you should use push_back instead of 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();

Upvotes: 1

Related Questions