博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
OpenCv的xml读写(opencv教程大全)
阅读量:4284 次
发布时间:2019-05-27

本文共 6762 字,大约阅读时间需要 22 分钟。

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]   
  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]   
  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]   
  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]   
  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]   
  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. }  
你可能感兴趣的文章
教你利用iframe在网页中显示天气
查看>>
利用Javascript获取当前日期的农历日期
查看>>
利用原生JavaScript获取样式的方式小结
查看>>
PHP制作验证码
查看>>
常用的CSS Hack技术集锦
查看>>
IE 8兼容:X-UA-Compatible的解释
查看>>
关于form.submit()不能提交表单的错误原因
查看>>
初识HTML 5:关于它的三个三
查看>>
Canvas入门(1):绘制矩形、圆、直线、曲线等基本图形
查看>>
Canvas入门(2):图形渐变和图像形变换
查看>>
Canvas入门(3):图像处理和绘制文字
查看>>
《千与千寻》给读者带来了什么?
查看>>
JQuery笔记:JQuery和JavaScript的联系与区别
查看>>
PHP的MySQL扩展:PHP访问MySQL的常用扩展函数
查看>>
PHP实现分页:文本分页和数字分页
查看>>
博客收录集的源代码分享,需要那就快来吧
查看>>
杂谈:HTML 5的消息通知机制
查看>>
Ajax异步请求PHP数据
查看>>
百家搜索:在网站中添加Google、百度等搜索引擎
查看>>
关于坛友的一个布局问题的解答
查看>>