《学习OpenCV》练习题第三章第八题b

系统 1609 0
      
          1
      
       #include <highgui.h>


      
          2
      
       #include <cv.h>


      
          3
      
       #include <stdio.h>


      
          4
      
      
          5
      
      
        #pragma
      
       comment (lib,"opencv_calib3d231d.lib")


      
          6
      
      
        #pragma
      
       comment (lib,"opencv_contrib231d.lib")


      
          7
      
      
        #pragma
      
       comment (lib,"opencv_core231d.lib")


      
          8
      
      
        #pragma
      
       comment (lib,"opencv_features2d231d.lib")


      
          9
      
      
        #pragma
      
       comment (lib,"opencv_flann231d.lib")


      
         10
      
      
        #pragma
      
       comment (lib,"opencv_gpu231d.lib")


      
         11
      
      
        #pragma
      
       comment (lib,"opencv_haartraining_engined.lib")


      
         12
      
      
        #pragma
      
       comment (lib,"opencv_highgui231d.lib")


      
         13
      
      
        #pragma
      
       comment (lib,"opencv_imgproc231d.lib")


      
         14
      
      
        #pragma
      
       comment (lib,"opencv_legacy231d.lib")


      
         15
      
      
        #pragma
      
       comment (lib,"opencv_ml231d.lib")


      
         16
      
      
        #pragma
      
       comment (lib,"opencv_objdetect231d.lib")


      
         17
      
      
        #pragma
      
       comment (lib,"opencv_ts231d.lib")


      
         18
      
      
        #pragma
      
       comment (lib,"opencv_video231d.lib")


      
         19
      
      
         20
      
      
        /*
      
      
         21
      
      
         *《学习OpenCV》第三章第八题b


      
      
         22
      
      
         * 完成时间:19:46 4/4 星期四 2013


      
      
         23
      
      
        */
      
      
         24
      
      
         25
      
      
        #define
      
       ARRAY_LENGTH 10    
      
        //
      
      
         数组长度
      
      
         26
      
      
         27
      
       typedef 
      
        struct
      
      
         my_struct


      
      
         28
      
      
        {


      
      
         29
      
      
        int
      
      
         i;


      
      
         30
      
      
            CvPoint point;


      
      
         31
      
      
            CvRect rect;


      
      
         32
      
      
        } MyStruct;


      
      
         33
      
      
         34
      
      
        void
      
       write_my_struct(CvFileStorage * fs, 
      
        const
      
      
        char
      
      * name, my_struct*
      
          ms)


      
      
         35
      
      
        {


      
      
         36
      
      
        //
      
      
        开始写数据
      
      
         37
      
           cvStartWriteStruct(fs, name, 
      
        6
      
      
        );


      
      
         38
      
      
         39
      
      
        //
      
      
        写入一个 整数
      
      
         40
      
           cvStartWriteStruct(fs,
      
        "
      
      
        integer
      
      
        "
      
      
        ,CV_NODE_SEQ);


      
      
         41
      
           cvWriteInt(fs,NULL,ms->
      
        i);


      
      
         42
      
      
            cvEndWriteStruct(fs);


      
      
         43
      
      
         44
      
      
        //
      
      
        写入cvpoint结构
      
      
         45
      
           cvStartWriteStruct(fs,
      
        "
      
      
        CvPoint
      
      
        "
      
      
        ,CV_NODE_SEQ);


      
      
         46
      
           cvWriteInt(fs,NULL,ms->
      
        point.x);


      
      
         47
      
           cvWriteInt(fs,NULL,ms->
      
        point.y);


      
      
         48
      
      
            cvEndWriteStruct(fs);


      
      
         49
      
      
         50
      
      
        //
      
      
        写入rect结构体
      
      
         51
      
           cvStartWriteStruct(fs,
      
        "
      
      
        CvRect
      
      
        "
      
      
        ,CV_NODE_SEQ);


      
      
         52
      
           cvWriteInt(fs,NULL,ms->
      
        rect.x);


      
      
         53
      
           cvWriteInt(fs,NULL,ms->
      
        rect.y);


      
      
         54
      
           cvWriteInt(fs,NULL,ms->
      
        rect.height);


      
      
         55
      
           cvWriteInt(fs,NULL,ms->
      
        rect.width);


      
      
         56
      
      
            cvEndWriteStruct(fs);


      
      
         57
      
      
         58
      
      
        //
      
      
        结束写数据
      
      
         59
      
      
            cvEndWriteStruct(fs);


      
      
         60
      
      
        }


      
      
         61
      
      
         62
      
      
        void
      
       read_my_struct(CvFileStorage* fs, CvFileNode* ms_node, my_struct*
      
         ms)


      
      
         63
      
      
        {


      
      
         64
      
      
        //
      
      
         读第一个整数


      
      
         65
      
      
        //
      
      
         注意:这里应使用node->data.i的value来读取Integer
      
      
         66
      
      
        int
      
       i = cvGetFileNodeByName(fs, ms_node, 
      
        "
      
      
        integer
      
      
        "
      
      )->
      
        data.i;


      
      
         67
      
           ms->i =
      
         i;


      
      
         68
      
      
         69
      
      
        //
      
      
         读CvPoint结构
      
      
         70
      
           CvSeq *s1 = cvGetFileNodeByName(fs, ms_node, 
      
        "
      
      
        CvPoint
      
      
        "
      
      )->
      
        data.seq;


      
      
         71
      
      
            CvPoint point;


      
      
         72
      
           point.x= cvReadInt((CvFileNode*)cvGetSeqElem(s1,
      
        0
      
      
        ));


      
      
         73
      
           point.y= cvReadInt((CvFileNode*)cvGetSeqElem(s1,
      
        1
      
      
        ));


      
      
         74
      
           ms->point =
      
         point;


      
      
         75
      
      
         76
      
      
        //
      
      
         读取CvRect结构
      
      
         77
      
           CvSeq *s2 = cvGetFileNodeByName(fs, ms_node, 
      
        "
      
      
        CvRect
      
      
        "
      
      )->
      
        data.seq;


      
      
         78
      
      
            CvRect rect;


      
      
         79
      
           rect.x=cvReadInt((CvFileNode*)cvGetSeqElem(s2, 
      
        0
      
      
        ));


      
      
         80
      
           rect.y=cvReadInt((CvFileNode*)cvGetSeqElem(s2, 
      
        1
      
      
        ));


      
      
         81
      
           rect.width=cvReadInt((CvFileNode*)cvGetSeqElem(s2, 
      
        3
      
      
        ));


      
      
         82
      
           rect.height=cvReadInt((CvFileNode*)cvGetSeqElem(s2, 
      
        2
      
      
        ));


      
      
         83
      
           ms->rect =
      
         rect;


      
      
         84
      
      
        }


      
      
         85
      
      
         86
      
      
        //
      
      
         将MyStruct的值显示出来
      
      
         87
      
      
        void
      
       ShowStructValue(MyStruct*
      
         pvalue)


      
      
         88
      
      
        {


      
      
         89
      
           printf(
      
        "
      
      
        integer:%d\n
      
      
        "
      
      , pvalue->
      
        i);


      
      
         90
      
           printf(
      
        "
      
      
        CvPoint: (%d, %d)\n
      
      
        "
      
      , pvalue->point.x, pvalue->
      
        point.y );


      
      
         91
      
           printf(
      
        "
      
      
        CvRect: h-->%d\tw-->%d\t(%d, %d)\n
      
      
        "
      
      , pvalue->
      
        rect.height, 


      
      
         92
      
               pvalue->rect.width, pvalue->rect.x, pvalue->
      
        rect.y);


      
      
         93
      
      
        }


      
      
         94
      
      
         95
      
      
        //
      
      
         检查两个MyStruct是否一致
      
      
         96
      
      
        bool
      
       check(MyStruct* msValue1, MyStruct*
      
         msValue2)


      
      
         97
      
      
        {


      
      
         98
      
      
        if
      
      ( (msValue1->i == msValue2->i) && 


      
         99
      
               (msValue1->point.x == msValue2->point.x) &&


      
        100
      
               (msValue1->point.y == msValue2->point.y) && 


      
        101
      
               (msValue1->rect.height == msValue2->rect.height) && 


      
        102
      
               (msValue1->rect.width == msValue2->rect.width) && 


      
        103
      
               (msValue1->rect.x == msValue2->rect.x) && 


      
        104
      
               (msValue1->rect.y == msValue2->
      
        rect.y) )


      
      
        105
      
      
        return
      
      
        true
      
      
        ;


      
      
        106
      
      
        else
      
      
        107
      
      
        return
      
      
        false
      
      
        ;


      
      
        108
      
      
        }


      
      
        109
      
      
        110
      
      
        int
      
      
         main()


      
      
        111
      
      
        {


      
      
        112
      
      
        /*
      
      
         写数据部分 
      
      
        */
      
      
        113
      
      
            MyStruct msArray[ARRAY_LENGTH];


      
      
        114
      
      
        115
      
           CvFileStorage* fs = cvOpenFileStorage(
      
        "
      
      
        My_struct.xml
      
      
        "
      
      , 
      
        0
      
      
        , CV_STORAGE_WRITE);


      
      
        116
      
      
        char
      
       pchTag[
      
        12
      
      
        ]; 


      
      
        117
      
      
        //
      
      
         随机生成数据
      
      
        118
      
      
        for
      
      (
      
        int
      
       i = 
      
        0
      
      ; i < ARRAY_LENGTH; i++
      
        )


      
      
        119
      
      
            {


      
      
        120
      
               CvRNG rng =
      
         cvRNG(cvGetTickCount());


      
      
        121
      
      
        122
      
               msArray[i].i = cvRandInt(&rng) % 
      
        256
      
      
        ;


      
      
        123
      
               msArray[i].point = cvPoint( cvRandInt(&rng) % 
      
        1000
      
      , cvRandInt(&rng) % 
      
        1000
      
      
        );


      
      
        124
      
               msArray[i].rect = cvRect( cvRandInt(&rng) % 
      
        1000
      
      , cvRandInt(&rng) % 
      
        1000
      
      
        ,


      
      
        125
      
                   cvRandInt(&rng) % 
      
        600
      
      , cvRandInt(&rng) % 
      
        600
      
      
         );


      
      
        126
      
      
        127
      
      
        //
      
      
         最后一个整数计数
      
      
        128
      
               sprintf( pchTag, 
      
        "
      
      
        my_struct_%d
      
      
        "
      
      
        , i );


      
      
        129
      
               write_my_struct(fs, pchTag, &
      
        msArray[i]);


      
      
        130
      
      
            }


      
      
        131
      
      
        132
      
           cvReleaseFileStorage(&
      
        fs);


      
      
        133
      
      
        134
      
      
        /*
      
      
         读数据部分 
      
      
        */
      
      
        135
      
           fs = cvOpenFileStorage(
      
        "
      
      
        My_struct.xml
      
      
        "
      
      
        , NULL, CV_STORAGE_READ );


      
      
        136
      
      
            MyStruct msArrayRead[ARRAY_LENGTH];


      
      
        137
      
            CvFileNode *
      
        pnode; 


      
      
        138
      
      
        139
      
      
        for
      
      (
      
        int
      
       i = 
      
        0
      
      ; i < ARRAY_LENGTH; i++
      
        )


      
      
        140
      
      
            {


      
      
        141
      
               sprintf( pchTag, 
      
        "
      
      
        my_struct_%d
      
      
        "
      
      
        , i );


      
      
        142
      
               pnode =
      
         cvGetFileNodeByName(fs, NULL, pchTag);


      
      
        143
      
               read_my_struct( fs, pnode, &
      
        msArrayRead[i] );


      
      
        144
      
      
        145
      
      
        //
      
      
         显示
      
      
        146
      
               printf(
      
        "
      
      
        ---------------------- %d: Write -------------------------\n
      
      
        "
      
      
        , i);


      
      
        147
      
               ShowStructValue( &
      
        msArray[i]);


      
      
        148
      
               printf(
      
        "
      
      
        ---------------------- %d: Read --------------------------\n
      
      
        "
      
      
        , i);


      
      
        149
      
               ShowStructValue( &
      
        msArrayRead[i]);


      
      
        150
      
      
        //
      
      
         检查读写是否一致
      
      
        151
      
      
        if
      
      (check(&msArray[i], &
      
        msArrayRead[i] ))


      
      
        152
      
      
                {


      
      
        153
      
                   printf(
      
        "
      
      
        Consistent?:\tAnswer: True\n
      
      
        "
      
      
        );


      
      
        154
      
      
                }


      
      
        155
      
      
        else
      
      
        156
      
      
                {


      
      
        157
      
                   printf(
      
        "
      
      
        Consistent?:\tAnswer: False\n
      
      
        "
      
      
        );


      
      
        158
      
      
                }


      
      
        159
      
      
                getchar();


      
      
        160
      
      
            }


      
      
        161
      
      
        162
      
           cvReleaseFileStorage(&
      
        fs); 


      
      
        163
      
      
        164
      
      
        return
      
      
        0
      
      
        ;


      
      
        165
      
       }
    

对应的XML文件:

《学习OpenCV》练习题第三章第八题b

 

运行结果:

《学习OpenCV》练习题第三章第八题b

 

 

《学习OpenCV》练习题第三章第八题b


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论