题目说明:创建一个结构,结构中包含一个整数,一个CvPoint和一个CvRect;称结构为“my_struct”。
a.写两个函数:void write_my_struct(CvFileStorage * fs,const char* name,my_struct* ms)和void read_my_struct(CvFileStorage* fs,CvFileNode* ms_node,my_struct* ms)
#include@H_404_6@ <@H_404_6@highgui.@H_404_6@h>@H_404_6@
#include@H_404_6@ <@H_404_6@cv.@H_404_6@h>@H_404_6@
#include@H_404_6@ <@H_404_6@stdio.@H_404_6@h>@H_404_6@
typedef struct my_struct
{
int i;
CvPoint point;
CvRect rect;
} MyStruct;
void@H_404_6@ write_my_struct(CvFileStorage *@H_404_6@ fs,const char*@H_404_6@ name,my_struct*@H_404_6@ ms)
{
//开始写数据@H_404_6@
cvStartWriteStruct(fs,name,CV_NODE_MAP);
//写入一个 整数@H_404_6@
cvStartWriteStruct(fs,"integer"@H_404_6@,CV_NODE_SEQ);
cvWriteInt(fs,NULL@H_404_6@,ms->@H_404_6@i);
cvEndWriteStruct(fs);
//写入cvpoint结构@H_404_6@
cvStartWriteStruct(fs,"CvPoint"@H_404_6@,ms->@H_404_6@point.@H_404_6@x);
cvWriteInt(fs,ms->@H_404_6@point.@H_404_6@y);
cvEndWriteStruct(fs);
//写入rect结构体@H_404_6@
cvStartWriteStruct(fs,"CvRect"@H_404_6@,ms->@H_404_6@rect.@H_404_6@x);
cvWriteInt(fs,ms->@H_404_6@rect.@H_404_6@y);
cvWriteInt(fs,ms->@H_404_6@rect.@H_404_6@height);
cvWriteInt(fs,ms->@H_404_6@rect.@H_404_6@width);
cvEndWriteStruct(fs);
//结束写数据@H_404_6@
cvEndWriteStruct(fs);
}
void@H_404_6@ read_my_struct(CvFileStorage*@H_404_6@ fs,CvFileNode*@H_404_6@ ms_node,my_struct*@H_404_6@ ms)
{
// 读第一个整数@H_404_6@
// 注意:这里应使用node->data.i的value来读取Integer@H_404_6@
int i =@H_404_6@ cvGetFileNodeByName(fs,ms_node,"integer"@H_404_6@)->@H_404_6@data@H_404_6@.@H_404_6@i;
ms->@H_404_6@i =@H_404_6@ i;//将读取到的值赋给全局变量@H_404_6@
// 读CvPoint结构@H_404_6@
CvSeq *@H_404_6@s1 =@H_404_6@ cvGetFileNodeByName(fs,"CvPoint"@H_404_6@)->@H_404_6@data@H_404_6@.@H_404_6@seq;
CvPoint point;
point.@H_404_6@x=@H_404_6@ cvReadInt((CvFileNode*@H_404_6@)cvGetSeqElem(s1,0@H_404_6@));
point.@H_404_6@y=@H_404_6@ cvReadInt((CvFileNode*@H_404_6@)cvGetSeqElem(s1,1@H_404_6@));
ms->@H_404_6@point =@H_404_6@ point;//将读取到的值赋给全局变量@H_404_6@
// 读取CvRect结构@H_404_6@
CvSeq *@H_404_6@s2 =@H_404_6@ cvGetFileNodeByName(fs,"CvRect"@H_404_6@)->@H_404_6@data@H_404_6@.@H_404_6@seq;
CvRect rect;
rect.@H_404_6@x=@H_404_6@cvReadInt((CvFileNode*@H_404_6@)cvGetSeqElem(s2,0@H_404_6@));
rect.@H_404_6@y=@H_404_6@cvReadInt((CvFileNode*@H_404_6@)cvGetSeqElem(s2,1@H_404_6@));
rect.@H_404_6@width=@H_404_6@cvReadInt((CvFileNode*@H_404_6@)cvGetSeqElem(s2,3@H_404_6@));
rect.@H_404_6@height=@H_404_6@cvReadInt((CvFileNode*@H_404_6@)cvGetSeqElem(s2,2@H_404_6@));
ms->@H_404_6@rect =@H_404_6@ rect;//将读取到的值赋给全局变量@H_404_6@
}
// 将MyStruct的值显示出来@H_404_6@
void@H_404_6@ ShowStructValue(MyStruct*@H_404_6@ pvalue)
{
printf("integer:%d\n"@H_404_6@,pvalue->@H_404_6@i);
printf("CvPoint: (%d,%d)\n"@H_404_6@,pvalue->@H_404_6@point.@H_404_6@x,pvalue->@H_404_6@point.@H_404_6@y );
printf("CvRect: h-->%d\tw-->%d\t(%d,pvalue->@H_404_6@rect.@H_404_6@height,pvalue->@H_404_6@rect.@H_404_6@width,pvalue->@H_404_6@rect.@H_404_6@x,pvalue->@H_404_6@rect.@H_404_6@y);
}
int main()
{
/* 写数据部分 */@H_404_6@
MyStruct struct_1;
struct_1.@H_404_6@i =@H_404_6@ 10@H_404_6@;
struct_1.@H_404_6@point =@H_404_6@ cvPoint(50@H_404_6@,145@H_404_6@);
struct_1.@H_404_6@rect =@H_404_6@ cvRect(2@H_404_6@,2@H_404_6@,400@H_404_6@,400@H_404_6@);
CvFileStorage*@H_404_6@ fs =@H_404_6@ cvOpenFileStorage("E:/My_struct.xml"@H_404_6@,0@H_404_6@,CV_STORAGE_WRITE);
write_my_struct(fs,"my_struct"@H_404_6@,&@H_404_6@struct_1);
cvReleaseFileStorage(&@H_404_6@fs);
/* 读数据部分 */@H_404_6@
fs =@H_404_6@ cvOpenFileStorage("E:/My_struct.xml"@H_404_6@,CV_STORAGE_READ );
MyStruct struct_2;
//cvGetFileNodeByName函数通过名字找到存储区fs最上方的根节点数据@H_404_6@
CvFileNode *@H_404_6@pnode =@H_404_6@ cvGetFileNodeByName(fs,"my_struct"@H_404_6@);
read_my_struct( fs,pnode,&@H_404_6@struct_2 );
// 显示@H_404_6@
printf("---------------- Write -----------------\n"@H_404_6@);
ShowStructValue( &@H_404_6@ struct_1 );
printf("---------------- Read -------------------\n"@H_404_6@);
ShowStructValue( &@H_404_6@ struct_2);
system("pause"@H_404_6@);
cvReleaseFileStorage(&@H_404_6@fs);
return@H_404_6@ 0@H_404_6@;
}@H_403_310@
注意这个函数的含义和用法:GetFileNodeByName
Finds a node in a map or file storage.
C: CvFileNode* cvGetFileNodeByName(const CvFileStorage* fs,const CvFileNode* map,const char* name)
Parameters:
•fs – File storage
•map – The parent map. If it is NULL,the function searches in all the top-level nodes (streams),starting with the first one.
•name – The file node name
The function finds a file node by name. The node is searched either in map or,if the pointer is NULL,among the top-level file storage nodes. Using this function for maps and GetSeqElem() (or sequence reader) for sequences,it is possible to navigate through the file storage. To speed up multiple queries for a certain key (e.g.,in the case of an array of structures) one may use a combination of GetHashedKey() and GetFileNode().
引用 qdsclove的专栏、OpenCV Documentation 2.4.11.0
http://www.jb51.cc/article/p-desddfvk-zh.html
http://www.docs.opencv.org/index.html