关闭

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

标签: opencvxml写入与读取
164人阅读 评论(0) 收藏 举报
分类:

题目说明:创建一个结构,结构中包含一个整数,一个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",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);
}

// 检查两个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", 0, 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, "my_struct_%d", i );
        pnode = cvGetFileNodeByName(fs, NULL, 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://blog.csdn.net/stk_overflow/article/details/8759540
http://www.docs.opencv.org/index.html

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:15179次
    • 积分:320
    • 等级:
    • 排名:千里之外
    • 原创:12篇
    • 转载:28篇
    • 译文:1篇
    • 评论:0条