I found a contour from an image. I want to find the min point and min point from the contours.
vector<Point> test = contours[0];
auto mmx = std::minmax_element(test.begin(), test.end(), less_by_y);
bool less_by_y(const cv::Point& lhs, const cv::Point& rhs)
{
return lhs.y < rhs.y;
}
I have tried this coding and it run successfully. But due to my stupidness, i do not know how to retrieve data from mmx. Anyone please help me?
If i want to access the value of point y from contours, how to do it? I really confused with those data types.
You can see from minmax_element documentation that it returns a pair of iterators.
Given:
you can access the iterator to the min element with
mmx.first
, and the iterator to the max element withmmx.second
.If you want to retrieve the min and max
y
values you need to do:Since you are in OpenCV, you can also find the
y
values usingboudingRect
:Although this is probably slower, you don't need to define the custom comparison function. This computes also min and max
x
, if needed.Here a complete example:
From the
std::minmax()
docs:So
mmx.first
is the minimum andmmx.second
is the maximum.