Reputation: 543
I have a, for example, 4*5*6 3D matrix. I would like to divide it into 6 2D matrix. The purpose is to make data operation on these 2D matrix and get results. Tried row(), rowRange(), I got errors. No clues right now. Anyone throw any better ideas? Thanks~
Upvotes: 2
Views: 3596
Reputation: 11941
Remember that last index varies fastest, so maybe you mean you have a 6*5*4 Mat and would like to divide it into six 5x4 Mats. According to the documentation "3-dimensional matrices are stored plane-by-plane".
However, assuming your 3D Mat was created like this:
int dims[] = {4, 5, 6};
Mat m3(3, dims, CV_8UC1, data);
You can do something like this to do what you asked (but possibly not what you actually want):
Mat m2(4, 30, CV_8UC1, m3.data);
Mat m2x6 = m2.reshape(6);
std::vector<cv::Mat> channels;
cv::split(m2x6, channels);
However, to get out 4 images from m3
that have 5 rows x 6 cols:
Mat p0(5, 6, CV_8UC1, m3.data + m3.step[0] * 0);
Mat p1(5, 6, CV_8UC1, m3.data + m3.step[0] * 1);
Mat p2(5, 6, CV_8UC1, m3.data + m3.step[0] * 2);
Mat p3(5, 6, CV_8UC1, m3.data + m3.step[0] * 3);
Because support for 3D Mats in OpenCV is not great, avoid using them if you can. An alternative would be to use a 2D Mat that has multiple channels. That is often much easier to handle.
Upvotes: 5