使用 OpenCV C++ 在轮廓中查找极值点

Finding extreme points in contours with OpenCV C++

我已经尝试实现这个code,但是当我想按照本教程确定轮廓的最极端点时遇到了麻烦。

# 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])

谁能帮我解决这个问题?

std::vector<cv::Point> 开始,您可以将 std::max_elementstd::min_element 与适当的比较器一起使用,该比较器可在 x 坐标上找到 left right 点,并在 y 坐标上找到 topbottom分:

// 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;
                  });