Ask Your Question
0

Conversion between multichannel and multidimensional matrices

asked 2018-11-19 15:01:29 -0600

atp gravatar image

updated 2018-11-19 15:28:12 -0600

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 flag offensive close merge delete

Comments

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

berak gravatar imageberak ( 2018-11-19 15:05:26 -0600 )edit

Thanks, fixed.

atp gravatar imageatp ( 2018-11-19 15:19:01 -0600 )edit

sorry, but it's probably stiill wrong

berak gravatar imageberak ( 2018-11-19 15:22:08 -0600 )edit

thanks, I removed the zeros part.

atp gravatar imageatp ( 2018-11-19 15:25:25 -0600 )edit

sorry, but still, no cigar here

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

berak gravatar imageberak ( 2018-11-19 15:26:53 -0600 )edit

ooooook ;)

berak gravatar imageberak ( 2018-11-19 15:31:13 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-11-20 05:53:14 -0600

berak gravatar image

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);
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-11-19 15:01:29 -0600

Seen: 586 times

Last updated: Nov 20 '18