简体   繁体   English

从std :: vector检索值 <cv::Point> ::为const_iterator

[英]Retrieve value from std::vector<cv::Point>::const_iterator

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. 但是由于我的愚蠢,我不知道如何从mmx检索数据。 Anyone please help me? 有人帮我吗

If i want to access the value of point y from contours, how to do it? 如果我想从轮廓访问y点的值,该怎么做? I really confused with those data types. 我真的对那些数据类型感到困惑。

You can see from minmax_element documentation that it returns a pair of iterators. 您可以从minmax_element文档中看到它返回了一对迭代器。

Given: 鉴于:

vector<Point> pts = ...
auto mmx = std::minmax_element(pts.begin(), pts.end(), less_by_y);

you can access the iterator to the min element with mmx.first , and the iterator to the max element with mmx.second . 您可以使用mmx.first来访问min元素的迭代器,使用mmx.second来访问max元素的迭代器。

If you want to retrieve the min and max y values you need to do: 如果要检索最小和最大y值,则需要执行以下操作:

int min_y = mmx.first->y;
int max_y = mmx.second->y;

Since you are in OpenCV, you can also find the y values using boudingRect : 由于您在OpenCV中,因此还可以使用boudingRect查找y值:

Rect box = boundingRect(pts);
std::cout << "min y: " << box.tl().y << std::endl;
std::cout << "max y: " << box.br().y - 1 << std::endl; // Note the -1!!!

Although this is probably slower, you don't need to define the custom comparison function. 尽管这可能会比较慢,但是您无需定义自定义比较功能。 This computes also min and max x , if needed. 如果需要,这还将计算min和max x


Here a complete example: 这里是一个完整的例子:

#include <opencv2/opencv.hpp>
#include <algorithm>
#include <iostream>
using namespace cv;

bool less_by_y(const cv::Point& lhs, const cv::Point& rhs)
{
    return lhs.y < rhs.y;
}

int main(int argc, char** argv)
{
    // Some points
    vector<Point> pts = {Point(5,5), Point(5,0), Point(3,5), Point(3,7)};

    // Find min and max "y"
    auto mmx = std::minmax_element(pts.begin(), pts.end(), less_by_y);

    // Get the values
    int min_y = mmx.first->y;
    int max_y = mmx.second->y;

    // Get the indices in the vector, if needed
    int idx_min_y = std::distance(pts.begin(), mmx.first);
    int idx_max_y = std::distance(pts.begin(), mmx.second);

    // Show results
    std::cout << "min y: " << min_y << " at index: " << idx_min_y << std::endl;
    std::cout << "max y: " << max_y << " at index: " << idx_max_y << std::endl;

    // Using OpenCV boundingRect

    Rect box = boundingRect(pts);
    std::cout << "min y: " << box.tl().y << std::endl;
    std::cout << "max y: " << box.br().y - 1 << std::endl; // Note the -1!!!

    return 0;
}

From the std::minmax() docs : std::minmax()文档

a pair consisting of an iterator to the smallest element as the first element and an iterator to the greatest element as the second. 一对,由最小元素的迭代器作为第一个元素,至最大元素的迭代器作为第二个元素。 Returns std::make_pair(first, first) if the range is empty. 如果范围为空,则返回std :: make_pair(first,first)。 If several elements are equivalent to the smallest element, the iterator to the first such element is returned. 如果几个元素等于最小的元素,则返回第一个此类元素的迭代器。 If several elements are equivalent to the largest element, the iterator to the last such element is returned. 如果几个元素等于最大元素,则返回最后一个此类元素的迭代器。

So mmx.first is the minimum and mmx.second is the maximum. 因此, mmx.first是最小值, mmx.second是最大值。

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

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