如何将Mat 2D重新整形为矢量<vector <mat>&gt;?

时间:2017-07-25 10:22:20

标签: c++ opencv

我想转换尺寸为[2000] [256]的Mat,我想将其转换为尺寸为[256] [20] [10] [10]的vector<vector<mat> >。在Matlab中,可以reshape(Mat2d, 10, 10, 20, 256);我想在这里也这样做。

1 个答案:

答案 0 :(得分: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;
}