# Conversion between multichannel and multidimensional matrices

Is there a direct way to convert from a multichannel matrix to a multidimensional matrix (and vice versa)?

For example, in the following code snippet, how could I convert A such that it has the same shape as B?

const int sizes = {50, 50, 3};
cv::Mat A(3, sizes, CV_64F);
cv::Mat B(50, 50, CV_64FC(3));

edit retag close merge delete

cv::Mat B = Mat::zeros(50, 50, 3); looks invalid. (using hardcoded 3 for "type") please check again.

sorry, but still, no cigar here

(please try to compile it before posting, just saying)

Sort by » oldest newest most voted

if it really is as in your example above, it would be a simple:

Mat C = A.reshape(3); // 3rd dimension -> channels


if you have seperate "planes", like:

const int sizes = {3, 50, 50};
cv::Mat A(3, sizes, CV_64F);


then it needs a different procedure:

vector<Mat> chn = {
Mat(50,50,CV_64F, A.ptr<double>(0)),
Mat(50,50,CV_64F, A.ptr<double>(1)),
Mat(50,50,CV_64F, A.ptr<double>(2))
};

Mat C;
merge(chn, C);

more

Official site

GitHub

Wiki

Documentation