user2970089
user2970089

Reputation: 235

how to access 2D matrix in a multi-dimension Mat in OpenCV

I have a multi-dimensional matrix in OpenCV, for example like

Mat matrix(1,1,CV_64FC(100), Scalar(0));

I know it is very easy to access the 2d matrix in a multi-dimension matrix in Matlab, for example like matrix(:,:,1), matrix(:,:,100). But how should we do this in OpenCV? It's true that OpenCV can use at to access one element in the matrix, but how can we access a 2d matrix while fixing the channel? Thanks!

Upvotes: 1

Views: 1619

Answers (2)

Yonatan Simson
Yonatan Simson

Reputation: 2575

Note there is difference between a multi-Channel Matrix and a multi-dimensional Matrix in OpenCV.

If you are interested in the latter here is an example that shows how to access each dimension for a 3d Matrix

Upvotes: 1

QED
QED

Reputation: 808

A simple way to do it would be the following

Mat matrix(1,1,CV_64FC(100), Scalar(0));

cv::vector<cv::Mat> channels;
cv::split(matrix,channels);
int i = 0;
cv::Mat channel_i = channels[i];

Pleae see docs split and maybe for more advanced manipulation mixChannels. (not used mixChannels myself)

Upvotes: 1

Related Questions