opencv Scalar中的值错误

时间:2014-05-04 11:07:52

标签: c++ opencv

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

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

UPD。在开放的cv Scalar是

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;

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

你忘了把你的值转回8bit(因此溢出):

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;
}