从 std::vector<cv::Point>::const_iterator 中检索值
Retrieve value from std::vector<cv::Point>::const_iterator
我从图像中找到了轮廓。我想从轮廓中找到最小点和最小点。
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;
}
我试过这种编码,运行 成功了。但是由于我的愚蠢,我不知道如何从 mmx 中检索数据。有人可以帮帮我吗?
如果我想从轮廓中获取y点的值,怎么办?我真的对那些数据类型感到困惑。
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. 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.
所以mmx.first
是最小值,mmx.second
是最大值。
您可以从 minmax_element 文档中看到它 returns 一对迭代器。
给定:
vector<Point> pts = ...
auto mmx = std::minmax_element(pts.begin(), pts.end(), less_by_y);
您可以使用 mmx.first
访问最小元素的迭代器,使用 mmx.second
.
访问最大元素的迭代器
如果您想检索最小和最大 y
值,您需要执行以下操作:
int min_y = mmx.first->y;
int max_y = mmx.second->y;
由于您在 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!!!
虽然这可能比较慢,但您不需要定义自定义比较函数。如果需要,这还会计算最小值和最大值 x
。
这里是一个完整的例子:
#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;
}
我从图像中找到了轮廓。我想从轮廓中找到最小点和最小点。
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;
}
我试过这种编码,运行 成功了。但是由于我的愚蠢,我不知道如何从 mmx 中检索数据。有人可以帮帮我吗?
如果我想从轮廓中获取y点的值,怎么办?我真的对那些数据类型感到困惑。
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. 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.
所以mmx.first
是最小值,mmx.second
是最大值。
您可以从 minmax_element 文档中看到它 returns 一对迭代器。
给定:
vector<Point> pts = ...
auto mmx = std::minmax_element(pts.begin(), pts.end(), less_by_y);
您可以使用 mmx.first
访问最小元素的迭代器,使用 mmx.second
.
如果您想检索最小和最大 y
值,您需要执行以下操作:
int min_y = mmx.first->y;
int max_y = mmx.second->y;
由于您在 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!!!
虽然这可能比较慢,但您不需要定义自定义比较函数。如果需要,这还会计算最小值和最大值 x
。
这里是一个完整的例子:
#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;
}