在OpenCV(C ++)中打印多维Mat [英] Print multidimensional Mat in OpenCV (C++)

查看:668
本文介绍了在OpenCV(C ++)中打印多维Mat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在OpenCV教程

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

是以下创建Mat的示例.

is the following example for creating a Mat.

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

这很好,但是当我尝试打印Mat时,我的程序崩溃了.

This works fine, but when i try to print the Mat my programm crashes.

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

为什么这行不通? 有没有办法做到这一点而无需循环或分裂Mat L?

Why doesn't this work ? Is there a way to do this without loops or splitting the Mat L ?

推荐答案

要打印n维矩阵,可以使用Matrix slice.由于2d矩阵是逐行存储的,3d矩阵是逐平面存储的,依此类推,因此可以使用代码:

To print n-dim matrix you could use Matrix slice. Since 2d matrices are stored row by row, 3d matrices plane by plane and so on, you could use code:

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

要执行垫层切片.要获得更大的尺寸,您应该制作更多的切片.示例显示了3维和4维矩阵:

To perform mat slice.For more dimensions you should make more slices. Example shows 3 and 4 dimension matrices:

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

最终结果屏幕

这篇关于在OpenCV(C ++)中打印多维Mat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆