前端之家收集整理的这篇文章主要介绍了
OpenCv的xml读写(opencv教程大全),
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
opencv教程专栏:http://blog.csdn.net/Augusdi/article/category/747412
- intsub_test_opencv_xml_write(void)
- {
-
- CvFileStorage*fs=cvOpenFileStorage("test.xml",CV_STORAGE_WRITE);
- //写注释
- cvWriteComment(fs,"测试写XML文件",1);
-
-
- cvStartWriteStruct(fs,"Employee",CV_NODE_MAP);
- //注释
- cvWriteComment(fs,"MAP类型,姓名,年龄,薪水",1);
- //姓名
- cvWriteString(fs,"name","刘越");
- //年龄
- cvWriteInt(fs,"age",18);
- //薪水
- cvWriteReal(fs,"salary",2780.3);
- //销售次数
- "sales_count",4);
- //销售具体数据
- intsales_record[]={30000,4200,50090};
- //注释
- "SEQ类型,销售记录",0); background-color:inherit">//开始写结构,类型是序列sequence,无名字的有序节点集合
- cvStartWriteStruct(fs,"sales_record",CV_NODE_SEQ);
- //前3条销售记录
- cvWriteRawData(fs,sales_record,3,"i");
- //第4条销售记录,注意无名字
- cvWriteInt(fs,6100);
- //结束
- cvEndWriteStruct(fs);
- }
- "MAP类型,亲友",0); background-color:inherit">//开始
- "Parent",CV_NODE_MAP);
- //父亲
- cvWriteString(fs,"father","杨舜");
- //母亲
- "mother","王娟");
- //结束
- cvEndWriteStruct(fs);
- //释放文件存储对象
- cvReleaseFileStorage(&fs);
- }
- intsub_test_opencv_xml_read(void)
- {
- //文件节点
- CvFileNode*node,*node2;
- char*str;
- intcount;
- int*d;
- //cve_dm.debug_break();
- //打开XML文件
- //获得第一层数据节点
- node=cvGetFileNodeByName(fs,"Employee");
- str=cvReadStringByName(fs,node,"name");
- printf("\n姓名=%s",str);
- printf("\n年龄=%d",cvReadIntByName(fs,"age"));
- printf("\n薪水=%g",cvReadRealByName(fs,"salary"));
- count=cvReadIntByName(fs,"sales_count");
- printf("\n销售%d条",count);
- d=cvAlloc(sizeof(int)*count);
- if(d)
- inti;
- node2=cvGetFileNodeByName(fs,"sales_record");
- if(node2)
- cvReadRawData(fs,node2,d,108); list-style:decimal-leading-zero outside; color:inherit; line-height:20px; margin:0px!important; padding:0px 3px 0px 10px!important"> printf("\n销售记录=");
- for(i=0;i<count;i++)
- printf("%d,",d[i]);
- cvFree(&d);
- //获得第二层节点
- "Parent");
- printf("\n根节点=%s",cvGetFileNodeName(node));
- str=cvReadStringByName(fs,"father");
- printf("\n父亲=%s",str);
- "mother");
- printf("\n母亲=%s",248)"> }
1.写XMl文件,
voidCrecognitionDlg::storeDirectoryFaces(){
- CvFileStorage*fileStorage;
- fileStorage=cvOpenFileStorage("directoryInfo.xml",CV_STORAGE_WRITE);
- cvWriteInt(fileStorage,"nFaces",indexFaces.size());
- cvStartWriteStruct(fileStorage,"CVFaceRecog",CV_NODE_MAP);
- for(size_ti=0;i<indexFaces.size();i++)
- charperson[100];
- sprintf(person,"person_%d",(i+1));//必须区分开,否则读的时候会出问题
- cvStartWriteStruct(fileStorage,person,CV_NODE_MAP);
- cvWriteInt(fileStorage,"index",indexFaces.at(i));
- cvWriteString(fileStorage,"name",namePerson.at(i));
- cvWriteString(fileStorage,"directory",pathFaces.at(i));
- cvEndWriteStruct(fileStorage);
- cvReleaseFileStorage(&fileStorage);
- }
写完的内容如下: