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