c++ - Finding min and max of a point vector -
i want find min , max value point vector. vector consists of x , y element type. want min , max of x , minmax of y. vector defined as:
std::vector<cv::point> location; findnozeroinmat(angles,location); //i tried use gives error minmaxloc(location.cols(0), &minval_x, &maxval_x); minmaxloc(location.cols(1), &minval_y, &maxval_y);
i tried location.x didn't work. how can min , max value of x , y seperately?
you can use std::minmax_element 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 y
. mmx.first
have iterator minimum element, , mmx.second
maximum one.
if don't have c++11 support auto
need 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 std::minmax_element
requires c++11 library support.
Comments
Post a Comment