Ask Your Question
0

Why do i get this ^@ when i attempt to access the first element of a CvMat data struct member?

asked 2013-10-11 10:02:29 -0600

joeish80829 gravatar image

updated 2013-10-11 16:41:51 -0600

Moster gravatar image

when i run this code

  double a[]={1.0,2.0,3.0,4.0};
  CvMat M=cvMat(8,8,CV_8UC1, a);
  cout<<"M.data.ptr="<< M.data.ptr[0]<<endl;

to access a 1.0, the first element of the matrix, i get the output M.data.ptr=^@ oddly enough i tried copying the cout output into this ? and the ^@ wouldn't copy..i got only M.data.ptr when i pasted...I tied other variations like M.data.ptr[0] and M.data.ptr but just got normal error and null output for those 2 respectively. how would i go about accessing the 1.0 by dereferencing the data struct member and not using a function i/e cvGet2D...just in case its emacs thats doing this i thought id report im using Emacs on Ubuntu Saucy...

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
4

answered 2013-10-11 16:38:23 -0600

Moster gravatar image

updated 2013-10-11 16:39:09 -0600

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 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;
edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-10-11 10:02:29 -0600

Seen: 196 times

Last updated: Oct 13 '13