2017-07-25 145 views
0

我想轉換尺寸爲[2000] [256]的Mat,並且我想將其轉換爲尺寸爲[256] [20] [10] [10]的vector<vector<mat> >。在Matlab中,有可能做到reshape(Mat2d, 10, 10, 20, 256);我想在這裏做同樣的事情。我如何重塑Mat 2D到矢量<vector<Mat>>?

+0

AFAIK沒有明顯的方式做到這一點整形,所以你必須指定它應該是怎樣做(又名手工編寫代碼) – user463035818

+0

出色, matlab它可以做重塑(mat2d,10,10,20,256),我想要做同樣的 –

+0

你看過像Eigen任何圖書館http://eigen.tuxfamily.org/index.php?title =涵蓋線性代數的Main_Page? (特別是http://eigen.tuxfamily.org/dox/group__TutorialReshapeSlicing.html) –

回答

0

我找到了一個解決方案:

vector<vector<Mat>> 
reshape2D4D(Mat &A, int dim1, int dim2, int dim3, int dim4) 
{ 
    vector<vector<Mat>> B; 
    for (int i = 0; i<dim1; i++) { 
     B.push_back(vector<Mat>()); 
     for (int j = 0; j<dim2; j++) 
      B[i].push_back(Mat(dim3, dim4, CV_64F, Scalar::all(1))); 
    } 
    Mat in = A.reshape(1, 1); 
    Mat subImg; 
    for (int i = 0; i < 256; i++) 
    { 
     for (int j = 0; j < 20; j++) 
     { 
      subImg = in(cv::Range(0, 1), cv::Range(100*j, 100*j + 100)); 
      subImg.reshape(10, 10); 
      B[i][j] = subImg; 
     } 
    } 
    return B; 
}