2013-08-27 69 views
0

我試圖在Matrix.M中保存4個矩陣,但我不確定是否可以這樣做。因此我將4矩陣轉換爲4個矢量。現在我想在矩陣中插入4個向量。我的代碼是:將節點保存在Mat中opencv

 Mat hist_TL = Mat::zeros(20,1, CV_32F); 
     Mat hist_TR = Mat::zeros(20,1, CV_32F); 
     Mat hist_BL = Mat::zeros(20,1, CV_32F); 
     Mat hist_BR = Mat::zeros(20,1, CV_32F); 
     for(int i=1;i<=21;i++) 
     { 
      for(int k=0;k<TL_k_stats.rows;k++) 
      { 
      if((angl_TL<=bins[i]) && (angl_TL>bins[i-1])) 
      { 
       hist_TL.at<float>(i-1,0)+=TL_k_stats.at<float>(k,4); 
      } 
      if((angl_TR<=bins[i]) && (angl_TR>bins[i-1])) 
       { 
       hist_TR.at<float>(i-1,0)+=TR_k_stats.at<float>(k,4); 
       } 

      if((angl_BL<=bins[i]) && (angl_BL>bins[i-1])) 
       { 
       hist_BL.at<float>(i-1,0)+=BL_k_stats.at<float>(k,4); 
       } 

      if((angl_BR<=bins[i]) && (angl_BR>bins[i-1])) 
       { 
       hist_BR.at<float>(i-1,0)+=BR_k_stats.at<float>(k,4); 
       } 
      } 
     hist_TL=hist_TL.inv(); 
     hist_TR=hist_TR.inv(); 
     hist_BL=hist_BL.inv(); 
     hist_BR=hist_BR.inv(); 
     std::vector<float> vhist_TL; 
     std::vector<float> vhist_TR; 
     std::vector<float> vhist_BL; 
     std::vector<float> vhist_BR; 

     hist_TL.copyTo(vhist_TL); 
     hist_TR.copyTo(vhist_TR); 
     hist_BL.copyTo(vhist_BL); 
     hist_BR.copyTo(vhist_BR); 

所以我想複製4個向量到一個矩陣。如果有什麼辦法可以不用矩陣來vector.Please轉換讓我know.In matlab的我們可以把它直接存儲到一個數組中,並返回它像這樣

features[] = {hist_TL', hist_TR', hist_BL', hist_BR'}; 

那麼,如何才能做到這一點在opencv ??

回答

0

你可以利用「山口」的方法,讓你值分別分配給列:

Mat hist_TL = Mat::zeros(20,1, CV_32F)+1; 
    Mat hist_TR = Mat::zeros(20,1, CV_32F)+2; 
    Mat hist_BL = Mat::zeros(20,1, CV_32F)+3; 
    Mat hist_BR = Mat::zeros(20,1, CV_32F)+4; 

    Mat result = Mat::zeros(20,4,CV_32F); 

    hist_TL.col(0).copyTo(result.col(0)); 
    hist_TR.col(0).copyTo(result.col(1)); 
    hist_BL.col(0).copyTo(result.col(2)); 
    hist_BR.col(0).copyTo(result.col(3)); 

UPDATE:

如果你不想更換整列,但只是他們中的部分,你可以使用「範圍」方法:

Mat result = Mat::zeros(20*4,1,CV_32F); 

    hist_TL.copyTo(result.rowRange(Range(20*0,20*1))); 
    hist_TR.copyTo(result.rowRange(Range(20*1,20*2))); 
    hist_BL.copyTo(result.rowRange(Range(20*2,20*3))); 
    hist_BR.copyTo(result.rowRange(Range(20*3,20*4))); 
+0

但我不得不調換墊hist_TL和其他節能it.So在這之前將有一個單行munltiple列 – user1583647

0

你可以複製4矩陣成一個較大的直接像這樣的:

Mat TL(rows, cols, type); 
    Mat TR(rows, cols, type); 
    Mat BL(rows, cols, type); 
    Mat BR(rows, cols, type); 
    Mat dst(rows * 2, cols *2, type); 
    TL.copyTo(dst(Rect(0, 0, cols, rows))); 
    TR.copyTo(dst(Rect(cols, 0, cols, rows))); 
    BL.copyTo(dst(Rect(0, rows, cols, rows))); 
    BR.copyTo(dst(Rect(cols, rows, cols, rows))); 

的文件:

http://docs.opencv.org/modules/core/doc/basic_structures.html#rect http://docs.opencv.org/modules/core/doc/basic_structures.html?highlight=copyto#mat-copyto