【问题标题】:Counting duplicates in a vector of cv::Points2f计算 cv::Points2f 向量中的重复项
【发布时间】:2017-12-23 00:21:21
【问题描述】:

我正在尝试在向量中查找和计算重复的 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&’

非常感谢您!

【问题讨论】:

  • 什么是“错误”?

标签: c++ opencv vector stdmap


【解决方案1】:

如果您想在std::map(在本例中为Point2f)中使用特定类型作为键,那么您必须为您的类型定义operator&lt;,因为映射使用operator&lt; 对其元素进行排序,否则它怎么知道哪个元素比另一个小?

【讨论】:

    【解决方案2】:

    显然您缺少 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;
    }
    

    第二个注释是说有一个运算符

    【讨论】:

    • 感谢您的回答。我真的是重新定义运营商的新人。当我在我的函数之前复制这个运算符实现并再次编译我的代码时,我得到了同样的错误。我可能忘记了什么或做错了什么?
    • 我意识到我必须添加 namespace cv {... your code} 才能使其正常工作。再次感谢您!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-08
    • 1970-01-01
    相关资源
    最近更新 更多