计算 cv::Points2f 向量中的重复项
Counting duplicates in a vector of cv::Points2f
我正在尝试查找并计算向量中重复的 cv::Points2f。为此,我尝试使用以下功能。但是当我尝试使用解除引用的值 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&’
非常感谢您!
如果您想在 std::map
(在本例中为 Point2f
)中使用特定类型作为键,那么您必须为您的类型定义 operator<
,因为映射使用 operator<
对它的元素进行排序,否则它怎么知道哪个元素比另一个小?
显然您缺少点 class 的比较运算符,因此您需要提供它,例如:
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;
}
第二个注释是说有一个运算符 < 可用于 cv:::String,但 Point2f 不能转换为它。
我正在尝试查找并计算向量中重复的 cv::Points2f。为此,我尝试使用以下功能。但是当我尝试使用解除引用的值 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&’
非常感谢您!
如果您想在 std::map
(在本例中为 Point2f
)中使用特定类型作为键,那么您必须为您的类型定义 operator<
,因为映射使用 operator<
对它的元素进行排序,否则它怎么知道哪个元素比另一个小?
显然您缺少点 class 的比较运算符,因此您需要提供它,例如:
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;
}
第二个注释是说有一个运算符 < 可用于 cv:::String,但 Point2f 不能转换为它。