gpt4 book ai didi

c++ - opencv标量中的错误值

转载 作者:太空宇宙 更新时间:2023-11-03 22:58:35 25 4
gpt4 key购买 nike

在这个简单的函数中,我将包含 RGB 信息的值从 int 转换为 opencv Scalar。

Scalar IntToScalarColour(int i){
//Scalar scal((int)(i & 0x000000FF),(int)(i & 0x0000FF00),(int)(i & 0x00FF0000));
int b = i & 0x000000FF;
int g = i & 0x0000FF00;
int r = i & 0x00FF0000;
Scalar scal(b,g,r);
return scal;
}

但在调试中我发现 scal 中的值不正确。怎么了?

enter image description here

更新。在开放的简历标量是

template<typename _Tp> class Scalar_ : public Vec<_Tp, 4>
{
public:
//! various constructors
Scalar_();
Scalar_(_Tp v0, _Tp v1, _Tp v2=0, _Tp v3=0);
Scalar_(const CvScalar& s);
Scalar_(_Tp v0);

//! returns a scalar with all elements set to v0
static Scalar_<_Tp> all(_Tp v0);
//! conversion to the old-style CvScalar
operator CvScalar() const;

//! conversion to another data type
template<typename T2> operator Scalar_<T2>() const;

//! per-element product
Scalar_<_Tp> mul(const Scalar_<_Tp>& t, double scale=1 ) const;

// returns (v0, -v1, -v2, -v3)
Scalar_<_Tp> conj() const;

// returns true iff v1 == v2 == v3 == 0
bool isReal() const;
};

typedef Scalar_<double> Scalar;

typedef struct CvScalar
{
double val[4];
}
CvScalar;

有什么想法吗?

最佳答案

您忘记将您的值移回 8 位(因此溢出):

Scalar IntToScalarColour(int i){
//Scalar scal((int)(i & 0x000000FF),(int)(i & 0x0000FF00),(int)(i & 0x00FF0000));
uchar b = (i & 0x000000FF);
uchar g = (i & 0x0000FF00) >> 8;
uchar r = (i & 0x00FF0000) >> 16;
Scalar scal(b,g,r);
return scal;
}

关于c++ - opencv标量中的错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23455491/

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