XML\YAML文件在OpenCV中的數據結構爲FileStorage,打開操做例如: node
string filename = "I.xml"; FileStorage fs(filename, FileStorage::WRITE); \\... fs.open(filename, FileStorage::READ);
文件關閉操做會在FileStorage結構銷燬時自動進行,但也可調用以下函數實現數據結構
fs.release();
寫入文件使用 << 運算符,例如:app
fs << "iterationNr" << 100;
讀取文件,使用 >> 運算符,例如函數
int itNr; fs["iterationNr"] >> itNr; itNr = (int) fs["iterationNr"];
Mat R = Mat_<uchar >::eye (3, 3), T = Mat_<double>::zeros(3, 1); fs << "R" << R; // Write cv::Mat fs << "T" << T; fs["R"] >> R; // Read cv::Mat fs["T"] >> T;
vector要注意在第一個元素前加上「[」,在最後一個元素前加上"]"。例如:spa
fs << "strings" << "["; // text - string sequence fs << "image1.jpg" << "Awesomeness" << "baboon.jpg"; fs << "]"; // close sequence
對於map結構的操做使用的符號是"{"和"}",例如code
fs << "Mapping"; // text - mapping fs << "{" << "One" << 1; fs << "Two" << 2 << "}";
讀取這些結構的時候,會用到FileNode和FileNodeIterator數據結構。對FileStorage類的[]操做符會返回FileNode數據類型,對於一連串的node,可使用FileNodeIterator結構,例如:xml
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;
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();