例如我有一组坐标:
10,40; 9,27; 5,68; 7,55; 8,15;
如何在不丢失已排序 Y 轴的正确 X 轴的情况下对这些坐标进行排序。
从上面的示例中,我想对坐标进行排序,因此正确的输出将是:
8,15; 9,27; 10,40; 7,55; 5,68。
任何建议将不胜感激。
谢谢。
std::sort 的文档 http://www.cplusplus.com/reference/algorithm/sort/
#include "opencv2/core/core.hpp"
#include <algorithm> // std::sort
// This defines a binary predicate that,
// taking two values of the same type of those
// contained in the list, returns true if the first
// argument goes before the second argument
struct myclass {
bool operator() (cv::Point pt1, cv::Point pt2) { return (pt1.y < pt2.y);}
} myobject;
int main () {
// input data
std::vector<cv::Point> pts(5);
pts[0] = Point(10,40);
pts[1] = Point(9,27);
pts[2] = Point(5,68);
pts[3] = Point(7,55);
pts[4] = Point(8,15);
// sort vector using myobject as comparator
std::sort(pts.begin(), pts.end(), myobject);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)