首页 文章

使用OpenCV C在轮廓中找到极 endpoints

提问于
浏览
3

我试图实现这个code,但是当我想确定轮廓的最极 endpoints 时,我遇到了麻烦 .

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

任何人都可以帮我解决这个问题吗?

1 回答

  • 3

    std::vector<cv::Point> 开始,您可以使用 std::max_elementstd::min_element 使用适当的比较器,它可以在 x 坐标上查找左右点,并在 y 坐标上查找顶部和底部点:

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

相关问题