在VS下使用OpenCV,有时候需要查看Mat里面的数据,可是VS并不提供直接查看的功能。在网上搜索了一下,大概有以下几种方式,这里以链接的方式给出。
1.【打印到屏幕】
直接调用std::cout输出到屏幕查看,它会自动按行来输出。
cv::Mat M = cv::eye(1); std::cout<<M<<std::endl;
该方法比较简单,适用于尺寸比较小的Mat,如果矩阵比较大,查看仍然是不方便的。
2.【输出到txt文档】
利用c++中的ofstream,输出到文档查看
#include <opencv2\opencv.hpp> #include <fstream> using namespace cv; void main() { cv::Mat M = (cv::Mat_<uchar>(3,3) << 1,2,3, 4,5,6, 7,8,9); //使用ofstream来输出mat std::ofstream Fs("test.txt"); if( !Fs.is_open() ) { std::cout<<"open file error!"<<std::endl; return; } //循环输出 int height = M.rows; int width = M.cols; for( int i=0; i<height; i++ ) { for( int j=0; j<width; j++ ) { Fs<<(int)M.ptr<uchar>(i)[j]<<'\t'; //不加类型转换用txt打开是字符 } Fs<<std::endl; } Fs.close(); }
用这种方法输出得到的文档可以放到excel里面或者matlab里面查看,可以比较实用。
3.【复制到数组里查看】