微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

《学习OpenCV》第三章课后题8-a

题目说明:创建一个结构,结构中包含一个整数,一个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",ms->point.x);
    cvWriteInt(fs,ms->point.y);
    cvendWriteStruct(fs);

    //写入rect结构体
    cvstartWriteStruct(fs,"CvRect",ms->rect.x);
    cvWriteInt(fs,ms->rect.y);
    cvWriteInt(fs,ms->rect.height);
    cvWriteInt(fs,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,"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,"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,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",CV_STORAGE_READ );
    MyStruct struct_2;
    //cvGetFileNodeByName函数通过名字找到存储区fs最上方的根节点数据
    CvFileNode *pnode = cvGetFileNodeByName(fs,"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://www.jb51.cc/article/p-desddfvk-zh.html
http://www.docs.opencv.org/index.html

原文地址:https://www.jb51.cc/xml/296513.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。