1. 程式人生 > >opencv資料讀寫操作

opencv資料讀寫操作

           主要參考

1.opencv讀寫yml和xml資料

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

#include "stdafx.h"
#include <stdio.h>
#include <iostream>

#include "opencv/cv.h"
#include "opencv/cxcore.h"
#include "opencv/highgui.h"

using namespace std;
void WriteYML()
{
    CvMat* mat = cvCreateMat( 3, 3, CV_32F );
    CvFileStorage* fs = cvOpenFileStorage( "example.yml", 0, CV_STORAGE_WRITE );

    cvSetIdentity( mat );
    cvWrite( fs, "A", mat, cvAttrList(0,0) );

    cvReleaseFileStorage( &fs );
    cvReleaseMat( &mat );
}

void WriteXML()
{


   double a[] = { 1,  0,  0,  0,  0,
               0,  1,  0,  0,  0,
               0,  0,  1,  0,  0,
               0,  0,  0,  1,  0,
               0,  0,  0,  0,  1 };
   CvMat A1;
   cvInitMatHeader( &A1, 5, 5, CV_64FC1, a, CV_AUTOSTEP );
   cvSave( "my_matrix_test.xml", &A1 );


}
void ReadXML()
{
   CvMat* A1 = (CvMat*)cvLoad( "my_matrix_test.xml" );
   for(int i = 0; i <5; i++ )
   {
      printf( "\n");
      for(int j = 0; j < 5; j++ )
      printf( "%f  ", (float) cvGetReal2D( A1, i, j ));   
   }  
   printf( "\n\n");   
}

void ReadYML()
{
   CvMat* A1 = (CvMat*)cvLoad( "example.yml" );
   for(int i = 0; i <3; i++ )
   {
      printf( "\n");
      for(int j = 0; j < 3; j++ )
      printf( "%f  ", (float) cvGetReal2D( A1, i, j ));   
   }  
   printf( "\n\n");   
}

int main( int argc, char** argv )
{
	int r;
        WriteYML();
	printf("Write the yml accomplished!");
	ReadYML();
	cin>>r;

    return 0;
}