题目说明:创建一个结构,结构中包含一个整数,一个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 <highgui.h>
#include <cv.h>
#include <stdio.h>
typedef struct my_struct
{
int i;
CvPoint point;
CvRect rect;
} MyStruct;
void write_my_struct(CvFileStorage * fs, const char* name, my_struct* ms)
{
//开始写数据
cvStartWriteStruct(fs, name,CV_NODE_MAP);
//写入一个 整数
cvStartWriteStruct(fs,"integer",CV_NODE_SEQ);
cvWriteInt(fs,NULL,ms->i);
cvEndWriteStruct(fs);
//写入cvpoint结构
cvStartWriteStruct(fs,"CvPoint",CV_NODE_SEQ);
cvWriteInt(fs,NULL,ms->point.x);
cvWriteInt(fs,NULL,ms->point.y);
cvEndWriteStruct(fs);
//写入rect结构体
cvStartWriteStruct(fs,"CvRect",CV_NODE_SEQ);
cvWriteInt(fs,NULL,ms->rect.x);
cvWriteInt(fs,NULL,ms->rect.y);
cvWriteInt(fs,NULL,ms->rect.height);
cvWriteInt(fs,NULL,ms->rect.width);
cvEndWriteStruct(fs);
//结束写数据
cvEndWriteStruct(fs);
}
void read_my_struct(CvFileStorage* fs, CvFileNode* ms_node, my_struct* ms)
{
// 读第一个整数
// 注意:这里应使用node->data.i的value来读取Integer
int i = cvGetFileNodeByName(fs, ms_node, "integer")->data.i;
ms->i = i;//将读取到的值赋给全局变量
// 读CvPoint结构
CvSeq *s1 = cvGetFileNodeByName(fs, ms_node, "CvPoint")->data.seq;
CvPoint point;
point.x= cvReadInt((CvFileNode*)cvGetSeqElem(s1,0));
point.y= cvReadInt((CvFileNode*)cvGetSeqElem(s1,1));
ms->point = point;//将读取到的值赋给全局变量
// 读取CvRect结构
CvSeq *s2 = cvGetFileNodeByName(fs, ms_node, "CvRect")->data.seq;
CvRect rect;
rect.x=cvReadInt((CvFileNode*)cvGetSeqElem(s2, 0));
rect.y=cvReadInt((CvFileNode*)cvGetSeqElem(s2, 1));
rect.width=cvReadInt((CvFileNode*)cvGetSeqElem(s2, 3));
rect.height=cvReadInt((CvFileNode*)cvGetSeqElem(s2, 2));
ms->rect = rect;//将读取到的值赋给全局变量
}
// 将MyStruct的值显示出来
void ShowStructValue(MyStruct* pvalue)
{
printf("integer:%d\n", pvalue->i);
printf("CvPoint: (%d, %d)\n", pvalue->point.x, pvalue->point.y );
printf("CvRect: h-->%d\tw-->%d\t(%d, %d)\n", pvalue->rect.height,
pvalue->rect.width, pvalue->rect.x, pvalue->rect.y);
}
int main()
{
/* 写数据部分 */
MyStruct struct_1;
struct_1.i = 10;
struct_1.point = cvPoint(50, 145);
struct_1.rect = cvRect(2, 2, 400, 400);
CvFileStorage* fs = cvOpenFileStorage("E:/My_struct.xml", 0, CV_STORAGE_WRITE);
write_my_struct(fs, "my_struct", &struct_1);
cvReleaseFileStorage(&fs);
/* 读数据部分 */
fs = cvOpenFileStorage("E:/My_struct.xml", NULL, CV_STORAGE_READ );
MyStruct struct_2;
//cvGetFileNodeByName函数通过名字找到存储区fs最上方的根节点数据
CvFileNode *pnode = cvGetFileNodeByName(fs, NULL, "my_struct");
read_my_struct( fs, pnode, &struct_2 );
// 显示
printf("---------------- Write -----------------\n");
ShowStructValue( & struct_1 );
printf("---------------- Read -------------------\n");
ShowStructValue( & struct_2);
system("pause");
cvReleaseFileStorage(&fs);
return 0;
}
注意这个函数的含义和用法: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://blog.csdn.net/stk_overflow/article/details/8759540
http://www.docs.opencv.org/index.html