作者:gnuhpc
出處:http://www.cnblogs.com/gnuhpc/
#include "highgui.h"
#include "cv.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main(void)
{
int a=1;
float b=2.;
double c[]={4.5,6.7,8.9};
CvMat *mat=cvCreateMat(3,3,CV_32SC1);
cvSetIdentity(mat);//初始化這個矩陣
CvFileStorage *fs=cvOpenFileStorage("test.xml",0,CV_STORAGE_WRITE);
cvWriteComment(fs,"my data",1);
//開始寫數據
cvStartWriteStruct(fs,"DATA",CV_NODE_MAP,0,cvAttrList(0,0));
//寫入數據c數組
cvStartWriteStruct(fs,"c",CV_NODE_SEQ,0,cvAttrList(0,0));
cvWriteRawData(fs,c,3,"d");
cvEndWriteStruct(fs);
//寫入單位矩陣mat
cvSave("mat.xml",mat);
//寫入整型數據a
cvWriteInt(fs,"a",a);
//寫入浮點型數據b
cvWriteReal(fs,"b",b);
cvStartWriteStruct(fs,"c",CV_NODE_SEQ,0,cvAttrList(0,0));
cvWriteRawData(fs,c,3,"d");
cvEndWriteStruct(fs);
cvEndWriteStruct(fs);
cvReleaseFileStorage(&fs);
cvReleaseMat(&mat);
/**********讀取XML***********/
int i,j;
CvMat *readbyname,*readbynode;
CvFileNode *mat_node;//文件結點
CvFileStorage *fs_read=NULL;//文件存儲
fs_read=cvOpenFileStorage("mat.xml",0,CV_STORAGE_READ);
readbyname = (CvMat *)cvReadByName(fs_read,NULL,"mat",NULL);
mat_node = cvGetFileNodeByName(fs_read,NULL,"mat");
readbynode = (CvMat *)cvRead(fs_read,mat_node);
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
cout<<CV_MAT_ELEM(*readbynode,int,i,j);
}
cout<<endl;
}
cvReleaseFileStorage(&fs_read);
cvReleaseMat(&readbynode);
cvReleaseMat(&readbyname);
return 0;
}
