学习OpenCV课后题3.8(a)

来源:互联网 发布:程序员机械键盘什么轴 编辑:程序博客网 时间:2024/05/21 15:03

// Ex3_8.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <cv.h>
#include <cvcam.h>
#include <cxcore.h>
#include <highgui.h>

 

#pragma comment(lib,"cv.lib")
#pragma comment(lib,"cvcam.lib")
#pragma comment(lib,"cxcore.lib")
#pragma comment(lib,"highgui.lib")

 

typedef struct tagMyStruct
{
 CvPoint m_Point;
  CvRect m_Rect;
}SMyStruct;


void WriteMyStruct(CvFileStorage *pStorage, const char *pName, SMyStruct *pMyStruct)
{

 if (NULL == pStorage)
 {
  return ;
 }

 cvStartWriteStruct(pStorage,pName,CV_NODE_MAP);

  cvStartWriteStruct(pStorage,"CvPoint",CV_NODE_SEQ);
   cvWriteInt(pStorage,NULL,pMyStruct->m_Point.x);
   cvWriteInt(pStorage,NULL,pMyStruct->m_Point.y);
  cvEndWriteStruct(pStorage);

  cvStartWriteStruct(pStorage,"CvRect",CV_NODE_SEQ);
   cvWriteInt(pStorage,NULL,pMyStruct->m_Rect.x);
   cvWriteInt(pStorage,NULL,pMyStruct->m_Rect.y);
   cvWriteInt(pStorage,NULL,pMyStruct->m_Rect.width);
   cvWriteInt(pStorage,NULL,pMyStruct->m_Rect.height);
  cvEndWriteStruct(pStorage);

 cvEndWriteStruct(pStorage);
}

void ReadMyStruct(CvFileStorage *pStorage, const char *pName, SMyStruct *pMyStruct)
{
 CvFileNode *pMapFileNode = cvGetFileNodeByName(pStorage,NULL,pName);
 assert(pMapFileNode != NULL);

 CvSeq *pSeq1 = cvGetFileNodeByName(pStorage,pMapFileNode,"CvPoint")->data.seq;
 assert(pSeq1 != NULL);

 pMyStruct->m_Point.x = cvReadInt( (CvFileNode*)cvGetSeqElem(pSeq1,0) );
 pMyStruct->m_Point.y = cvReadInt( (CvFileNode*)cvGetSeqElem(pSeq1,1) );

  printf("%d/n",pMyStruct->m_Point.x);
  printf("%d/n",pMyStruct->m_Point.y);

 CvSeq *pSeq2 = cvGetFileNodeByName(pStorage,pMapFileNode,"CvRect")->data.seq;
 assert(pSeq2 != NULL);

 pMyStruct->m_Rect.x = cvReadInt( (CvFileNode*)cvGetSeqElem(pSeq2,0) );
 pMyStruct->m_Rect.y = cvReadInt( (CvFileNode*)cvGetSeqElem(pSeq2,1) );
 pMyStruct->m_Rect.width = cvReadInt( (CvFileNode*)cvGetSeqElem(pSeq2,2) );
 pMyStruct->m_Rect.height = cvReadInt( (CvFileNode*)cvGetSeqElem(pSeq2,3) );


 
 printf("%d/n",pMyStruct->m_Rect.x);
 printf("%d/n",pMyStruct->m_Rect.y);
 printf("%d/n",pMyStruct->m_Rect.width);
 printf("%d/n",pMyStruct->m_Rect.height);

 

}

int main(int argc, char* argv[])
{
 /**************** 写数据 *******************/
 {
 CvFileStorage *pStorage = cvOpenFileStorage(
           "struct.xml",
             NULL,
          CV_STORAGE_WRITE
            );
 assert(pStorage != NULL);

 SMyStruct sTest = {1, 10, 10, 10,10,10};

 WriteMyStruct(pStorage,"MyStruct",&sTest);

 cvReleaseFileStorage(&pStorage);
 }

 /*************** 读数据 ******************/
 {
  SMyStruct sReadStruct;
  
  CvFileStorage *pStorage = cvOpenFileStorage(
          "struct.xml",
            NULL,
          CV_STORAGE_READ
               );
  assert(pStorage != NULL);
  
  ReadMyStruct(pStorage,"MyStruct",&sReadStruct);

 }

 return 0;
}

 

原创粉丝点击