0

この問題が発生しました。写真に輪郭が見つかりました。それらとそのすべてのものを近似します。ここで、Y 軸で並べ替え、次に X 軸で並べ替えます。それは可能ですか?私はこれに std::sort を2回使用していますが、常に1つの軸のみを並べ替えます。できるだけ早く答えてください。ありがとう

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::tieC++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::sort1 回だけです。

于 2013-06-03T17:01:27.873 に答える