本文介绍了如何使用opencv C ++将3D矩阵分成2D矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个,例如,4 * 5 * 6的3D矩阵。我想把它分成6个2D矩阵。目的是使这些2D矩阵上的数据操作和获得结果。
Tried row(),rowRange(),我得到错误。现在没有线索。任何人抛出任何更好的想法?
感谢〜

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~

推荐答案

请记住,最后一个索引变化最快,所以也许你的意思是你有一个6 * 5 * 4 Mat,并想把它分成六个5x4垫。根据3维矩阵存储plane-

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".

但是,假设您的3D Mat是这样创建的:

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);

但是,要从 m3 具有5行x6列:

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);

因为在OpenCV中对3D Mats的支持不是很好,如果可以,请避免使用它们。
另一种方法是使用具有多个通道的2D Mat。这通常更容易处理。

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.

这篇关于如何使用opencv C ++将3D矩阵分成2D矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 02:27