OpenCv的xml读写(opencv教程大全)

前端之家收集整理的这篇文章主要介绍了OpenCv的xml读写(opencv教程大全)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

opencv教程专栏:http://blog.csdn.net/Augusdi/article/category/747412

  1. intsub_test_opencv_xml_write(void)
  2. {
  3. //创建文件存储对象
  4. CvFileStorage*fs=cvOpenFileStorage("test.xml",CV_STORAGE_WRITE);
  5. //写注释
  6. cvWriteComment(fs,"测试写XML文件",1);
  7. //开始写结构,类型是图map,也就是有名字的无序节点集合
  8. cvStartWriteStruct(fs,"Employee",CV_NODE_MAP);
  9. //注释
  10. cvWriteComment(fs,"MAP类型,姓名,年龄,薪水",1);
  11. //姓名
  12. cvWriteString(fs,"name","刘越");
  13. //年龄
  14. cvWriteInt(fs,"age",18);
  15. //薪水
  16. cvWriteReal(fs,"salary",2780.3);
  17. //销售次数
  18. "sales_count",4);
  19. //销售具体数据
  20. intsales_record[]={30000,4200,50090};
  21. //注释
  22. "SEQ类型,销售记录",0); background-color:inherit">//开始写结构,类型是序列sequence,无名字的有序节点集合
  23. cvStartWriteStruct(fs,"sales_record",CV_NODE_SEQ);
  24. //前3条销售记录
  25. cvWriteRawData(fs,sales_record,3,"i");
  26. //第4条销售记录,注意无名字
  27. cvWriteInt(fs,6100);
  28. //结束
  29. cvEndWriteStruct(fs);
  30. }
  31. "MAP类型,亲友",0); background-color:inherit">//开始
  32. "Parent",CV_NODE_MAP);
  33. //父亲
  34. cvWriteString(fs,"father","杨舜");
  35. //母亲
  36. "mother","王娟");
  37. //结束
  38. cvEndWriteStruct(fs);
  39. //释放文件存储对象
  40. cvReleaseFileStorage(&fs);
  41. }
  42. intsub_test_opencv_xml_read(void)
  43. {
  44. //文件节点
  45. CvFileNode*node,*node2;
  46. char*str;
  47. intcount;
  48. int*d;
  49. //cve_dm.debug_break();
  50. //打开XML文件
  51. //获得第一层数据节点
  52. node=cvGetFileNodeByName(fs,"Employee");
  53. str=cvReadStringByName(fs,node,"name");
  54. printf("\n姓名=%s",str);
  55. printf("\n年龄=%d",cvReadIntByName(fs,"age"));
  56. printf("\n薪水=%g",cvReadRealByName(fs,"salary"));
  57. count=cvReadIntByName(fs,"sales_count");
  58. printf("\n销售%d条",count);
  59. d=cvAlloc(sizeof(int)*count);
  60. if(d)
  61. inti;
  62. node2=cvGetFileNodeByName(fs,"sales_record");
  63. if(node2)
  64. 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销售记录=");
  65. for(i=0;i<count;i++)
  66. printf("%d,",d[i]);
  67. cvFree(&d);
  68. //获得第二层节点
  69. "Parent");
  70. printf("\n根节点=%s",cvGetFileNodeName(node));
  71. str=cvReadStringByName(fs,"father");
  72. printf("\n父亲=%s",str);
  73. "mother");
  74. printf("\n母亲=%s",248)"> }

1.写XMl文件,

[html] view plain copy
    voidCrecognitionDlg::storeDirectoryFaces(){
  1. CvFileStorage*fileStorage;
  2. fileStorage=cvOpenFileStorage("directoryInfo.xml",CV_STORAGE_WRITE);
  3. cvWriteInt(fileStorage,"nFaces",indexFaces.size());
  4. cvStartWriteStruct(fileStorage,"CVFaceRecog",CV_NODE_MAP);
  5. for(size_ti=0;i<indexFaces.size();i++)
  6. charperson[100];
  7. sprintf(person,"person_%d",(i+1));//必须区分开,否则读的时候会出问题
  8. cvStartWriteStruct(fileStorage,person,CV_NODE_MAP);
  9. cvWriteInt(fileStorage,"index",indexFaces.at(i));
  10. cvWriteString(fileStorage,"name",namePerson.at(i));
  11. cvWriteString(fileStorage,"directory",pathFaces.at(i));
  12. cvEndWriteStruct(fileStorage);
  13. cvReleaseFileStorage(&fileStorage);
  14. }

写完的内容如下:

copy
    <?xmlversion="1.0"?>
  1. opencv_storage>
  2. nFaces>3</>
  3. CVFaceRecogperson_1index>0name>aaadirectory>C:\Pictures\kobe>person_2>1>bbb>C:\Pictures\Liperson_3>2>ccc>C:\Pictures\Sun>

2.读XML

[cpp] copy
    intCrecognitionDlg::loadDirectoryFaces(){
  1. CvFileStorage*fileStorage=NULL;
  2. inti;
  3. CvSeq*seq;
  4. CvSeqReaderreader;
  5. fileStorage=cvOpenFileStorage("directoryInfo.xml",108); list-style:decimal-leading-zero outside; color:inherit; line-height:20px; margin:0px!important; padding:0px 3px 0px 10px!important"> if(!fileStorage){
  6. return0;
  7. namePerson.clear();
  8. pathFaces.clear();
  9. indexFaces.clear();
  10. CvFileNode*root=cvGetRootFileNode(fileStorage,0);
  11. CvFileNode*data=cvGetFileNodeByName(fileStorage,root,"CVFaceRecog");
  12. seq=data->data.seq;
  13. cvStartReadSeq(seq,&reader,0);
  14. intnFaces=cvReadIntByName(fileStorage,"nFaces",0);
  15. for(i=0;i<nFaces;i++)
  16. CvFileNode*pt=(CvFileNode*)reader.ptr;
  17. namePerson.push_back(cvReadStringByName(fileStorage,pt,0));
  18. pathFaces.push_back(cvReadStringByName(fileStorage,"directory",0));
  19. indexFaces.push_back(cvReadIntByName(fileStorage,"index",248)"> CV_NEXT_SEQ_ELEM(seq->elem_size,reader);
  20. cvReleaseFileStorage(&fileStorage);
  21. return0;
  22. }
copy
    FileStoragefs("test.yml",FileStorage::WRITE);
  1. fs<<"frameCount"<<5;
  2. time_trawtime;time(&rawtime);
  3. fs<<"calibrationDate"<<asctime(localtime(&rawtime));
  4. MatcameraMatrix=(Mat_<double>(3,3)<<1000,320,1000,240,1);//又一种Mat初始化方式
  5. MatdistCoeffs=(Mat_<double>(5,1)<<0.1,0.01,-0.001,0);
  6. fs<<"cameraMatrix"<<cameraMatrix<<"distCoeffs"<<distCoeffs;
  7. //features为一个大小为3的向量,其中每个元素由随机数x,y和大小为8的uchar数组组成
  8. fs<<"features"<<"[";
  9. for(inti=0;i<3;i++)
  10. intx=rand()%640;
  11. inty=rand()%480;
  12. ucharlbp=rand()%256;
  13. fs<<"{:"<<"x"<<x<<"y"<<y<<"lbp"<<"[:";
  14. intj=0;j<8;j++)
  15. fs<<((lbp>>j)&1);
  16. fs<<"]"<<"}";
  17. fs<<"]";
  18. fs.release();
copy
    }
原文链接:https://www.f2er.com/xml/294500.html

猜你在找的XML相关文章