CvFileStorage结构

来源:互联网 发布:淘宝同城件送货上门 编辑:程序博客网 时间:2024/05/21 11:22

CvFileStorage结构的使用

 (2010-12-06 22:04:54)
转载
标签: 

cpp

分类: 学习

1.CvFileStorage结构

  1. //3-16   CvFileStorage结构,数据通过CxCore数据存储函数访问  
  2. typedef struct CvFileStorage  
  3.  
  4.     ...  //hidden fields  
  5. }CvFileStorage;  

2.写入简单的数据和结构

  1. #include "stdafx.h"  
  2. #include <cv.h>  
  3. #include <highgui.h>  
  4.   
  5. int main(int argc,char** argv)  
  6.  
  7.     CvFileStorage* fs=cvOpenFileStorage("cfg.xml",0,CV_STORAGE_WRITE);//打开存储文件  
  8.     cvWriteInt(fs,"frame_count",10);//写入整数型  
  9.     cvStartWriteStruct(fs,"frame_size",CV_NODE_SEQ);//开始写入新的数据结构  
  10.     cvWriteInt(fs,0,320);  
  11.     cvWriteInt(fs,0,200);  
  12.     cvEndWriteStruct(fs);//结束写入数据结构  
  13.     cvReleaseFileStorage(&fs);//释放存储的数据  
  14.     return 0;  
  15.  

运行结果:

  1. <?xml version="1.0"?>  
  2. <opencv_storage>  
  3. <frame_count>10</frame_count>  
  4. <frame_size>  
  5.   320 200</frame_size>  
  6. </opencv_storage>  

3.写入一个对象

  1. #include "stdafx.h"  
  2. #include <cv.h>  
  3. #include <highgui.h>  
  4. #include <cxcore.h>  
  5.   
  6. int main(int argc,char** argv)  
  7.  
  8.     CvMat* mat=cvCreateMat(3,3,CV_32F);//创建一个矩阵  
  9.     CvFileStorage* fs=cvOpenFileStorage("example.xml",0,CV_STORAGE_WRITE);//打开文件,用来存储  
  10.   
  11.     cvSetIdentity(mat);  
  12.     cvWrite(fs,"A",mat,cvAttrList(0,0));//写入一个对象,如CvMat  
  13.   
  14.     cvReleaseFileStorage(&fs);//释放文件  
  15.     cvReleaseMat(&mat);//释放矩阵空间  
  16.   
  17.     return 0;  
  18.  

运行结果:

  1. <?xml version="1.0"?>  
  2. <opencv_storage>  
  3. <A type_id="opencv-matrix">  
  4.   <rows>3</rows>  
  5.   <cols>3</cols>  
  6.   <dt>f</dt>  
  7.   <data>  
  8.     1. 0. 0. 0. 1. 0. 0. 0. 1.</data></A>  
  9. </opencv_storage>  

4.二合一

  1. main()里面的内容  
  2.   
  3. CvFileStorage* fs=cvOpenFileStorage("cfgFinal.xml",0,CV_STORAGE_WRITE);//打开存储文件  
  4.       
  5.     cvWriteInt(fs,"frame_count",10);//写入整数型  
  6.     cvStartWriteStruct(fs,"frame_size",CV_NODE_SEQ);//开始写入新的数据结构  
  7.     cvWriteInt(fs,0,320);  
  8.     cvWriteInt(fs,0,200);  
  9.     cvEndWriteStruct(fs);//结束写入数据结构  
  10.   
  11.     CvMat* mat=cvCreateMat(3,3,CV_32F);//创建一个矩阵  
  12.     cvSetIdentity(mat);  
  13.     cvWrite(fs,"color_cvt_matrix",mat,cvAttrList(0,0));//写入一个对象,如CvMat  
  14.   
  15.     cvReleaseFileStorage(&fs);//释放存储的数据  
  16.     cvReleaseMat(&mat);//释放矩阵空间

运行结果:

  1. <?xml version="1.0"?>  
  2. <opencv_storage>  
  3. <frame_count>10</frame_count>  
  4. <frame_size>  
  5.   320 200</frame_size>  
  6. <color_cvt_matrix type_id="opencv-matrix">  
  7.   <rows>3</rows>  
  8.   <cols>3</cols>  
  9.   <dt>f</dt>  
  10.   <data>  
  11.     1. 0. 0. 0. 1. 0. 0. 0. 1.</data></color_cvt_matrix>  
  12. </opencv_storage>  

5.从硬盘读取xml文件

  1. CvFileStorage* fs=cvOpenFileStorage("cfgFinal.xml",0,CV_STORAGE_READ);  
  2. int frame_count=cvReadIntByName(fs,0,"frame_count",5);  
  3. printf("frame_count:%d\n",frame_count);  
  4. CvSeq* s=cvGetFileNodeByName(fs,0,"frame_size")->data.seq;  
  5. int frame_width=cvReadInt((CvFileNode*)cvGetSeqElem(s,0));  
  6. printf("frame_width:%d\n",frame_width);  
  7. int frame_height=cvReadInt((CvFileNode*)cvGetSeqElem(s,1));  
  8. printf("frame_height:%d\n",frame_height);  
  9. CvMat* color_cvt_matrix=(CvMat*)cvReadByName(fs,0,"color_cvt_matrix");  
  10. printf("矩阵color_cvt_matrix的元素(0,0)=%f\n",CV_MAT_ELEM(*color_cvt_matrix,float,0,0));//取,float型,原数为float型1  
  11. printf("矩阵color_cvt_matrix的元素(0,1)=%f\n",CV_MAT_ELEM(*color_cvt_matrix,int,0,1));//取,int型,原数为float型0  
  12. printf("矩阵color_cvt_matrix的元素(0,2)=%f\n",CV_MAT_ELEM(*color_cvt_matrix,double,0,2));//取,double型,原数为float型0  
  13. cvReleaseFileStorage(&fs);  

运行结果:

  1. frame_count:10  
  2. frame_width:320  
  3. frame_height:200  
  4. 矩阵color_cvt_matrix的元素(0,0)float型=1.000000  
  5. 矩阵color_cvt_matrix的元素(0,1)int型=-298429229800261920000000000000000000000000000  
  6. 00000000000000000000000000000000000000000000000000000000000000000000000000000000  
  7. 00000000000000000000000000000000000000.000000  
  8. 矩阵color_cvt_matrix的元素(0,2)double型=0.000000 

笔记:

  • 读取的格式不同,结果也不同
  • 一定注意函数的名称正确,否则很费时费精力

转载: http://blog.csdn.net/ShangYT/archive/2010/07/21/5752349.aspx

原创粉丝点击