Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

First I want to say that the c style cvMat is deprecated and wont be supported in the future, maybe even removed.

Anyways, you are basically doing 2 mistakes. You are creating a Mat of type uchar, but your data is a double array, and you are trying to access the cvMat in a wrong way for you data type.

correct initialization:

double a[]={1.0,2.0,3.0,4.0};
CvMat M=cvMat(8,8,CV_64FC1, a);

To access the data:

cout<<"M.data.ptr="<< M.data.db[0]<<endl;

Looking at the data union would have helped in this case.

union
{
    uchar* ptr;
    short* s;
    int* i;
    float* fl;
    double* db;
} data;

First I want to say that the c style cvMat is deprecated and wont be supported in the future, maybe even removed.

Anyways, you are basically doing 2 mistakes. You are creating a Mat of type uchar, but your data is a double array, and you are trying to access the cvMat in a wrong way for you your data type.

correct initialization:

double a[]={1.0,2.0,3.0,4.0};
CvMat M=cvMat(8,8,CV_64FC1, a);

To access the data:

cout<<"M.data.ptr="<< M.data.db[0]<<endl;

Looking at the data union would have helped in this case.

union
{
    uchar* ptr;
    short* s;
    int* i;
    float* fl;
    double* db;
} data;