zoukankan      html  css  js  c++  java
  • 《学习OpenCV》练习题第三章第八题a

      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》第三章第八题a
     22  * 完成时间:19:12 4/4 星期四  2013
     23  */
     24 
     25 typedef struct my_struct
     26 {
     27     int i;
     28     CvPoint point;
     29     CvRect rect;
     30 } MyStruct;
     31 
     32 void write_my_struct(CvFileStorage * fs, const char* name, my_struct*  ms)
     33 {
     34     //开始写数据
     35     cvStartWriteStruct(fs, name, 6);
     36 
     37     //写入一个 整数
     38     cvStartWriteStruct(fs,"integer",CV_NODE_SEQ);
     39     cvWriteInt(fs,NULL,ms->i);
     40     cvEndWriteStruct(fs);
     41 
     42     //写入cvpoint结构
     43     cvStartWriteStruct(fs,"CvPoint",CV_NODE_SEQ);
     44     cvWriteInt(fs,NULL,ms->point.x);
     45     cvWriteInt(fs,NULL,ms->point.y);
     46     cvEndWriteStruct(fs);
     47     
     48     //写入rect结构体
     49     cvStartWriteStruct(fs,"CvRect",CV_NODE_SEQ);
     50     cvWriteInt(fs,NULL,ms->rect.x);
     51     cvWriteInt(fs,NULL,ms->rect.y);
     52     cvWriteInt(fs,NULL,ms->rect.height);
     53     cvWriteInt(fs,NULL,ms->rect.width);
     54     cvEndWriteStruct(fs);
     55      
     56     //结束写数据
     57     cvEndWriteStruct(fs);
     58 }
     59 
     60 void read_my_struct(CvFileStorage* fs, CvFileNode* ms_node, my_struct* ms)
     61 {
     62     // 读第一个整数
     63     // 注意:这里应使用node->data.i的value来读取Integer
     64     int i = cvGetFileNodeByName(fs, ms_node, "integer")->data.i;
     65     ms->i = i;
     66 
     67     // 读CvPoint结构
     68     CvSeq *s1 = cvGetFileNodeByName(fs, ms_node, "CvPoint")->data.seq;
     69     CvPoint point;
     70     point.x= cvReadInt((CvFileNode*)cvGetSeqElem(s1,0));
     71     point.y= cvReadInt((CvFileNode*)cvGetSeqElem(s1,1));
     72     ms->point = point;
     73 
     74     // 读取CvRect结构
     75     CvSeq *s2 = cvGetFileNodeByName(fs, ms_node, "CvRect")->data.seq;
     76     CvRect rect;
     77     rect.x=cvReadInt((CvFileNode*)cvGetSeqElem(s2, 0));
     78     rect.y=cvReadInt((CvFileNode*)cvGetSeqElem(s2, 1));
     79     rect.width=cvReadInt((CvFileNode*)cvGetSeqElem(s2, 3));
     80     rect.height=cvReadInt((CvFileNode*)cvGetSeqElem(s2, 2));
     81     ms->rect = rect;
     82 }
     83 
     84 // 将MyStruct的值显示出来
     85 void ShowStructValue(MyStruct* pvalue)
     86 {
     87     printf("integer:%d\n", pvalue->i);
     88     printf("CvPoint: (%d, %d)\n", pvalue->point.x, pvalue->point.y );
     89     printf("CvRect: h-->%d\tw-->%d\t(%d, %d)\n", pvalue->rect.height, 
     90         pvalue->rect.width, pvalue->rect.x, pvalue->rect.y);
     91 }
     92 
     93 
     94 int main()
     95 {
     96     /* 写数据部分 */
     97     MyStruct struct_1; 
     98     struct_1.i = 10;
     99     struct_1.point = cvPoint(50, 145);
    100     struct_1.rect = cvRect(2, 2, 400, 400);
    101 
    102     CvFileStorage* fs = cvOpenFileStorage("My_struct.xml", 0, CV_STORAGE_WRITE);
    103 
    104     write_my_struct(fs, "my_struct", &struct_1);
    105     cvReleaseFileStorage(&fs);
    106 
    107     /* 读数据部分 */
    108     fs = cvOpenFileStorage("My_struct.xml", NULL, CV_STORAGE_READ );
    109     MyStruct struct_2;
    110      CvFileNode *pnode = cvGetFileNodeByName(fs, NULL, "my_struct");
    111 
    112     read_my_struct( fs, pnode, &struct_2 );
    113 
    114     // 显示
    115     printf("---------------- Write -----------------\n");
    116     ShowStructValue( & struct_1 );
    117     printf("---------------- Read -------------------\n");
    118     ShowStructValue( & struct_2);
    119 
    120     cvReleaseFileStorage(&fs);
    121 
    122     return 0;
    123 }

    相应的XML文件:

    运行结果:

  • 相关阅读:
    JSON,数组根据字段多次分组
    .net c#后台请求接口
    数组的高级应用含ES6 for of 用法
    js获取当前页面url网址信息
    资源管理神器Clover
    ES6的7个实用技巧
    单标签实现气泡三角形
    移动端--上拉加载更多
    checkbox对齐-复选框图标
    textarea显示源代码
  • 原文地址:https://www.cnblogs.com/qdsclove/p/3010675.html
Copyright © 2011-2022 走看看