自己整理的,使用OPENCV读写XML文件的数组,不是很完善,欢迎拍砖!
//将图片存为xml文件程序: #include <iostream> #include <string> #include <vector> using namespace std; #include <highgui.h> #include "cxcore.h" struct CatInfo { string Title; int TotalCnt; } ; void my_opencv_xml_write(void);
int main() { struct CatInfo Cats[3];//类别数组 Cats[0].Title= "S01"; Cats[0].TotalCnt=10; Cats[1].Title= "S02"; Cats[1].TotalCnt=20; Cats[2].Title= "S03"; Cats[2].TotalCnt=30;
CvFileStorage* fs = cvOpenFileStorage( "upimg_matched_points.xml", 0, CV_STORAGE_WRITE ); if(!fs) { return -1; } cvStartWriteStruct(fs, "CatInfo", CV_NODE_MAP);//对应于 读取时父节点的子节点 char SN[256];//序号 int max = 3;//类别数组元素个数
for(int i=0; i<max; i++) { sprintf(SN, "%d", i); char CatType[256] = "CatInfo";//初始化 char* Key = strcat(CatType, SN);//拼接关键字,以免重复
cvStartWriteStruct(fs, Key, CV_NODE_MAP, Cats[i].Title.c_str());//子节点的子节点,参数2:【不能重名】 cvWriteString(fs, "Title", Cats[i].Title.c_str()); cvWriteInt(fs, "TotalCnt", Cats[i].TotalCnt); cvEndWriteStruct(fs); } cvEndWriteStruct(fs);
cvReleaseFileStorage(&fs);
/*文件内容 <?xml version="1.0"?> <opencv_storage> <CatInfo> <CTitle1 type_id="S01"> <Title>S01</Title> <TotalCnt>10</TotalCnt> </CTitle1> <CTitle2 type_id="S02"> <Title>S02</Title> <TotalCnt>20</TotalCnt> </CTitle2> </CatInfo> </opencv_storage> */ CvSeq* seq; //节点序列 CvSeqReader reader;//阅读器 fs = cvOpenFileStorage( "upimg_matched_points.xml", 0, CV_STORAGE_READ );//打开文件 CvFileNode* data1 = cvGetRootFileNode( fs, 0);//读出<opencv_storage>节点 CvFileNode* data = cvGetFileNodeByName( fs, data1, "CatInfo" );//读出父节点的子节点 seq = data->data.seq;//读出子节点内所含节点列表 cvStartReadSeq( seq, &reader, 0 );//将阅读器与节点序列头部关联 vector<CatInfo> CatList; int ElemCnt = seq->header_size/seq->elem_size;//列表元素个数
for(i = 0; i < ElemCnt; i++)//如何判断reader指向了最后一个节点??? { //获取阅读器所指节点 CvFileNode *pt=(CvFileNode*)reader.ptr;
CatInfo newCat; newCat.Title = cvReadStringByName(fs, pt, "Title", "unknown"); newCat.TotalCnt = cvReadIntByName(fs, pt, "TotalCnt", 0);
//附加新节点 CatList.push_back(newCat);
//移动到下一个节点。elem_size类似于结构体大小,移动步长。 CV_NEXT_SEQ_ELEM(seq->elem_size, reader); }
cvReleaseFileStorage(&fs);
return 0; }
///下面转发一个函数,有点复杂,留着以后用///
//有关写入复杂内容的函数例子 void my_opencv_xml_write(void) { char pID[] = "0001"; int featureCount = 10; // 具体数据 int _record[]={30000,4200,50090}; //创建文件存储对象 CvFileStorage *fs=cvOpenFileStorage("test.xml",0,CV_STORAGE_WRITE);
// 写注释 cvWriteComment(fs,"Person Feature Analysis NO.0001",1); // 开始写结构,类型是图map,也就是有名字的无序节点集合 cvStartWriteStruct(fs, "Person", CV_NODE_MAP, pID , cvAttrList(NULL,NULL)); //{ //1 cvWriteComment(fs,"Face Feature NO.001",1); // 开始写结构,类型是图map,也就是有名字的无序节点集合 cvStartWriteStruct(fs, "Face_Feature", CV_NODE_MAP, "001", cvAttrList(NULL,NULL)); // { //注释Properties cvWriteComment(fs,"MAPTYPE Properties",1); // 开始写结构,类型是图map,也就是有名字的无序节点集合 cvStartWriteStruct(fs, "Properties", CV_NODE_MAP, NULL, cvAttrList(NULL,NULL)); // 注释 cvWriteComment(fs,"MAPType, name, age",0); // 姓名 cvWriteString(fs,"name","liuyue", 0); // 年龄 cvWriteInt(fs,"age",18); // 特征值 cvWriteInt(fs,"feature_count",featureCount); // 结束Properties cvEndWriteStruct(fs); // } // { // 注释Feature cvWriteComment(fs,"SEQType Feature",1); // 开始写结构,类型是序列sequence,无名字的有序节点集合 cvStartWriteStruct(fs,"Feature",CV_NODE_SEQ, NULL, cvAttrList(NULL,NULL)); // 前3条记录 cvWriteRawData(fs,_record,3,"i"); // 第4条记录,注意无名字 cvWriteInt(fs,0,6100); // 结束Feature cvEndWriteStruct(fs); // } // 结束1 cvEndWriteStruct(fs); // } // { //2 cvWriteComment(fs,"Face Feature NO.002",1); // 开始写结构,类型是图map,也就是有名字的无序节点集合 cvStartWriteStruct(fs, "Face_Feature", CV_NODE_MAP, "002", cvAttrList(NULL,NULL)); // { //注释Properties cvWriteComment(fs,"MAPTYPE Properties",1); // 开始写结构,类型是图map,也就是有名字的无序节点集合 cvStartWriteStruct(fs, "Properties", CV_NODE_MAP, NULL, cvAttrList(NULL,NULL)); // 注释 cvWriteComment(fs,"MAPType, name, age",0); // 姓名 cvWriteString(fs,"name","liuyue", 0); // 年龄 cvWriteInt(fs,"age",18);
// 特征值 cvWriteInt(fs,"feature_count",featureCount); // 结束Properties cvEndWriteStruct(fs); // } // { // 注释Feature cvWriteComment(fs,"SEQType Feature",1); // 开始写结构,类型是序列sequence,无名字的有序节点集合 cvStartWriteStruct(fs,"Feature",CV_NODE_SEQ, NULL, cvAttrList(NULL,NULL)); // 前3条记录 cvWriteRawData(fs,_record,3,"i"); // 第4条记录,注意无名字 cvWriteInt(fs,0,6100); // 结束Feature cvEndWriteStruct(fs); // } // 结束2 cvEndWriteStruct(fs); // }
// 结束 cvEndWriteStruct(fs); // 释放文件存储对象 cvReleaseFileStorage(&fs); }