# 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[3] = {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.

( 2018-11-19 15:05:26 -0500 )edit

Thanks, fixed.

( 2018-11-19 15:19:01 -0500 )edit

sorry, but it's probably stiill wrong

( 2018-11-19 15:22:08 -0500 )edit

thanks, I removed the zeros part.

( 2018-11-19 15:25:25 -0500 )edit

sorry, but still, no cigar here

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

( 2018-11-19 15:26:53 -0500 )edit

ooooook ;)

( 2018-11-19 15:31:13 -0500 )edit

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] = {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

## Stats

Seen: 174 times

Last updated: Nov 20 '18