user3747190
user3747190

Reputation: 1717

How to store values from an array to a Mat in OpenCV

I have an array of double. I want to store them in Mat. This is how I am doing it.

double g2f1_[] = { 0.001933,  0.118119,  0.338927, -0.921300,  0.338927, 0.118119, 0.001933};
g2f1y=Mat(7,1,CV_32F,&g2f1_);
        for(int i=0;i<7;i++){
                        for(int j=0;j<1;j++){
                                cout<<g2f1y.at<float>(i,j)<<" ";

                        }
                        cout<<endl;
                }

But when I cout those values I get the following result which is entirely different then what I had stored. Also I am getting different values on running it again and again.

Output:

8.65736e+31 
0 
3.61609e+31 
0 
0 
0 
1.02322e+15  

I have gone through following link

initialize an OpenCV Mat with an 2D array

Upvotes: 0

Views: 1607

Answers (1)

Y.AL
Y.AL

Reputation: 1793

We need to modify when creating Mat to specify the type CV_32F for storing a float array, or CV_64F for double array. so both solutions worked with me:

float g2f1_[] = { 0.001933,  0.118119,  0.338927, -0.921300,  0.338927, 0.118119, 0.001933};
Mat g2f1y=Mat(7,1,CV_32F,&g2f1_);
for(int i=0;i<7;i++){
    for(int j=0;j<1;j++){
      cout<<g2f1y.at<float>(i,j)<<" ";
  }
    cout<<endl;
}

Or

   double g2f1_[] = { 0.001933,  0.118119,  0.338927, -0.921300,  0.338927, 0.118119, 0.001933};
    Mat g2f1y=Mat(7,1,CV_64F,&g2f1_);
    for(int i=0;i<7;i++){
        for(int j=0;j<1;j++){
          cout<<g2f1y.at<double>(i,j)<<" ";
      }
        cout<<endl;
    }

Upvotes: 3

Related Questions