加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 09:03:49 所属栏目:百科 来源:网络整理
导读:题目说明:创建一个结构,结构中包含一个整数,一个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,m

题目说明:创建一个结构,结构中包含一个整数,一个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:%dn",pvalue->i);
    printf("CvPoint: (%d,%d)n",pvalue->point.x,pvalue->point.y );
    printf("CvRect: h-->%dtw-->%dt(%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.52php.cn/article/p-desddfvk-zh.html
http://www.docs.opencv.org/index.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读