OpenCv的xml读写

前端之家收集整理的这篇文章主要介绍了OpenCv的xml读写前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. int sub_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. cvWriteInt(fs,"sales_count",4);
  19. {
  20. // 销售具体数据
  21. int sales_record[]={30000,4200,50090};
  22. // 注释
  23. cvWriteComment(fs,"SEQ类型,销售记录",1);
  24. // 开始写结构,类型是序列sequence,无名字的有序节点集合
  25. cvStartWriteStruct(fs,"sales_record",CV_NODE_SEQ);
  26. // 前3条销售记录
  27. cvWriteRawData(fs,sales_record,3,"i");
  28. // 第4条销售记录,注意无名字
  29. cvWriteInt(fs,6100);
  30. // 结束
  31. cvEndWriteStruct(fs);
  32. }
  33. {
  34. // 注释
  35. cvWriteComment(fs,"MAP类型,亲友",1);
  36. // 开始
  37. cvStartWriteStruct(fs,"Parent",CV_NODE_MAP);
  38. // 父亲
  39. cvWriteString(fs,"father","杨舜");
  40. // 母亲
  41. cvWriteString(fs,"mother","王娟");
  42. // 结束
  43. cvEndWriteStruct(fs);
  44. }
  45. // 结束
  46. cvEndWriteStruct(fs);
  47. // 释放文件存储对象
  48. cvReleaseFileStorage(&fs);
  49. }
  50.  
  51. int sub_test_opencv_xml_read(void)
  52. {
  53. // 文件节点
  54. CvFileNode * node,*node2;
  55. char * str;
  56. int count;
  57. int * d;
  58. //cve_dm.debug_break();
  59. // 打开XML文件
  60. CvFileStorage *fs = cvOpenFileStorage("test.xml",CV_STORAGE_READ);
  61. // 获得第一层数据节点
  62. node = cvGetFileNodeByName(fs,"Employee");
  63. str = cvReadStringByName(fs,node,"name");
  64. printf("\n姓名=%s",str);
  65. printf("\n年龄=%d",cvReadIntByName(fs,"age"));
  66. printf("\n薪水=%g",cvReadRealByName(fs,"salary"));
  67. count = cvReadIntByName(fs,"sales_count");
  68. printf("\n销售%d条",count);
  69. d = cvAlloc(sizeof(int)*count);
  70. if(d)
  71. {
  72. int i;
  73. node2 = cvGetFileNodeByName(fs,"sales_record");
  74. if(node2)
  75. {
  76. cvReadRawData(fs,node2,d,"i");
  77. printf("\n销售记录=");
  78. for(i=0;i<count;i++)
  79. printf("%d,",d[i]);
  80. }
  81. cvFree(&d);
  82. }
  83. // 获得第二层节点
  84. node = cvGetFileNodeByName(fs,"Parent");
  85. printf("\n根节点=%s",cvGetFileNodeName(node));
  86. str = cvReadStringByName(fs,"father");
  87. printf("\n父亲=%s",str);
  88. str = cvReadStringByName(fs,"mother");
  89. printf("\n母亲=%s",str);
  90. }

1.写XMl文件,

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

写完的内容如下:

  1. <?xml version="1.0"?>
  2. <opencv_storage>
  3. <nFaces>3</nFaces>
  4. <CVFaceRecog>
  5. <person_1>
  6. <index>0</index>
  7. <name>aaa</name>
  8. <directory>C:\Pictures\kobe</directory></person_1>
  9. <person_2>
  10. <index>1</index>
  11. <name>bbb</name>
  12. <directory>C:\Pictures\Li</directory></person_2>
  13. <person_3>
  14. <index>2</index>
  15. <name>ccc</name>
  16. <directory>C:\Pictures\Sun</directory></person_3></CVFaceRecog>
  17. </opencv_storage>

2.读XML

  1. int CrecognitionDlg::loadDirectoryFaces(){
  2. CvFileStorage * fileStorage = NULL;
  3. int i;
  4. CvSeq* seq;
  5. CvSeqReader reader;
  6. fileStorage = cvOpenFileStorage( "directoryInfo.xml",CV_STORAGE_READ );
  7. if( !fileStorage ) {
  8. return 0;
  9. }
  10. namePerson.clear();
  11. pathFaces.clear();
  12. indexFaces.clear();
  13. CvFileNode* root = cvGetRootFileNode( fileStorage,0);
  14. CvFileNode* data = cvGetFileNodeByName( fileStorage,root,"CVFaceRecog" );
  15. seq = data->data.seq;
  16. cvStartReadSeq( seq,&reader,0 );
  17. int nFaces = cvReadIntByName( fileStorage,0 );
  18. for(i = 0; i < nFaces; i++)
  19. {
  20. CvFileNode *pt = (CvFileNode*)reader.ptr;
  21. namePerson.push_back(cvReadStringByName(fileStorage,pt,0));
  22. pathFaces.push_back(cvReadStringByName(fileStorage,0));
  23. indexFaces.push_back(cvReadIntByName(fileStorage,0));
  24. CV_NEXT_SEQ_ELEM(seq->elem_size,reader);
  25. }
  26. cvReleaseFileStorage( &fileStorage );
  27. return 0;
  28. }
  1. FileStorage fs("test.yml",FileStorage::WRITE);
  2. fs << "frameCount" << 5;
  3. time_t rawtime; time(&rawtime);
  4. fs << "calibrationDate" << asctime(localtime(&rawtime));
  5. Mat cameraMatrix = (Mat_<double>(3,3) << 1000,320,1000,240,1); //又一种Mat初始化方式
  6. Mat distCoeffs = (Mat_<double>(5,1) << 0.1,0.01,-0.001,0);
  7. fs << "cameraMatrix" << cameraMatrix << "distCoeffs" << distCoeffs;
  8. //features为一个大小为3的向量,其中每个元素由随机数x,y和大小为8的uchar数组组成
  9. fs << "features" << "[";
  10. for( int i = 0; i < 3; i++ )
  11. {
  12. int x = rand() % 640;
  13. int y = rand() % 480;
  14. uchar lbp = rand() % 256;
  15. fs << "{:" << "x" << x << "y" << y << "lbp" << "[:";
  16. for( int j = 0; j < 8; j++ )
  17. fs << ((lbp >> j) & 1);
  18. fs << "]" << "}";
  19. }
  20. fs << "]";
  21. fs.release();
  1. int sub_test_opencv_xml_write(void)
  2. {
  3. // 创建文件存储对象
  4. CvFileStorage *fs=cvOpenFileStorage("test.xml",str);
  5. }

猜你在找的XML相关文章