OpenCV 读写XML

1.写XMl文件,

void CrecognitionDlg::storeDirectoryFaces(){
CvFileStorage * fileStorage;
fileStorage = cvOpenFileStorage( "directoryInfo.xml",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",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 );
for(i = 0; i < nFaces; i++)
{
CvFileNode *pt = (CvFileNode*)reader.ptr;
namePerson.push_back(cvReadStringByName(fileStorage,pt,0));
pathFaces.push_back(cvReadStringByName(fileStorage,0));
indexFaces.push_back(cvReadIntByName(fileStorage,0));
CV_NEXT_SEQ_ELEM(seq->elem_size,reader);
}
cvReleaseFileStorage( &fileStorage );
return 0;
}


http://blog.sina.com.cn/s/blog_929e35c60100z5tj.html

相关文章

引言 NOKIA 有句著名的广告语:“科技以人为本”。任何技术都是为了满足人的生产生活需要而产生的。具体...
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章...
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章...
http://blog.jobbole.com/79252/ 引言 NOKIA 有句著名的广告语:“科技以人为本”。任何技术都是为了满...
(点击上方公众号,可快速关注) 公众号:smart_android 作者:耿广龙|loonggg 点击“阅读原文”,可查看...
一、xml与xslt 相信所有人对xml都不陌生,其被广泛的应用于数据数据传输、保存与序列化中,是一种极为强...