從xml或yml檔案中讀取資料

來源:互聯網
上載者:User

CvFileStorage:檔案儲存體器,這是資料持久化和RTTI部分基礎的資料結構,該部分的其他函數均通過此結構來訪問檔案。


typedef struct CvFileStorage

{

    int flags;

    int is_xml;

    int write_mode;

    int is_first;

    CvMemStorage* memstorage;

    CvMemStorage* dststorage;

    CvMemStorage* strstorage;

    CvStringHash* str_hash;

    CvSeq* roots;

    CvSeq* write_stack;

    int struct_indent;

    int struct_flags;

    CvString struct_tag;

    int space;

    char* filename;

    FILE* file;

    char* buffer;

    char* buffer_start;

    char* buffer_end;

    int wrap_margin;

    int lineno;

    int dummy_eof;

    const char* errmsg;

    char errmsgbuf[128];

    CvStartWriteStruct start_write_struct;

    CvEndWriteStruct end_write_struct;

    CvWriteInt write_int;

    CvWriteReal write_real;

    CvWriteString write_string;

    CvWriteComment write_comment;

    CvStartNextStream start_next_stream;

    //CvParse parse;

}

CvFileStorage;


CvFileStorage結構是一個“黑箱”,代表著和磁碟上檔案相關聯的檔案儲存體器。下面介紹的一些函數使用CvFileStorage作為輸入並允許使用者去儲存或裝入分層次的集合,包括標量值、標準的CXCORE對象(如矩陣、序列、映像)和使用者定義物件等。

函數cvGetFileNodeByName通過結點名找到結點。此結點在map中找,或者如果map指標為NULL就從檔案儲存體器的頂層結點以此尋找。
在maps和cvGetSeqElem (或序列讀對象)中,使用此函數可以方便的穿梭於檔案儲存體器中。要加速對特定關鍵詞的多查詢(如,數組結構情況下),可以同時使用cvGetHashedKey和cvGetFileNode。


CvFileNode* cvGetFileNodeByName( const CvFileStorage* fs,

                                 const CvFileNode* map,

                                 const char* name );


fs

檔案儲存體器。

map

父map,如果為NULL函數將從第一個開始尋找所有頂層結點(流)。

name

檔案結點名


XML:

<?xml version="1.0"><opencv_storage><A type_id="opencv-matrix">  <rows>3</rows>  <cols>3</cols>  <dt>f</dt>  <data>1. 0. 0. 0. 1. 0. 0. 0. 1.</data></A></opencv_storage>


YML:

%YAML:1.0A: !!opencv-matrix  rows: 3  cols: 3  dt: f  data: [ 1., 0., 0., 0., 1., 0., 0., 0., 1.]

如例子中所示,XML使用嵌套標記來展示層次,而YAML使用縮排來實現(類似Python程式設計語言)。


同樣的CXCORE函數都可以使用這兩種格式來讀和寫,具體使用那種格式由開啟的檔案的副檔名來確定,是.xml則為XML檔案,.yml或.yaml則為YAML檔案。


#include "cxcore.h"#include "stdio.h"#pragma comment(lib, "cxcore.lib")/************************************************************************//* 使用OpenCV從檔案讀二維數組的執行個體                   *//* 作者:jink2005                    *//* 來源:http://www.aiseminar.cn/bbs/                                   *//************************************************************************/int main( int argc, char** argv ){ CvFileStorage *fs = cvOpenFileStorage("iris.xml", NULL, CV_STORAGE_READ); // 讀資料 CvFileNode *pa = cvGetFileNodeByName(fs, NULL, "A"); CvFileNode *par = cvGetFileNodeByName(fs, pa, "rows"); CvFileNode *pac = cvGetFileNodeByName(fs, pa, "cols"); CvFileNode *pad = cvGetFileNodeByName(fs, pa, "data"); int r = cvReadInt(par, 0); int c = cvReadInt(pac, 0); printf("r = %d, c = %d\n", r, c); // 取資料 if (CV_NODE_IS_SEQ(pad->tag)) {  CvSeq *seq = pad->data.seq;  int total = seq->total;  CvSeqReader reader;  cvStartReadSeq(seq, &reader, 0);  for (int i = 0; i < total; i ++)  {   CvFileNode *pt = (CvFileNode *)reader.ptr;   double x = cvReadReal(pt);   printf("%f ", x);   if ((i + 1) % c == 0)    printf("\n");   CV_NEXT_SEQ_ELEM(seq->elem_size, reader);  } }  // 釋放所用結構 cvReleaseFileStorage(&fs);    return 0;}




來自:http://www.aiseminar.cn/bbs/forum.php?mod=viewthread&tid=817

感謝~

=====================================

以下來自: http://blog.csdn.net/lzg188/article/details/5556518

/*寫一個設定檔*/

 CvFileStorage *fs = cvOpenFileStorage(".//config.xml",NULL,CV_STORAGE_WRITE);
 float matrix_data[9] = {1,0,0,0,1,0,0,0,1};
 CvMat cmatrix = cvMat( 3, 3, CV_32F, matrix_data );  
 cvWriteInt( fs, "frame_count", 10 );

 cvStartWriteStruct( fs, "frame_size", CV_NODE_SEQ ); 
 cvWriteInt( fs, 0, 320 ); 
 cvWriteInt( fs, 0, 200 ); 
 cvEndWriteStruct(fs);  
 cvSave( "E://config.xml", &cmatrix );

 cvReleaseFileStorage(&fs);

 

 /*讀一個設定檔*/
 CvFileStorage *fs = cvOpenFileStorage("D://config.xml",NULL,CV_STORAGE_READ);
 cvReadIntByName(fs,NULL,"frame_count",0);
 CvSeq *s = cvGetFileNodeByName(fs,0,"frame_size")->data.seq;
 int frame_width = cvReadInt((CvFileNode*)cvGetSeqElem(s,0),0);
 int frame_height = cvReadInt((CvFileNode*)cvGetSeqElem(s,1),0);
 cvReleaseFileStorage(&fs);

 

上面的代碼的檔案節點為序列的,沒有名字,使用起來可能不方便,

可以使用map形式的檔案節點,下面是執行個體的代碼及其效果。

<?xml version="1.0"?>
<opencv_storage>
<High_calibration>
  <first_levle_calibration>320</first_levle_calibration>
  <second_levle_calibration>200</second_levle_calibration></High_calibration>
</opencv_storage>

 

 CvFileStorage *fs = NULL;
 fs = cvOpenFileStorage(".//config.xml",NULL,CV_STORAGE_WRITE);
 cvStartWriteStruct( fs, "High_calibration", CV_NODE_MAP ); 
 cvWriteInt( fs, "first_levle_calibration", 320 ); 
 cvWriteInt( fs, "second_levle_calibration", 200 ); 
 cvEndWriteStruct(fs);  
 cvReleaseFileStorage(&fs);

 

CvFileStorage *fs1 = NULL;
 fs1 = cvOpenFileStorage(".//config.xml",NULL, CV_STORAGE_READ);
 CvFileNode *fn = cvGetFileNodeByName(fs1,NULL,"High_calibration");
 int i_first_level = cvReadIntByName(fs1,fn,"first_levle_calibration",0);
 int i_secont_level = cvReadIntByName(fs1,fn,"second_levle_calibration",0);
 cvReleaseFileStorage(&fs1);

 

另外,對於同一個設定檔,重新建立一個CvFileStorage之後,再重新讀寫後,原來的檔案中的內容將會消失,建議在不同的檔案,塊中寫入同一個設定檔時,最好建立一個全域的CvFileStorage變數。

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.