排序从左上角到右下角的坐标 [英] Sort Coordinates from top left to bottom right
问题描述
我使用C ++和OpenCV从10x11 LED阵列的轮廓中提取了110个坐标,并将它们存储在向量中。现在我想从左上到右下对它们进行排序,以检测特定行和列中的LED是否亮起。我通过y位置预定了坐标,以确保向量中前10个坐标代表我的图像中的第一个LED行。
I extracted 110 coordinates from contours of a 10x11 LED-Array using C++ and OpenCV and stored them in a vector. Now I want to sort them from top left to bottom right to detect if a LED in a certain row and column is on. I presorted the coordinates by y-position to make sure that the first 10 coordinates in the vector representing the first LED row in my Image.
vector<Point2f> centers;
bool compareY(Point2f p1, Point2f p2){
if(p1.y < p2.y) return true;
if(p1.y > p2.y) return false;
}
sort(centers.begin(), centers.end(), compareY);
现在我必须通过x位置对它们进行排序。问题是来自第二行或任何其他行中的第一LED的x位置可以比行1中的第一LED小一点。由于这个事实,它们必须从中心[0]到中心[9],中心[10]到中心[20] ...逐行排序。有任何人有想法如何做?
Now I have to sort them by x-position. The problem is that the x-position from the first LED in row two or any other row can be a bit smaller then the first LED in row one. Due to that fact they have to be sorted from centers[0] to centers[9], centers[10] to centers[20]... row by row. Has anybody an idea how to do that?
提前感谢!
BTW LED面板看起来像这样
BTW the LED Panel looks like this
编辑:管理以对点进行排序,但是基于轮廓检测的算法不足以检测所有LED。有没有人想要一个健壮的方法来检测它们?
Managed to sort the points but my algorithm based on contour detection is not robust enough to detect all LEDs. Has anyone an idea for a robust method to detect them?
推荐答案
如果你想通过Y坐标执行词典排序, X坐标,你只需要提供一个合适的比较函数,真正实现严格的弱排序使用。例如
If you want to perform a lexicographical sorting by Y coordinate and then X coordinate, you just have to provide a suitable comparison function that really implements a strict weak ordering using. For example
#include <tuple>
bool compareYX(const Point2f& p1, const Point2f& p2)
{
return std::tie(p1.y, p1.x) < std::tie(p2.y, p2.x);
}
您也可以手动实现词典比较,但这是非常容易出错。
You can also implement the lexicographical comparison manually, but this is quite error-prone.
这篇关于排序从左上角到右下角的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!