Opencv (3) -- Implement I/O operations on XML and yaml files

Source: Internet
Author: User

The data structure of the XML \ yaml file in opencv is filestorage.

string filename = "I.xml";FileStorage fs(filename, FileStorage::WRITE);\\...fs.open(filename, FileStorage::READ);
fs.release();

 

Use the <operator to read files and the> operator to write files

fs << "iterationNr" << 100;
int itNr;fs["iterationNr"] >> itNr;itNr = (int) fs["iterationNr"];

 

Input and Output of opencv Data Structure

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;

 

Add "[" before the first element and "]" before the last element to the vector.

fs << "strings" << "["; // text - string sequencefs << "image1.jpg" << "Awesomeness" << "baboon.jpg";fs << "]"; // close sequence

The symbols used for map structure operations are "{" and "}"

fs << "Mapping"; // text - mappingfs << "{" << "One" << 1;fs << "Two" << 2 << "}";

 

The filenode and filenodeiterator data structures are used to read these structures. The [] operator of the filestorage class returns the filenode data type. For a series of nodes, the filenodeiterator structure can be used.

FileNode n = fs["strings"]; // Read string sequence - Get nodeif (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 nodefor (; it != it_end; ++it)cout << (string)*it << endl;

 

<?xml version="1.0"?><opencv_storage><depthImg190 type_id="opencv-image"><width>320</width>

 

This format cannot directly use XML, but requires

Iplimage * depth = (iplimage *) cvload ("depthimg190.xml ");

Cvload Function

 

Const char * filename = "zhang.jpg"; STD: ifstream file (filename); STD: vector <char> data; File> STD: noskipws; // noskipws does not ignore blank STD: Copy (STD: istream_iterator <char> (file), STD: istream_iterator <char> (), STD :: back_inserter (data); CV: mat matrixjprg = CV: imdecode (CV: MAT (data), 1); iplimage qimg; qimg = iplimage (matrixjprg ); // CV: mat-> iplimage can be forcibly converted to cvsaveimage (". /out.jpg ", & qimg );

When cvsaveimage directly saves the CV: mat data as an image, a problem may occur. The above code must be forcibly converted.

Opencv (3) -- Implement 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.