Finding extreme points in contours with OpenCV C++

♀尐吖头ヾ 提交于 2019-12-12 14:15:01

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/36591981/finding-extreme-points-in-contours-with-opencv-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!