opencv教程专栏:http://blog.csdn.net/Augusdi/article/category/747412
- int sub_test_opencv_xml_write(void)
- {
- // 创建文件存储对象
- CvFileStorage *fs=cvOpenFileStorage("test.xml",0,CV_STORAGE_WRITE);
- // 写注释
- cvWriteComment(fs,"测试写XML文件",1);
- // 开始写结构,类型是图map,也就是有名字的无序节点集合
- cvStartWriteStruct(fs,"Employee",CV_NODE_MAP);
- // 注释
- cvWriteComment(fs,"MAP类型,姓名,年龄,薪水",1);
- // 姓名
- cvWriteString(fs,"name","刘越");
- // 年龄
- cvWriteInt(fs,"age",18);
- // 薪水
- cvWriteReal(fs,"salary",2780.3);
- // 销售次数
- cvWriteInt(fs,"sales_count",4);
- {
- // 销售具体数据
- int sales_record[]={30000,4200,50090};
- // 注释
- cvWriteComment(fs,"SEQ类型,销售记录",1);
- // 开始写结构,类型是序列sequence,无名字的有序节点集合
- cvStartWriteStruct(fs,"sales_record",CV_NODE_SEQ);
- // 前3条销售记录
- cvWriteRawData(fs,sales_record,3,"i");
- // 第4条销售记录,注意无名字
- cvWriteInt(fs,0,6100);
- // 结束
- cvEndWriteStruct(fs);
- }
- {
- // 注释
- cvWriteComment(fs,"MAP类型,亲友",1);
- // 开始
- cvStartWriteStruct(fs,"Parent",CV_NODE_MAP);
- // 父亲
- cvWriteString(fs,"father","杨舜");
- // 母亲
- cvWriteString(fs,"mother","王娟");
- // 结束
- cvEndWriteStruct(fs);
- }
- // 结束
- cvEndWriteStruct(fs);
- // 释放文件存储对象
- cvReleaseFileStorage(&fs);
- }
- int sub_test_opencv_xml_read(void)
- {
- // 文件节点
- CvFileNode * node, *node2;
- char * str;
- int count;
- int * d;
- //cve_dm.debug_break();
- // 打开XML文件
- CvFileStorage *fs = cvOpenFileStorage("test.xml",0,CV_STORAGE_READ);
- // 获得第一层数据节点
- node = cvGetFileNodeByName(fs,0,"Employee");
- str = cvReadStringByName(fs,node,"name");
- printf("\n姓名=%s",str);
- printf("\n年龄=%d",cvReadIntByName(fs,node,"age"));
- printf("\n薪水=%g",cvReadRealByName(fs,node,"salary"));
- count = cvReadIntByName(fs,node,"sales_count");
- printf("\n销售%d条",count);
- d = cvAlloc(sizeof(int)*count);
- if(d)
- {
- int i;
- node2 = cvGetFileNodeByName(fs,node,"sales_record");
- if(node2)
- {
- cvReadRawData(fs,node2,d,"i");
- printf("\n销售记录=");
- for(i=0;i<count;i++)
- printf("%d, ",d[i]);
- }
- cvFree(&d);
- }
- // 获得第二层节点
- node = cvGetFileNodeByName(fs,node,"Parent");
- printf("\n根节点=%s",cvGetFileNodeName(node));
- str = cvReadStringByName(fs,node,"father");
- printf("\n父亲=%s",str);
- str = cvReadStringByName(fs,node,"mother");
- printf("\n母亲=%s",str);
- }
1.写XMl文件,
- void CrecognitionDlg::storeDirectoryFaces(){
- CvFileStorage * fileStorage;
- fileStorage = cvOpenFileStorage( "directoryInfo.xml", 0, CV_STORAGE_WRITE );
- cvWriteInt( fileStorage, "nFaces", indexFaces.size() );
- cvStartWriteStruct(fileStorage, "CVFaceRecog", CV_NODE_MAP);
- for (size_t i=0;i<indexFaces.size();i++)
- {
- char person[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);
- }
- cvEndWriteStruct(fileStorage);
- cvReleaseFileStorage( &fileStorage );
- }
写完的内容如下:
- <?xml version="1.0"?>
- <opencv_storage>
- <nFaces>3</nFaces>
- <CVFaceRecog>
- <person_1>
- <index>0</index>
- <name>aaa</name>
- <directory>C:\Pictures\kobe</directory></person_1>
- <person_2>
- <index>1</index>
- <name>bbb</name>
- <directory>C:\Pictures\Li</directory></person_2>
- <person_3>
- <index>2</index>
- <name>ccc</name>
- <directory>C:\Pictures\Sun</directory></person_3></CVFaceRecog>
- </opencv_storage>
2.读XML
- int CrecognitionDlg::loadDirectoryFaces(){
- CvFileStorage * fileStorage = NULL;
- int i;
- CvSeq* seq;
- CvSeqReader reader;
- fileStorage = cvOpenFileStorage( "directoryInfo.xml", 0, CV_STORAGE_READ );
- if( !fileStorage ) {
- return 0;
- }
- namePerson.clear();
- pathFaces.clear();
- indexFaces.clear();
- CvFileNode* root = cvGetRootFileNode( fileStorage, 0);
- CvFileNode* data = cvGetFileNodeByName( fileStorage, root, "CVFaceRecog" );
- seq = data->data.seq;
- cvStartReadSeq( seq, &reader, 0 );
- int nFaces = cvReadIntByName( fileStorage, 0, "nFaces", 0 );
- for(i = 0; i < nFaces; i++)
- {
- CvFileNode *pt = (CvFileNode*)reader.ptr;
- namePerson.push_back(cvReadStringByName(fileStorage, pt, "name", 0));
- pathFaces.push_back(cvReadStringByName(fileStorage, pt, "directory", 0));
- indexFaces.push_back(cvReadIntByName(fileStorage,pt,"index",0));
- CV_NEXT_SEQ_ELEM(seq->elem_size, reader);
- }
- cvReleaseFileStorage( &fileStorage );
- return 0;
- }
- FileStorage fs("test.yml", FileStorage::WRITE);
- fs << "frameCount" << 5;
- time_t rawtime; time(&rawtime);
- fs << "calibrationDate" << asctime(localtime(&rawtime));
- Mat cameraMatrix = (Mat_<double>(3,3) << 1000, 0, 320, 0, 1000, 240, 0, 0, 1); //又一种Mat初始化方式
- Mat distCoeffs = (Mat_<double>(5,1) << 0.1, 0.01, -0.001, 0, 0);
- fs << "cameraMatrix" << cameraMatrix << "distCoeffs" << distCoeffs;
- //features为一个大小为3的向量,其中每个元素由随机数x,y和大小为8的uchar数组组成
- fs << "features" << "[";
- for( int i = 0; i < 3; i++ )
- {
- int x = rand() % 640;
- int y = rand() % 480;
- uchar lbp = rand() % 256;
- fs << "{:" << "x" << x << "y" << y << "lbp" << "[:";
- for( int j = 0; j < 8; j++ )
- fs << ((lbp >> j) & 1);
- fs << "]" << "}";
- }
- fs << "]";
- fs.release();
- int sub_test_opencv_xml_write(void)
- {
- // 创建文件存储对象
- CvFileStorage *fs=cvOpenFileStorage("test.xml",0,CV_STORAGE_WRITE);
- // 写注释
- cvWriteComment(fs,"测试写XML文件",1);
- // 开始写结构,类型是图map,也就是有名字的无序节点集合
- cvStartWriteStruct(fs,"Employee",CV_NODE_MAP);
- // 注释
- cvWriteComment(fs,"MAP类型,姓名,年龄,薪水",1);
- // 姓名
- cvWriteString(fs,"name","刘越");
- // 年龄
- cvWriteInt(fs,"age",18);
- // 薪水
- cvWriteReal(fs,"salary",2780.3);
- // 销售次数
- cvWriteInt(fs,"sales_count",4);
- {
- // 销售具体数据
- int sales_record[]={30000,4200,50090};
- // 注释
- cvWriteComment(fs,"SEQ类型,销售记录",1);
- // 开始写结构,类型是序列sequence,无名字的有序节点集合
- cvStartWriteStruct(fs,"sales_record",CV_NODE_SEQ);
- // 前3条销售记录
- cvWriteRawData(fs,sales_record,3,"i");
- // 第4条销售记录,注意无名字
- cvWriteInt(fs,0,6100);
- // 结束
- cvEndWriteStruct(fs);
- }
- {
- // 注释
- cvWriteComment(fs,"MAP类型,亲友",1);
- // 开始
- cvStartWriteStruct(fs,"Parent",CV_NODE_MAP);
- // 父亲
- cvWriteString(fs,"father","杨舜");
- // 母亲
- cvWriteString(fs,"mother","王娟");
- // 结束
- cvEndWriteStruct(fs);
- }
- // 结束
- cvEndWriteStruct(fs);
- // 释放文件存储对象
- cvReleaseFileStorage(&fs);
- }
- int sub_test_opencv_xml_read(void)
- {
- // 文件节点
- CvFileNode * node, *node2;
- char * str;
- int count;
- int * d;
- //cve_dm.debug_break();
- // 打开XML文件
- CvFileStorage *fs = cvOpenFileStorage("test.xml",0,CV_STORAGE_READ);
- // 获得第一层数据节点
- node = cvGetFileNodeByName(fs,0,"Employee");
- str = cvReadStringByName(fs,node,"name");
- printf("\n姓名=%s",str);
- printf("\n年龄=%d",cvReadIntByName(fs,node,"age"));
- printf("\n薪水=%g",cvReadRealByName(fs,node,"salary"));
- count = cvReadIntByName(fs,node,"sales_count");
- printf("\n销售%d条",count);
- d = cvAlloc(sizeof(int)*count);
- if(d)
- {
- int i;
- node2 = cvGetFileNodeByName(fs,node,"sales_record");
- if(node2)
- {
- cvReadRawData(fs,node2,d,"i");
- printf("\n销售记录=");
- for(i=0;i<count;i++)
- printf("%d, ",d[i]);
- }
- cvFree(&d);
- }
- // 获得第二层节点
- node = cvGetFileNodeByName(fs,node,"Parent");
- printf("\n根节点=%s",cvGetFileNodeName(node));
- str = cvReadStringByName(fs,node,"father");
- printf("\n父亲=%s",str);
- str = cvReadStringByName(fs,node,"mother");
- printf("\n母亲=%s",str);
- }