如何存储值从一个数组到一个Mat在OpenCV

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

本文关键字:一个 数组 Mat OpenCV 何存储 存储      更新时间:2023-10-16

我有一个double类型的数组。我想把它们存储在Mat中,这就是我的做法。

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;
                }

但是当我计算这些值时,我得到以下结果,这与我存储的结果完全不同。我也得到不同的值运行一次又一次。

输出:

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

我已经浏览了下面的链接

用2D数组初始化OpenCV Mat

我们需要在创建Mat时进行修改,以指定用于存储float数组的类型CV_32F,或用于存储double数组的类型CV_64F。所以这两种解决方案对我都有效:

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;
}

   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;
    }
相关文章: