本文共 6762 字,大约阅读时间需要 22 分钟。
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);
-
-
- 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);
-
- cvStartWriteStruct(fs,"sales_record",CV_NODE_SEQ);
-
- cvWriteRawData(fs,sales_record,3,"i");
-
- 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;
-
-
-
- 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 distCoeffs = (Mat_<double>(5,1) << 0.1, 0.01, -0.001, 0, 0);
- fs << "cameraMatrix" << cameraMatrix << "distCoeffs" << distCoeffs;
-
-
- 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);
-
-
- 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);
-
- cvStartWriteStruct(fs,"sales_record",CV_NODE_SEQ);
-
- cvWriteRawData(fs,sales_record,3,"i");
-
- 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;
-
-
-
- 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);
- }