简体   繁体   English

在OpenCV(C ++)中打印多维Mat

[英]Print multidimensional Mat in OpenCV (C++)

In the OpenCV Tutorial 在OpenCV教程中

http://docs.opencv.org/master/d6/d6d/tutorial_mat_the_basic_image_container.html http://docs.opencv.org/master/d6/d6d/tutorial_mat_the_basic_image_container.html

is the following example for creating a Mat. 以下是创建Mat的示例。

int sz[3] = {2,2,2};
Mat L(3,sz, CV_8UC(1), Scalar::all(0));

This works fine, but when i try to print the Mat my programm crashes. 这工作正常,但是当我尝试打印Mat时,我的程序崩溃了。

cout << "L = " << endl << " " << L << endl << endl;

Why doesn't this work ? 为什么不起作用? Is there a way to do this without loops or splitting the Mat L ? 有没有一种方法可以做到没有循环或分裂Mat L?

To print n-dim matrix you could use Matrix slice. 要打印n维矩阵,可以使用Matrix slice。 Since 2d matrices are stored row by row, 3d matrices plane by plane and so on, you could use code: 由于2d矩阵是逐行存储的,3d矩阵是逐平面存储的,依此类推,因此可以使用以下代码:

cv::Mat sliceMat(cv::Mat L,int dim,std::vector<int> _sz)
{
cv::Mat M(L.dims - 1, std::vector<int>(_sz.begin() + 1, _sz.end()).data(), CV_8UC1, L.data + L.step[0] * 0);
return M;
}

To perform mat slice.For more dimensions you should make more slices. 要进行垫层切片,要获得更多尺寸,您应该制作更多切片。 Example shows 3 and 4 dimension matrices: 示例显示了3维和4维矩阵:

std::cout << "3 dimensions" << std::endl;

std::vector<int> sz = { 3,3,3 };

cv::Mat L;
L.create(3, sz.data(), CV_8UC1);
L = cv::Scalar(255);

std::cout<< sliceMat(L, 1, sz);

std::cout << std::endl;
std::cout <<"4 dimensions"<< std::endl;
sz = { 5,4,3,5 };
L.create(4, sz.data(), CV_8UC1);
L = cv::Scalar(255);
std::cout << sliceMat(sliceMat(L, 1, sz),2, std::vector<int>(sz.begin() + 1, sz.end()));

end result screen 最终结果屏幕

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM