Reputation: 1245
I want to find the min and max value from a point vector. The vector consists of x and y element type. I want the min and max of x and the minmax of y. My vector is defined as:
std::vector<cv::Point> location;
findNOZeroInMat(angles,location);
//I tried to use but it gives an error
minMaxLoc(location.cols(0), &minVal_x, &maxVal_x);
minMaxLoc(location.cols(1), &minVal_y, &maxVal_y);
I tried location.x too but it didn't work. How can I get the min and max value of x and y seperately?
Upvotes: 2
Views: 8065
Reputation: 5027
cv::boundingRect
is doing what you want. It
Calculates the up-right bounding rectangle of a point set.
The result is of type cv::Rect
which has attributes x, y, width, heigth
but also provides methods tl
(= top left corner) and br
(= bottom right corner) which can be used to find desired min. and max. values. Note that tl
is inclusive but bt
is exclusive.
std::vector<cv::Point> points;
// do something to fill points...
cv::Rect rect = cv::boundingRect(points);
// tl() directly equals to the desired min. values
cv::Point minVal = rect.tl();
// br() is exclusive so we need to subtract 1 to get the max. values
cv::Point maxVal = rect.br() - cv::Point(1, 1);
Upvotes: 5
Reputation: 227498
You can use std::minmax_element with custom less-than comparison functions/functors:
#include <algorithm>
bool less_by_x(const cv::Point& lhs, const cv::Point& rhs)
{
return lhs.x < rhs.x;
}
Then
auto mmx = std::minmax_element(location.begin(), location.end(), less_by_x);
and similarly for y
. mmx.first
will have an iterator to the minimum element, and mmx.second
to the maximum one.
If you don't have C++11 support for auto
you need to be explicit:
typedef std::vector<cv::Point>::const_iterator PointIt;
std::pair<PointIt, PointIt> mmx = std::minmax_element(location.begin(),
location.end(),
less_by_x);
but note that std::minmax_element
requires C++11 library support.
Upvotes: 9