如何在c ++中的Map中存储cv :: Scalar对象

时间:2015-06-29 14:24:25

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

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

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 ==========

有人可以弄清楚我的代码中的错误,或者提供一种更好的方法来在Map中存储随机生成的标量颜色值。将它们存储在地图中的意图是避免生成重复的颜色值。我想防止生成类似的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)));

1 个答案:

答案 0 :(得分:1)

如果您希望将标量作为地图的关键,则需要提供“减去”字样。运算符,所以它可以对它进行排序。像这样:

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;