1.CvFileStorage结构
- //3-16
CvFileStorage结构,数据通过CxCore数据存储函数访问 - typedef
struct CvFileStorage - {
-
... //hidden fields - }CvFileStorage;
2.写入简单的数据和结构
- #include
"stdafx.h" - #include
<cv.h> - #include
<highgui.h> -
- int
main(int argc,char** argv) - {
-
CvFileStorage* fs=cvOpenFileStorage("cfg.xml",0,CV_STORAGE_WRITE);//打开存储文件 -
cvWriteInt(fs,"frame_count",10);//写入整数型 -
cvStartWriteStruct(fs,"frame_size",CV_NODE_SEQ);//开始写入新的数据结构 -
cvWriteInt(fs,0,320); -
cvWriteInt(fs,0,200); -
cvEndWriteStruct(fs);//结束写入数据结构 -
cvReleaseFileStorage(&fs);//释放存储的数据 -
return 0; - }
运行结果:
- <?xml
version="1.0"?> - <opencv_storage>
- <frame_count>10</frame_count>
- <frame_size>
-
320 200</frame_size> - </opencv_storage>
3.写入一个对象
- #include
"stdafx.h" - #include
<cv.h> - #include
<highgui.h> - #include
<cxcore.h> -
- int
main(int argc,char** argv) - {
-
CvMat* mat=cvCreateMat(3,3,CV_32F);//创建一个矩阵 -
CvFileStorage* fs=cvOpenFileStorage("example.xml",0,CV_STORAGE_WRITE);//打开文件,用来存储 -
-
cvSetIdentity(mat); -
cvWrite(fs,"A",mat,cvAttrList(0,0));//写入一个对象,如CvMat -
-
cvReleaseFileStorage(&fs);//释放文件 -
cvReleaseMat(&mat);//释放矩阵空间 -
-
return 0; - }
运行结果:
- <?xml
version="1.0"?> - <opencv_storage>
- <A
type_id="opencv-matrix"> -
<rows>3</rows> -
<cols>3</cols> -
<dt>f</dt> -
<data> -
1. 0. 0. 0. 1. 0. 0. 0. 1.</data></A> - </opencv_storage>
4.二合一
- main()里面的内容
-
- CvFileStorage*
fs=cvOpenFileStorage("cfgFinal.xml",0,CV_STORAGE_WRITE);//打开存储文件 -
-
cvWriteInt(fs,"frame_count",10);//写入整数型 -
cvStartWriteStruct(fs,"frame_size",CV_NODE_SEQ);//开始写入新的数据结构 -
cvWriteInt(fs,0,320); -
cvWriteInt(fs,0,200); -
cvEndWriteStruct(fs);//结束写入数据结构 -
-
CvMat* mat=cvCreateMat(3,3,CV_32F);//创建一个矩阵 -
cvSetIdentity(mat); -
cvWrite(fs,"color_cvt_matrix",mat,cvAttrList(0,0));//写入一个对象,如CvMat -
-
cvReleaseFileStorage(&fs);//释放存储的数据 -
cvReleaseMat(&mat);//释放矩阵空间
运行结果:
- <?xml
version="1.0"?> - <opencv_storage>
- <frame_count>10</frame_count>
- <frame_size>
-
320 200</frame_size> - <color_cvt_matrix
type_id="opencv-matrix"> -
<rows>3</rows> -
<cols>3</cols> -
<dt>f</dt> -
<data> -
1. 0. 0. 0. 1. 0. 0. 0. 1.</data></color_cvt_matrix> - </opencv_storage>
在OpenCV程序中,需要保存中间结果的时候常常会使用.xml / .yml文件,opencv2.0之前都是使用C风格的代码,当时读写XML文件分别使用函数cvLoad()和cvSave()。在2.0以后的OpenCV转为支持C++,这一举措大大减少了代码量以及编程时需要考虑的细节。
新版本的OpenCV的C++接口中,imwrite()和imread()只能保存整数数据,且需要以图像格式。当需要保存浮点数据或者XML/YML文件时,之前的C语言接口cvSave()函数已经在C++接口中被删除,代替它的是FileStorage类。这个类非常的方便,封装了很多数据结构的细节,编程的时候可以根据统一的接口对数据结构进行保存。
1. FileStorage类写XML/YML文件
• 新建一个FileStorage对象,以FileStorage::WRITE的方式打开一个文件。
• 使用 << 操作对该文件进行操作。
• 释放该对象,对文件进行关闭。
FileStorage fs("test.yml", FileStorage::WRITE); fs << "frameCount" << 5; time_t rawtime; time(&rawtime); fs << "calibrationDate" << asctime(localtime(&rawtime)); Mat cameraMatrix = (Mat_<double>(3,3) << 1000, 0, 320, 0, 1000, 240, 0, 0, 1); //又一种Mat初始化方式 Mat distCoeffs = (Mat_<double>(5,1) << 0.1, 0.01, -0.001, 0, 0); fs << "cameraMatrix" << cameraMatrix << "distCoeffs" << distCoeffs; //features为一个大小为3的向量,其中每个元素由随机数x,y和大小为8的uchar数组组成 fs << "features" << "["; for( int i = 0; i < 3; i++ ) { int x = rand() % 640; int y = rand() % 480; uchar lbp = rand() % 256; fs << "{:" << "x" << x << "y" << y << "lbp" << "[:"; for( int j = 0; j < 8; j++ ) fs << ((lbp >> j) & 1); fs << "]" << "}"; } fs << "]"; fs.release();
2. FileStorage类读XML/YML文件
FileStorage对存储内容在内存中是以层次的节点组成的,每个节点类型为FileNode,FileNode可以使单个的数值、数组或者一系列FileNode的集合。FileNode又可以看做是一个容器,使用iterator接口可以对该节点内更小单位的内容进行访问,例如访问到上面存储的文件中"features"的内容。步骤与写文件类似:
• 新建FileStorage对象,以FileStorage::READ 方式打开一个已经存在的文件
• 使用FileStorage::operator []()函数对文件进行读取,或者使用FileNode和FileNodeIterator
• 使用FileStorage::release()对文件进行关闭
例子如下:
FileStorage fs("test.yml", FileStorage::READ); //方式一: []操作符 int frameCount = (int)fs["frameCount"]; //方式二: FileNode::operator >>() string date; fs["calibrationDate"] >> date; Mat cameraMatrix2, distCoeffs2; fs["cameraMatrix"] >> cameraMatrix2; fs["distCoeffs"] >> distCoeffs2; //注意FileNodeIterator的使用,似乎只能用一维数组去读取里面所有的数据 FileNode features = fs["features"]; FileNodeIterator it = features.begin(), it_end = features.end(); int idx = 0; std::vector<uchar> lbpval; for( ; it != it_end; ++it, idx++ ) { cout << "feature #" << idx << ": "; cout << "x=" << (int)(*it)["x"] << ", y=" << (int)(*it)["y"] << ", lbp: ("; (*it)["lbp"] >> lbpval; //直接读出一维向量 for( int i = 0; i < (int)lbpval.size(); i++ ) cout << " " << (int)lbpval[i]; cout << ")" << endl; } fs.release();