【问题标题】:How to store cv::Scalar objects in a Map in c++如何在 C++ 中的 Map 中存储 cv::Scalar 对象
【发布时间】:2015-09-16 01:34:34
【问题描述】:

我想将随机生成的标量值存储在地图中。但是下面的尝试给了我一个编译错误。

RNG rng(0xFFFFFFFF);
std::map<Scalar, int> segmentColors;

Scalar randomColorTemp1 = randomColor(rng);
Scalar randomColorTemp2 = randomColor(rng);

segmentColors.insert(pair<Scalar, int>(randomColorTemp1, 1));
segmentColors.insert(pair<Scalar, int>(randomColorTemp1, 1));

报错如下。

1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\xstddef(193): error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const cv::Scalar' (or there is no acceptable conversion)
1>          e:\opencv\build\include\opencv2\core\operations.hpp(3193): could be 'bool cv::operator <(const cv::FileNodeIterator &,const cv::FileNodeIterator &)' [found using argument-dependent lookup]
1>          e:\opencv\build\include\opencv2\core\mat.hpp(1303): or       'cv::MatExpr cv::operator <(const cv::Mat &,const cv::Mat &)' [found using argument-dependent lookup]
1>          e:\opencv\build\include\opencv2\core\mat.hpp(1304): or       'cv::MatExpr cv::operator <(const cv::Mat &,double)' [found using argument-dependent lookup]
1>          e:\opencv\build\include\opencv2\core\mat.hpp(1305): or       'cv::MatExpr cv::operator <(double,const cv::Mat &)' [found using argument-dependent lookup]
1>          e:\opencv\build\include\opencv2\core\mat.hpp(1984): or       'bool cv::operator <(const cv::MatConstIterator &,const cv::MatConstIterator &)' [found using argument-dependent lookup]
1>          while trying to match the argument list '(const cv::Scalar, const cv::Scalar)'
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\xstddef(192) : while compiling class template member function 'bool std::less<_Kty>::operator ()(const _Ty &,const _Ty &) const'
1>          with
1>          [
1>              _Kty=cv::Scalar
1>  ,            _Ty=cv::Scalar
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\map(228) : see reference to function template instantiation 'bool std::less<_Kty>::operator ()(const _Ty &,const _Ty &) const' being compiled
1>          with
1>          [
1>              _Kty=cv::Scalar
1>  ,            _Ty=cv::Scalar
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\type_traits(572) : see reference to class template instantiation 'std::less<_Kty>' being compiled
1>          with
1>          [
1>              _Kty=cv::Scalar
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\xtree(1023) : see reference to class template instantiation 'std::is_empty<std::less<_Kty>>' being compiled
1>          with
1>          [
1>              _Kty=cv::Scalar
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\map(70) : see reference to class template instantiation 'std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>' being compiled
1>          with
1>          [
1>              _Kty=cv::Scalar
1>  ,            _Ty=int
1>  ,            _Pr=std::less<cv::Scalar>
1>  ,            _Alloc=std::allocator<std::pair<const cv::Scalar,int>>
1>          ]
1>          g:\uom\level 4-s1\research project\coding tests\textextraction\textextraction\source1.cpp(288) : see reference to class template instantiation 'std::map<cv::Scalar,int,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>' being compiled
1>          with
1>          [
1>              _Kty=cv::Scalar
1>  ,            _Ty=int
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

有人可以找出我的代码中的错误或提供更好的方法来将随机生成的标量颜色值存储在地图中吗?将它们存储在地图中的目的是避免生成重复的颜色值。我想防止生成相似的 RGB 值。


更新


根据 berak 的回答,我尝试了这个。


    RNG rng(0xFFFFFFFF);
    struct ScalarLess
    {
        bool operator()(Scalar &a, Scalar &b)
        {
            return a[0] < b[0]; // bogus, i doubt, that you need *real* sorting
        }
    };
    Scalar randomColorTemp = randomColor(rng);
    map<Scalar, int, ScalarLess> segmentColorsMap;
    segmentColorsMap.insert((pair<Scalar, int>(randomColorTemp, 2)));

【问题讨论】:

    标签: c++ opencv dictionary data-structures collections


    【解决方案1】:

    如果您希望 Scalar 作为地图的键,您需要提供“Less”运算符,以便对其进行排序。像这样:

    struct ScalarLess
    {
       bool operator()(const Scalar &a, const Scalar &b)
       {
           return a[0] < b[0]; // bogus, i doubt, that you need *real* sorting
       }
    };
    
    
    map<Scalar,int,ScalarLess> segmentColors;
    

    【讨论】:

    • 或者为Scalar定义operator&lt;
    • @ooga,那必须是班级成员,不是吗?
    • 我不认为它必须是会员。
    • 我试过这个。但仍然是相同类型的错误。 c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(523): error C2664: 'bool wmain::ScalarLess::operator ()(cv::Scalar &,cv::Scalar &) ' : 无法将参数 1 从 'const cv::Scalar_' 转换为 'cv::Scalar &' 1> 转换丢失限定符我将添加我的新代码作为问题的更新。
    • 你能解释一下通过在地图声明中添加一个 ScarLess 成员所做的事情吗?我是 C++ 新手,请您简要解释一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-29
    • 2011-08-10
    • 1970-01-01
    • 2020-06-01
    • 2019-05-18
    相关资源
    最近更新 更多