Implementing I/O operations on XML and YAML files

Source: Internet
Author: User

1. Open and close the file

The data structure of the Xml\yaml file in OpenCV is Filestorage, which opens operations such as:

string " I.xml " ; Filestorage FS (filename, filestorage::write); \\...fs.open (filename, filestorage::read);

The file close operation will be done automatically when the filestorage structure is destroyed, but it can also be called the following function implementation

Fs.release ();
2. Input and output of text and numbers

Write files using the << operator, for example:

" Iterationnr " ;

Read the file, using the >> operator, for example

int itnr;fs["Iterationnr"] >>= (int) fs["  Iterationnr"];
3. The input and output of the OPENCV data structure is the same as the basic C + + form
Mat R = Mat_<uchar >::eye (3,3), T= mat_<Double>::zeros (3,1); FS<<"R"<< R;//Write Cv::matFS <<"T"<<t;fs["R"] >> R;//Read Cv::matfs["T"] >> T;
4. Input and output of vector (arrays) and maps

The vector should be careful to add "[" to the first element before the last element with "]". For example:

FS <<"Strings"<<"[";//text-string SequenceFS <<"image1.jpg"<<"awesomeness"<<"baboon.jpg"; FS<<"]";//Close Sequence

The symbols used for the operation of the map structure are "{" and "}", for example

" Mapping " // text-mapping " {""one"1"  2"}";

When reading these structures, the filenode and filenodeiterator data structures are used. The [] operator of the Filestorage class returns the Filenode data type, and for a series of node, you can use the filenodeiterator structure, for example:

5. File read
 filenode n = Fs[ " strings  " ]; //  Read string Sequence–get node  if  (N.type ()!= Filenode::seq) {cerr  <<  "  strings is not a sequence! FAIL   << Endl;  return  1  ;} Filenodeiterator It  = N.begin (), it_end = N.end (); //  Go through the node  for  (; it! = It_end; ++it) cout  << (string ) *it << Endl; 
6. Example Introduction

if(Filename.empty ()) {return 0;    } cv::filestorage FS (Filename,cv::filestorage::write); if(!fs.isopened ()) {        return 0; } FS<<"Prefiltercap"<<stBM.state->Prefiltercap; FS<<"sadwindowsize"<<stBM.state->sadwindowsize; FS<<"mindisparity"<<stBM.state->mindisparity; FS<<"numberofdisparities"<<stBM.state->numberofdisparities; FS<<"Texturethreshold"<<stBM.state->Texturethreshold; FS<<"Uniquenessratio"<<stBM.state->Uniquenessratio; FS<<"specklewindowsize"<<stBM.state->specklewindowsize; FS<<"Specklerange"<<stBM.state->Specklerange; FS<<"Leftvalidarea" ; FS<<"{"<<"roi1x"<<stBM.state->roi1.x; FS<<"roi1y"<<stBM.state->roi1.y; FS<<"Roi1width"<<stBM.state->Roi1.width; FS<<"Roi1height"<<stBM.state->Roi1.height; FS<<"}" ; FS<<"Rightvalidarea" ; FS<<"{"<<"roi2x"<<stBM.state->roi2.x; FS<<"roi2y"<<stBM.state->roi2.y; FS<<"Roi2width"<<stBM.state->Roi2.width; FS<<"Roi2height"<<stBM.state->Roi2.height; FS<<"}" ; Fs.release ();
7. Experimental results

Implementing I/O operations on XML and YAML files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.