为什么断言失败了

为什么断言失败了

本文介绍了为什么断言失败了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么当我创建一个 CvMat * 时,断言失败?在cv :: Mat中使用指针加载图片时不会发生。

Why does the assertion fail here when i create a CvMat *? It does not happen with an image i load in cv::Mat using a pointer.

    struct RGB { unsigned char b, g, r; };
    cv::Point p;
    RGB *data;
    CvMat* mat = cvCreateMat(300,300,CV_32FC1);
    for( row = 0; row < mat->rows; ++row)
    {
            for ( col = 0; col < mat->cols; ++col)
            {
                 p.x=row,p.y=col;
        ERROR ----->>>   assert((mat->step/mat->cols) == sizeof(RGB));
                 data = (RGB*)&mat->data;
                 data += p.y * mat->cols + p.x;
            }
    }

对于此代码,断言不会失败: p>

For this code the assertion does not fail:

    IplImage * img=cvLoadImage("blah.jpg");
    int row=0,col=0;
    cv::Mat in(img);
    cv::Mat *mat=&in;
    cv::Point p;
    struct RGB { unsigned char b, g, r; };
    RGB *data;
    for( row = 0; row < mat->rows; ++row)
    {
            for ( col = 0; col < mat->cols; ++col)
            {
                 p.x=row,p.y=col;
                 assert((mat->step/mat->cols) == sizeof(RGB));
                 data = (RGB*)&mat->data;
                 data += p.y * mat->cols + p.x;
                 printf("Row=%dxCol=%d      b=%u g=%u r=%u\n",row,col,data->b,data->g,data->r);
                 wait_for_frame(1);
            }
    }


推荐答案

sizeof(RGB)!= sizeof(float),这是你在这里填写的矩阵:

Because sizeof(RGB) != sizeof(float), which is what you filled the matrix with here:

 CvMat* mat = cvCreateMat(300,300,CV_32FC1);

CV_32FC1 表示1个组件,32位浮点。您可能需要 CV_8UC3 。请参见或其他OpenCV参考。

CV_32FC1 means 1 component, 32-bit floating point. You probably want CV_8UC3. See here or another OpenCV reference.

这篇关于为什么断言失败了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:24