【问题标题】:Finding extreme points in contours with OpenCV C++使用 OpenCV C++ 查找轮廓中的极值点
【发布时间】:2016-08-04 04:05:11
【问题描述】:

我已经尝试实现这个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])

谁能帮我解决这个问题?

【问题讨论】:

标签: c++ image-processing opencv3.0


【解决方案1】:

std::vector<cv::Point> 开始,您可以将std::max_elementstd::min_element 与适当的比较器一起使用,该比较器在x 坐标上查找leftright 点,并在 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;
                  });

【讨论】:

  • extRight 和 extBot 的代码需要颠倒比较(lhs.x > rhs.x 用于 extRight 和 lhs.y > rhs.y 用于 extBot)
  • @bantic 你是对的......实际上这段代码很烂;)我会尽快使用 minmax_element 重写它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-11
  • 2012-11-06
相关资源
最近更新 更多