【问题标题】:Sort points with less STL operator使用较少的 STL 运算符对点进行排序
【发布时间】:2015-03-24 22:31:40
【问题描述】:

我想从 openCV blob 检测中对 110 个关键点进行排序,从上到下,从左到右。但有时会出错。

bool sortRects(const Rect &a, const Rect &b)
{
return ( (a.x + a.y*10) < (b.x + b.y*10) );
}

   vector<Rect> convertedKeyPoints;

   for(int i = 0; i < detectedLedPositions.size(); i++)
   {
   Point2f point(detectedLedPositions[i].pt.x+1.f, detectedLedPositions[i].pt.y + 1.f);
   Rect keyPointToRect(detectedLedPositions[i].pt, point);
   convertedKeyPoints.push_back(keyPointToRect);
   }

   sort(convertedKeyPoints.begin(), convertedKeyPoints.end(), sortRects);

 for(int i = 0; i < convertedKeyPoints.size(); i++)
        {
            QPointF currentPoint(QPoint(convertedKeyPoints[i].tl().x , convertedKeyPoints[i].tl().y));
            ledPosition.push_back(currentPoint);
        }

这是转换为 QPointF 后调试控制台的输出。

QPointF(133, 138)
QPointF(188, 134)
QPointF(240, 134)
QPointF(290, 135)
QPointF(347, 142)
QPointF(454, 137)
QPointF(398, 144)
QPointF(507, 136)
QPointF(27, 189)
QPointF(191, 191)
**QPointF(138, 199)**
QPointF(244, 191)
QPointF(293, 194)
QPointF(345, 189)
QPointF(400, 194)
QPointF(451, 190)
QPointF(505, 192)

那我做错了什么?

【问题讨论】:

  • 看看你有问题的三个点:191 + 191 * 10
  • 为 y 坐标尝试更大的权重,例如乘以 1000 而不是 10。
  • @JonathanPotter 它会一直工作,直到它不起作用。

标签: c++ qt sorting opencv stl


【解决方案1】:

我认为您的排序操作假设 a.x

试试:

bool sortRects(const Rect &a, const Rect &b)
{
  return a.y == b.y ? a.x < b.x : a.y < b.y;
}

这将首先在 Y 轴上排序(从上到下),如果两者完全相等,将回退到在 X 轴上排序(从左到右)。

【讨论】:

  • 唯一需要注意的是浮点精度比较相等性。
  • 谢谢大家,但无论我选择哪一个因素,都会出现一两点排序错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-22
  • 2012-03-09
  • 1970-01-01
  • 2014-05-07
  • 2020-09-11
  • 2016-03-31
相关资源
最近更新 更多