如何组织或排序std :: vector< cv :: Point2f> [英] how to organize or sort a std::vector <cv::Point2f>

查看:151
本文介绍了如何组织或排序std :: vector< cv :: Point2f>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个充满cv :: Point的向量,并且我想组织这个向量,以便具有最小x和y值的Point应该是第一个,最后一个应该具有最高的x,y值?任何想法我该怎么做?

I have a vector full of cv::Point and I want to organize this vector so, that the Point with the smallest x and y value should be the first and the last one should have the highst x,y value ? any Idea how can I do that ?

推荐答案

使用 std :: sort .

std::sort(vec.begin(), vec.end(), [](const cv::Point2f &a, const cv::Point2f &b) {
    return (/* This is where you would compare a and b however you want */);
});

真的,很难说出您认为最大(x,y)对和最小(x,y)对的含义.一种解决方案是添加坐标以赋予其大小.

Really, it's quite hard to tell what you deem as the greatest (x,y) pair and the least (x,y) pair. One solution is to add the coordinates to give them a magnitude.

我将使用距原点的距离:返回a.x * a.x + a.y * a.y<b.x * b.x + b.y * b.y

I'd use the distance from the origin: return a.x*a.x + a.y*a.y < b.x*b.x + b.y*b.y

如果您无法使用C ++ 11功能,则等同于上述解决方案:

In case you can't use C++11 functionality, here's the equivalent of the above solution:

bool point_comparator(const cv::Point2f &a, const cv::Point2f &b) {
    return (/* Your expression */);
}

std::sort(vec.begin(), vec.end(), point_comparator);

这篇关于如何组织或排序std :: vector&lt; cv :: Point2f&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆