本文介绍了使用OpenCV C ++查找轮廓中的极点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已尝试实施此代码,但是在我要确定轮廓的最极端时遇到麻烦.
I have tried to implement this code, but I have a trouble when I want to determine the most extreme point of the contour follow this tutorial.
# determine the most extreme points along the contour
extLeft = tuple(c[c[:, :, 0].argmin()][0])
extRight = tuple(c[c[:, :, 0].argmax()][0])
extTop = tuple(c[c[:, :, 1].argmin()][0])
extBot = tuple(c[c[:, :, 1].argmax()][0])
有人可以帮我解决这个问题吗?
Can anyone help me to solve this problem?
推荐答案
从std::vector<cv::Point>
开始,可以将std::max_element
和std::min_element
与适当的比较器一起使用,该比较器可在x
坐标上工作以找到 left 和 right 点,并在y
坐标上工作以找到 top 和 bottom 点:
Starting from a std::vector<cv::Point>
, you can use std::max_element
and std::min_element
with an appropriate comparator, that works on x
coordinates to find left and right points, and works on y
coordinates to find top and bottom points:
// Your points
vector<Point> pts;
...
Point extLeft = *min_element(pts.begin(), pts.end(),
[](const Point& lhs, const Point& rhs) {
return lhs.x < rhs.x;
});
Point extRight = *max_element(pts.begin(), pts.end(),
[](const Point& lhs, const Point& rhs) {
return lhs.x < rhs.x;
});
Point extTop = *min_element(pts.begin(), pts.end(),
[](const Point& lhs, const Point& rhs) {
return lhs.y < rhs.y;
});
Point extBot = *max_element(pts.begin(), pts.end(),
[](const Point& lhs, const Point& rhs) {
return lhs.y < rhs.y;
});
这篇关于使用OpenCV C ++查找轮廓中的极点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!