gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:11:51 25 4
gpt4 key购买 nike

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

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 中存储随机生成的标量颜色值。将它们存储在 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)));

最佳答案

如果您希望标量作为 map 的键,您需要提供一个“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;

关于c++ - 如何在 C++ 中的 Map 中存储 cv::Scalar 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31118209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com