OpenCV - Mat :: convertTo()断言错误。

时间:2011-11-30 18:22:14

标签: c++ gcc opencv

我刚刚从Ubuntu 10.04迁移到11.10,我正在使用OpenCV 2.3.1。 出于某些原因,现在我做了类似的事情:

Mat_<unsigned> a = Mat_<unsigned>(10,2);
Mat_<float> b;

a.convertTo(b,CV_32F); 

我收到以下错误:

OpenCV Error: Assertion failed (func != 0) in convertTo, file /home/memecs/Desktop/OpenCV-2.3.1/modules/core/src/convert.cpp, line 937
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/memecs/Desktop/OpenCV-2.3.1/modules/core/src/convert.cpp:937: error: (-215) func != 0 in function convertTo

Aborted

,函数断言是:

Mat::convertTo(OutputArray _dst, int _type, double alpha, double beta) const
{
    bool noScale = fabs(alpha-1) < DBL_EPSILON && fabs(beta) < DBL_EPSILON;

    if( _type < 0 )
        _type = _dst.fixedType() ? _dst.type() : type();
    else
        _type = CV_MAKETYPE(CV_MAT_DEPTH(_type), channels());

    int sdepth = depth(), ddepth = CV_MAT_DEPTH(_type);
    if( sdepth == ddepth && noScale )
    {
        copyTo(_dst);
        return;
    }

    Mat src = *this;

    BinaryFunc func = noScale ? getConvertFunc(sdepth, ddepth) : getConvertScaleFunc(sdepth, ddepth);
    double scale[] = {alpha, beta};
    int cn = channels();

    ################### THIS IS THROWING THE ASSERTION ERROR ###############
    CV_Assert( func != 0 );


    if( dims <= 2 )
    {
        _dst.create( size(), _type );
        Mat dst = _dst.getMat();
        Size sz = getContinuousSize(src, dst, cn);
        func( src.data, src.step, 0, 0, dst.data, dst.step, sz, scale );
    }
    else
    {
        _dst.create( dims, size, _type );
        Mat dst = _dst.getMat();
        const Mat* arrays[] = {&src, &dst, 0};
        uchar* ptrs[2];
        NAryMatIterator it(arrays, ptrs);
        Size sz((int)(it.size*cn), 1);

        for( size_t i = 0; i < it.nplanes; i++, ++it )
            func(ptrs[0], 0, 0, 0, ptrs[1], 0, sz, scale);
    }
}

有任何帮助吗?我真的不知道如何解决这个问题。

2 个答案:

答案 0 :(得分:3)

Mat_<unsigned>对OpenCV毫无用处。 OpenCV函数都不支持unsigned int数据类型。请尝试使用signed intunsigned short代替。它们受OpenCV支持。

答案 1 :(得分:1)

请试试。

Mat a = Mat_<unsigned char>(10,2);
Mat b ;
a.convertTo(b,CV_32F);
cout<<"a type"<<endl<<a.depth()<<endl<<endl<<"b type"<<endl<<b.depth()<<endl;