本文主要是介绍CvFileStorage结构的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
CvFileStorage结构的使用
1.CvFileStorage结构
- //3-16
CvFileStorage结构,数据通过CxCore数据存储函数访问 - typedef
struct CvFileStorage - {
-
... //hidden fields - }CvFileStorage;
2.写入简单的数据和结构
- #include
"stdafx.h" - #include
<cv.h> - #include
<highgui.h> -
- int
main( intargc, 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( intargc, 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>
5.从硬盘读取xml文件
- CvFileStorage*
fs=cvOpenFileStorage( "cfgFinal.xml",0,CV_STORAGE_READ); - int
frame_count=cvReadIntByName(fs,0, "frame_count",5); - printf("frame_count:%d\n",frame_count);
- CvSeq*
s=cvGetFileNodeByName(fs,0,"frame_size")->data.seq; - int
frame_width=cvReadInt((CvFileNode*)cvGetSeqElem(s,0)); - printf("frame_width:%d\n",frame_width);
- int
frame_height=cvReadInt((CvFileNode*)cvGetSeqElem(s,1)); - printf("frame_height:%d\n",frame_height);
- CvMat*
color_cvt_matrix=(CvMat*)cvReadByName(fs,0,"color_cvt_matrix"); - printf("矩阵color_cvt_matrix的元素(0,0)=%f\n",CV_MAT_ELEM(*color_cvt_matrix,float,0,0));//取,float型,原数为float型1
- printf("矩阵color_cvt_matrix的元素(0,1)=%f\n",CV_MAT_ELEM(*color_cvt_matrix,int,0,1));//取,int型,原数为float型0
- printf("矩阵color_cvt_matrix的元素(0,2)=%f\n",CV_MAT_ELEM(*color_cvt_matrix,double,0,2));//取,double型,原数为float型0
- cvReleaseFileStorage(&fs);
运行结果:
- frame_count:10
- frame_width:320
- frame_height:200
- 矩阵color_cvt_matrix的元素(0,0)float型=1.000000
- 矩阵color_cvt_matrix的元素(0,1)int型=-298429229800261920000000
000000000000000000000 - 000000000000000000000000
000000000000000000000000 000000000000000000000000 00000000 - 000000000000000000000000
00000000000000.000000 - 矩阵color_cvt_matrix的元素(0,2)double型=0.000000
这篇关于CvFileStorage结构的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!