Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ opencv标量中的值错误_C++_Opencv - Fatal编程技术网

C++ opencv标量中的值错误

C++ opencv标量中的值错误,c++,opencv,C++,Opencv,在这个简单的函数中,我将包含RGB信息的值从int转换为opencv标量 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 &

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

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中的值是不正确的。怎么了

UPD。在开放cv中,标量是

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;

有什么想法吗?

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

typedef struct CvScalar
{
    double val[4];
}
CvScalar;
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;
}