0

我遇到了这个问题。我在我的照片中找到了轮廓。近似它们和所有这些东西。现在,我想先按 Y 轴排序,然后按 X 轴排序。这可能吗?我为此使用了两次 std::sort ,并且我总是只对一个轴进行排序。请尽快回答。谢谢

bool operator()(vector<Point> c1,vector<Point>c2){
    double a=boundingRect( Mat(c1)).y;
    double b=boundingRect( Mat(c2)).y;
    return a<b;
    }

这是 Y 轴的示例。对 X 轴使用相同的 (y=>x)。

4

2 回答 2

3

也许您的老师希望您先按 Y 对它们进行排序,如果 Y 相同,则按 X 排序。如果是这种情况,您应该将比较器编写为

bool operator()(const vector<Point>& c1, const vector<Point>& c2) {
  // get bounding boxes for c1 and c2
  cv::Rect bbox1 = boundingRect(cv::Mat(c1));
  cv::Rect bbox2 = boundingRect(cv::Mat(c2));
  // if c1's top is lower than c2's bottom, order should be c2 c1
  if (c1.y > c2.y+c2.height-1) {
    return false;
  }
  // if c2's top is lower than c1's bottom, order should be c1 c2
  if (c2.y > c1.y+c1.height-1) {
    return true; 
  }
  // they have overlap in y direction, just sort them by their x
  return c1.x < c2.x;
}
于 2013-06-03T16:48:14.517 回答
0

std::tie如果您可以访问 C++11 功能,则可以使用 最简洁地完成此操作。

bool operator()(vector<Point> c1,vector<Point>c2)
{
    double a=boundingRect( Mat(c1)).y;
    double b=boundingRect( Mat(c2)).y;
    return std::tie(a.y, a.x) < std::tie(b.y, b.x);
}

然后,您只需调用std::sort一次。

于 2013-06-03T17:01:27.873 回答