在使用OpenCV开发程序时,如果想查看矩阵数据,比较费劲,而matlab查看数据很方便,有一种方法,是matlab和c++混合编程,可以用matlab访问c++的内存,可惜我不会这种方式,所以我就把数据写到文件里,用matlab读出来,然后用matlab各种高级功能查看数据的值。
1、将Mat的数据写入指定文件
为了方便拿来主义者,我直接把这个函数贴出来,你只要把代码拷贝到自己的代码里,就可以直接用了。如果有问题,赶紧评论,我会尽快看看问题出在哪里。
#include <iostream>
#include <fstream>
#include <string>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int abWrite(const Mat &im, const string &fname)
{
ofstream ouF;
ouF.open(fname.c_str(), std::ofstream::binary);
if (!ouF)
{
cerr << "failed to open the file : " << fname << endl;
return 0;
}
for (int r = 0; r < im.rows; r++)
{
ouF.write(reinterpret_cast<const char*>(im.ptr(r)), im.cols*im.elemSize