例如,我有一个 4*5*6 3D 矩阵。我想把它分成6个二维矩阵。目的是对这些二维矩阵进行数据运算并得到结果。尝试了 row()、rowRange(),我得到了错误。目前没有线索。有人提出更好的想法吗?谢谢~
1 回答
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.