CvFileStorage结构的使用

CvFileStorage结构的使用

 

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. int main(int argc,char** argv)
  5. {
  6. CvFileStorage* fs=cvOpenFileStorage("cfg.xml",0,CV_STORAGE_WRITE);//打开存储文件
  7. cvWriteInt(fs,"frame_count",10);//写入整数型
  8. cvStartWriteStruct(fs,"frame_size",CV_NODE_SEQ);//开始写入新的数据结构
  9. cvWriteInt(fs,0,320);
  10. cvWriteInt(fs,0,200);
  11. cvEndWriteStruct(fs);//结束写入数据结构
  12. cvReleaseFileStorage(&fs);//释放存储的数据
  13. return 0;
  14. }

运行结果:

  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. int main(int argc,char** argv)
  6. {
  7. CvMat* mat=cvCreateMat(3,3,CV_32F);//创建一个矩阵
  8. CvFileStorage* fs=cvOpenFileStorage("example.xml",0,CV_STORAGE_WRITE);//打开文件,用来存储
  9. cvSetIdentity(mat);
  10. cvWrite(fs,"A",mat,cvAttrList(0,0));//写入一个对象,如CvMat
  11. cvReleaseFileStorage(&fs);//释放文件
  12. cvReleaseMat(&mat);//释放矩阵空间
  13. return 0;
  14. }

运行结果:

  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. CvFileStorage* fs=cvOpenFileStorage("cfgFinal.xml",0,CV_STORAGE_WRITE);//打开存储文件
  3. cvWriteInt(fs,"frame_count",10);//写入整数型
  4. cvStartWriteStruct(fs,"frame_size",CV_NODE_SEQ);//开始写入新的数据结构
  5. cvWriteInt(fs,0,320);
  6. cvWriteInt(fs,0,200);
  7. cvEndWriteStruct(fs);//结束写入数据结构
  8. CvMat* mat=cvCreateMat(3,3,CV_32F);//创建一个矩阵
  9. cvSetIdentity(mat);
  10. cvWrite(fs,"color_cvt_matrix",mat,cvAttrList(0,0));//写入一个对象,如CvMat
  11. cvReleaseFileStorage(&fs);//释放存储的数据
  12. 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
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值