计算 cv::P oints2f 向量中的重复项

Counting duplicates in a vector of cv::Points2f

本文关键字:向量 cv oints2f 计算      更新时间:2023-10-16

Im 试图在向量中查找和计数重复的 cv::P oints2f。为此,我尝试使用以下函数。但是当我尝试使用取消引用的值时出现错误rv[*val]++.

std::map<cv::Point2f, unsigned int> counter(const std::vector<Point2f>& vals) 
{    
std::map<Point2f, unsigned int> rv;
for (auto val = vals.begin(); val != vals.end(); ++val) {
rv[*val]++;
}
return rv;
}  

最后,我想使用一个包含键列表(没有重复键(的容器,以及在原始向量中发现每个键的次数。

例如,对于以下向量

vector<Point2f> v{Point2f(2,2),Point2f(3,3),Point2f(1,2),Point2f(2,2),Point2f(3,3)};

我想获取一个包含此信息的容器: (1,2) 1; (2,2) 2; (3,3( 2

编辑:

只是为了澄清,我得到了不同的注释和错误,如:

/usr/include/c++/5/bits/stl_function.h:387:20: error: no match for ‘operator<’ (operand types are ‘const cv::Point_<float>’ and ‘const cv::Point_<float>’)
{ return __x < __y; }
/usr/local/include/opencv2/core/cvstd.hpp:1031:20: note:   no known conversion for argument 1 from ‘const cv::Point_<float>’ to ‘const cv::String&’

提前非常感谢!

显然,您缺少 Point 类的比较运算符,因此您需要提供它,例如:

bool operator <(const cv::Point2f &a, const cv::Point2f &b)
{
if (a.x < b.x) return true;
if (a.x > b.x) return false;
return a.y < b.y;
}

第二个注意事项是说有一个运算符

如果你想在std::map的情况下使用特定类型作为键(在这种情况下Point2f(,那么你必须为你的类型定义operator<,因为地图使用operator<对它的元素进行排序,否则它怎么知道哪个元素比另一个小?