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

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

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

1.写XMl文件,

[html]  view plain  copy
  1. void CrecognitionDlg::storeDirectoryFaces(){  
  2. CvFileStorage * fileStorage;  
  3. fileStorage = cvOpenFileStorage( "directoryInfo.xml", 0, 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, "name", namePerson.at(i));  
  13. cvWriteString(fileStorage, "directory", pathFaces.at(i));  
  14. cvEndWriteStruct(fileStorage);  
  15. }  
  16. cvEndWriteStruct(fileStorage);  
  17. cvReleaseFileStorage( &fileStorage );  
  18. }  

写完的内容如下:

[html]  view plain  copy
  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

[cpp]  view plain  copy
  1. int CrecognitionDlg::loadDirectoryFaces(){  
  2. CvFileStorage * fileStorage = NULL;  
  3. int i;  
  4. CvSeq* seq;  
  5. CvSeqReader reader;  
  6. fileStorage = cvOpenFileStorage( "directoryInfo.xml", 0, 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, "nFaces", 0 );  
  18. for(i = 0; i < nFaces; i++)  
  19. {  
  20. CvFileNode *pt = (CvFileNode*)reader.ptr;  
  21. namePerson.push_back(cvReadStringByName(fileStorage, pt, "name", 0));  
  22. pathFaces.push_back(cvReadStringByName(fileStorage, pt, "directory", 0));  
  23. indexFaces.push_back(cvReadIntByName(fileStorage,pt,"index",0));  
  24. CV_NEXT_SEQ_ELEM(seq->elem_size, reader);  
  25. }  
  26. cvReleaseFileStorage( &fileStorage );  
  27. return 0;  
  28. }  
[cpp]  view plain  copy
  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, 0, 320, 0, 1000, 240, 0, 0, 1); //又一种Mat初始化方式  
  6.     Mat distCoeffs = (Mat_<double>(5,1) << 0.1, 0.01, -0.001, 0, 0);  
  7.     fs << "cameraMatrix" << cameraMatrix << "distCoeffs" << distCoeffs;  
  8.       
  9.     //features为一个大小为3的向量,其中每个元素由随机数x,y和大小为8的uchar数组组成  
  10.     fs << "features" << "[";  
  11.     forint i = 0; i < 3; i++ )  
  12.     {  
  13.         int x = rand() % 640;  
  14.         int y = rand() % 480;  
  15.         uchar lbp = rand() % 256;  
  16.         fs << "{:" << "x" << x << "y" << y << "lbp" << "[:";  
  17.         forint j = 0; j < 8; j++ )  
  18.             fs << ((lbp >> j) & 1);  
  19.         fs << "]" << "}";  
  20.     }  
  21.     fs << "]";  
  22.     fs.release();  
[cpp]  view plain  copy
  1. int sub_test_opencv_xml_write(void)  
  2. {  
  3.     // 创建文件存储对象  
  4.     CvFileStorage *fs=cvOpenFileStorage("test.xml",0,CV_STORAGE_WRITE);  
  5.     // 写注释  
  6.     cvWriteComment(fs,"测试写XML文件",1);  
  7.       
  8.     // 开始写结构,类型是图map,也就是有名字的无序节点集合  
  9.     cvStartWriteStruct(fs,"Employee",CV_NODE_MAP);  
  10.     // 注释  
  11.     cvWriteComment(fs,"MAP类型,姓名,年龄,薪水",1);  
  12.     // 姓名  
  13.     cvWriteString(fs,"name","刘越");  
  14.     // 年龄  
  15.     cvWriteInt(fs,"age",18);  
  16.     // 薪水  
  17.     cvWriteReal(fs,"salary",2780.3);  
  18.     // 销售次数  
  19.     cvWriteInt(fs,"sales_count",4);  
  20.     {  
  21.         // 销售具体数据  
  22.         int sales_record[]={30000,4200,50090};  
  23.         // 注释  
  24.         cvWriteComment(fs,"SEQ类型,销售记录",1);  
  25.         // 开始写结构,类型是序列sequence,无名字的有序节点集合  
  26.         cvStartWriteStruct(fs,"sales_record",CV_NODE_SEQ);  
  27.         // 前3条销售记录  
  28.         cvWriteRawData(fs,sales_record,3,"i");  
  29.         // 第4条销售记录,注意无名字  
  30.         cvWriteInt(fs,0,6100);  
  31.         // 结束  
  32.         cvEndWriteStruct(fs);  
  33.     }  
  34.     {  
  35.         // 注释  
  36.         cvWriteComment(fs,"MAP类型,亲友",1);  
  37.         // 开始  
  38.         cvStartWriteStruct(fs,"Parent",CV_NODE_MAP);  
  39.         // 父亲  
  40.         cvWriteString(fs,"father","杨舜");  
  41.         // 母亲  
  42.         cvWriteString(fs,"mother","王娟");  
  43.         // 结束  
  44.         cvEndWriteStruct(fs);  
  45.     }  
  46.     // 结束  
  47.     cvEndWriteStruct(fs);  
  48.     // 释放文件存储对象  
  49.     cvReleaseFileStorage(&fs);  
  50. }  
  51.   
  52. int sub_test_opencv_xml_read(void)  
  53. {  
  54.     // 文件节点  
  55.     CvFileNode * node, *node2;  
  56.     char * str;  
  57.     int count;  
  58.     int * d;  
  59.       
  60.     //cve_dm.debug_break();  
  61.     // 打开XML文件  
  62.     CvFileStorage *fs = cvOpenFileStorage("test.xml",0,CV_STORAGE_READ);  
  63.     // 获得第一层数据节点  
  64.     node = cvGetFileNodeByName(fs,0,"Employee");  
  65.     str = cvReadStringByName(fs,node,"name");  
  66.     printf("\n姓名=%s",str);  
  67.     printf("\n年龄=%d",cvReadIntByName(fs,node,"age"));  
  68.     printf("\n薪水=%g",cvReadRealByName(fs,node,"salary"));  
  69.     count = cvReadIntByName(fs,node,"sales_count");  
  70.     printf("\n销售%d条",count);  
  71.     d = cvAlloc(sizeof(int)*count);  
  72.     if(d)  
  73.     {  
  74.         int i;  
  75.         node2 = cvGetFileNodeByName(fs,node,"sales_record");  
  76.         if(node2)  
  77.         {  
  78.             cvReadRawData(fs,node2,d,"i");  
  79.             printf("\n销售记录=");  
  80.             for(i=0;i<count;i++)  
  81.                 printf("%d, ",d[i]);  
  82.         }  
  83.         cvFree(&d);  
  84.     }  
  85.     // 获得第二层节点  
  86.     node = cvGetFileNodeByName(fs,node,"Parent");  
  87.     printf("\n根节点=%s",cvGetFileNodeName(node));  
  88.     str = cvReadStringByName(fs,node,"father");  
  89.     printf("\n父亲=%s",str);  
  90.     str = cvReadStringByName(fs,node,"mother");  
  91.     printf("\n母亲=%s",str);  
  92. }  

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值