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

前端之家收集整理的这篇文章主要介绍了《学习OpenCV》第三章课后题8-b前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

题目说明:创建一个结构,结构中包含一个整数,一个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)
b.创建一个元素为my_struct结构体且长度为10的数组,并将数组写入磁盘和从磁盘读入内存。

#include <highgui.h>
#include <cv.h>
#include <stdio.h>

#define ARRAY_LENGTH 10    // 数组长度

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);
}

// 检查两个MyStruct是否一致
bool check(MyStruct* msValue1,MyStruct* msValue2)
{
    if( (msValue1->i == msValue2->i) && 
        (msValue1->point.x == msValue2->point.x) &&
        (msValue1->point.y == msValue2->point.y) && 
        (msValue1->rect.height == msValue2->rect.height) && 
        (msValue1->rect.width == msValue2->rect.width) && 
        (msValue1->rect.x == msValue2->rect.x) && 
        (msValue1->rect.y == msValue2->rect.y) )
        return true;
    else 
        return false;
}

int main()
{
    /* 写数据部分 */
    MyStruct msArray[ARRAY_LENGTH]; CvFileStorage* fs = cvOpenFileStorage("My_struct.xml",CV_STORAGE_WRITE); char pchTag[12]; //随机生成数据 for(int i = 0; i < ARRAY_LENGTH; i++) { CvRNG rng = cvRNG(cvGetTickCount()); msArray[i].i = cvRandInt(&rng) % 256; msArray[i].point = cvPoint( cvRandInt(&rng) % 1000,cvRandInt(&rng) % 1000); msArray[i].rect = cvRect( cvRandInt(&rng) % 1000,cvRandInt(&rng) % 1000,cvRandInt(&rng) % 600,cvRandInt(&rng) % 600 ); // 最后一个整数计数 sprintf( pchTag,"my_struct_%d",i ); write_my_struct(fs,pchTag,&msArray[i]); } cvReleaseFileStorage(&fs); /* 读数据部分 */ fs = cvOpenFileStorage("My_struct.xml",NULL,CV_STORAGE_READ ); MyStruct msArrayRead[ARRAY_LENGTH]; CvFileNode *pnode; for(int i = 0; i < ARRAY_LENGTH; i++) { sprintf( pchTag,i ); pnode = cvGetFileNodeByName(fs,pchTag); read_my_struct( fs,pnode,&msArrayRead[i] ); // 显示 printf("---------------------- %d: Write -------------------------\n",i); ShowStructValue( &msArray[i]); printf("---------------------- %d: Read --------------------------\n",i); ShowStructValue( &msArrayRead[i]); // 检查读写是否一致 if(check(&msArray[i],&msArrayRead[i] )) { printf("Consistent?:\tAnswer: True\n"); } else { printf("Consistent?:\tAnswer: False\n"); } getchar(); } cvReleaseFileStorage(&fs); return 0; }

引用 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.f2er.com/xml/296508.html

猜你在找的XML相关文章