David
David

Reputation: 11

Pointer Arithmetic in Opencv Using CvMat?

So I'm trying to access matrix elements via a pointer. Here is my code:

    CvMat *Q = cvCreateMat(3,3, CV_32F);
    for(int i = 0; i < Q->rows ; i++){
    float *ptr = (float *)(Q->data.ptr + i * Q->step );
    for(int j = 0; j < Q->cols ; j++){
        *ptr = 0.0;
        if((i ==0)&&(j==0)) *ptr = 1;
        if((i ==0)&&(j==1)) *ptr = 2;
        if((i ==0)&&(j==2)) *ptr = 3;
        if((i ==1)&&(j==0)) *ptr = 4;
        if((i ==1)&&(j==1)) *ptr = 5;
        if((i ==1)&&(j==2)) *ptr = 6;
        if((i ==2)&&(j==0)) *ptr = 7;
        if((i ==2)&&(j==1)) *ptr = 8;
        if((i ==2)&&(j==2)) *ptr = 9;
        //cout << *ptr << endl;
        //system("pause");
    }
}
cout << CV_MAT_ELEM(*Q,float,0,0) << endl;
cout << CV_MAT_ELEM(*Q,float,0,1) << endl;
cout << CV_MAT_ELEM(*Q,float,0,2) << endl;
cout << CV_MAT_ELEM(*Q,float,1,0) << endl;
cout << CV_MAT_ELEM(*Q,float,1,1) << endl;
cout << CV_MAT_ELEM(*Q,float,1,2) << endl;
cout << CV_MAT_ELEM(*Q,float,2,0) << endl;
cout << CV_MAT_ELEM(*Q,float,2,1) << endl;
cout << CV_MAT_ELEM(*Q,float,2,2) << endl;
system("pause");

I am trying to make the matrix in the for loop as:

[1 2 3

4 5 6

7 8 9],

but when cout'ing them, I get:

3

-4.31602e+008

-4.31602e+008

6

-4.31602e+008

-4.31602e+008

9

-4.31602e+008

-4.31602e+008

Where is the -4.31602e+008 coming from? What am I not understanding here? I'm a little new to pointers.

Upvotes: 0

Views: 678

Answers (2)

David Nilosek
David Nilosek

Reputation: 1412

Check out the API for CvMat (also you might want to consider using Mat if you can use c++).

I'm not totally sure what you are trying to accomplish here, but if you want to access the data using the pointer you are doing it slightly wrong at this point.

float *ptr = (float *)(Q->data.ptr + i * Q->step );

The step here is the number of bytes in a row (so here it would be 12, 4 bytes per element * 3 elements) the pointer will automatically step the correct number of bytes based on the datatype of the pointer when you do arithmetic with it (Good tutorial here). In order to access it like an array you should do it like this:

CvMat *Q = cvCreateMat(3,3, CV_32F);
for(int i = 0; i < Q->rows ; i++){
  for(int j = 0; j < Q->cols ; j++){
    float *ptr = (float *)(Q->data.ptr) + i*Q->rows + j; //Index is row major
    if((i ==0)&&(j==0)) *ptr = 1;
    if((i ==0)&&(j==1)) *ptr = 2;
    if((i ==0)&&(j==2)) *ptr = 3;
    if((i ==1)&&(j==0)) *ptr = 4;
    if((i ==1)&&(j==1)) *ptr = 5;
    if((i ==1)&&(j==2)) *ptr = 6;
    if((i ==2)&&(j==0)) *ptr = 7;
    if((i ==2)&&(j==1)) *ptr = 8;
    if((i ==2)&&(j==2)) *ptr = 9;
   }
}

A much simpler solution would be to use the CV_MAT_ELEM macro that exists.

CvMat *Q = cvCreateMat(3,3, CV_32F);
for(int i = 0; i < Q->rows ; i++){
  for(int j = 0; j < Q->cols ; j++){
    CV_MAT_ELEM(*Q, float, i, j) = i*Q->rows + j + 1;
   }
}

Upvotes: 2

Dima
Dima

Reputation: 39389

You should increment ptr inside your inner j loop.

Upvotes: 0

Related Questions