C++vector and opencv Mat

转载:https://blog.csdn.net/u012507022/article/details/50979011?utm_source=blogxgwz5

 

最近在写Opencv程序,用到离散小波变换,但Opencv没有提供离散小波变换函数。但找到了离散小波变换函数的C++程序,发现在C++中,可以用 vector<vector<double>> 来表示矩阵。

 

  1.  
    int Height = 200;
  2.  
    int Width = 400;
  3.  
    vector<vector<double>> signal2(Height,Width);
  4.  
    cout<<"矩阵的行数"<<signal2.size()<<endl;
  5.  
    cout<<"矩阵的列数"<<signal2[0].size()<<endl;

 

vector<vector<double>>与Mat数据类型可以进行转换

  1.  
    //Mat 转换为vector<vector<double>>
  2.  
    void Mat2vector(Mat &src,vector<vector<double>> &dst)
  3.  
    {
  4.  
    assert( src.rows== dst.size());
  5.  
    MatIterator_<double> it, it2;
  6.  
    it = src.begin<double>();
  7.  
    it2 = src.end<double>();
  8.  
    for(int i=0;i<src.rows;i++)
  9.  
    {
  10.  
    for(int j=0;j<src.cols;j++)
  11.  
    {
  12.  
    dst[i][j]=*it++;
  13.  
    }
  14.  
    }
  15.  
    }
  16.  
    //vector<vector<double>> 转换为Mat
  17.  
    void Vector2Mat(vector<vector<double>>src,Mat dst)
  18.  
    {
  19.  
    assert( dst.rows== src.size());
  20.  
    MatIterator_<double> it = dst.begin<double>();
  21.  
    for(int i=0;i<src.size();i++)
  22.  
    {
  23.  
    for(int j=0;j<src[0].size();j++)
  24.  
    {
  25.  
    *it=src[i][j];
  26.  
    it++;
  27.  
    }
  28.  
    }
  29.  
    }

 

 

 
posted @ 2020-09-13 14:34  梅长苏枫笑  阅读(257)  评论(0编辑  收藏  举报