什么是XML、YAML文件
XML(eXtensible Markup Language)是一種元標記語言。所謂“原標記”,就是開發者可以根據自身需要定義的標記,任何滿足XML命名規則的名稱都可以標記。此外,XML是一種語義/結構化語言,它描述了文檔的結構和語義。
??YAML(YAML Ain’t a Markup Language)也是一種置標語言,但它是以數據為中心,而不是以置標語言為重點,用來表達資料序列的格式。
為什么要使用這種文件格式?
https://blog.csdn.net/wei7017406/article/details/81133852
https://blog.csdn.net/weixin_30508309/article/details/95178005?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task
https://segmentfault.com/q/1010000017097976/a-1020000017098736
實現寫入和讀取文件的步驟
1、實例化一個FileStorage類,利用構造函數完成初始化。
2、使用流操作符<<進行文件寫入操作,或者>>進行文件讀取操作
3、使用FileStorage::release()函數析構掉FileStorage類,并關閉文件
【1】XML、YAML文件的打開
1、準備文件寫操作
構造函數FileStorage::FileStorage()的使用方法:
1、帶參數的
FileStorage fs("abc.xml",FileStorage ::WRITE);
2、不帶參數
FileStorage fs;
fs.open("abc.xml",FileStorage::WRITE);
2、準備文件讀操作
1、帶參數的
FileStorage fs("abc.xml",FileStorage ::READ);
2、不帶參數
FileStorage fs;
fs.open("abc.xml",FileStorage::READ);
【2】進行文件讀寫操作
1、文本和數字的輸入和輸出
1、寫入文件操作
fs<<"iterationNr"<<100; ???什么意思
2、讀取文件操作
int itNr;
fs["iterationNr"]>>itNr; ???這句話和下面一句話有什么異同
itNr = (int) fs["iterationNr"];
2、Opencv數據結構的輸入和輸出
//數據結構的初始化
Mat R= Mat_<uchar>::eye(3,3); //單位矩陣 就是只有對角線元素為1的矩陣
Mat T= Mat_<uchar>::zeros(3,3);
//向Mat中寫入數據
fs<<"R"<<R;
fs<<"T"<<T;
//從Mat中讀取數據
fs["R"]>>R;
fs["T"]>>T;
【3】vector(arrays)和maps的輸入和輸出
對于vector結構的輸入和輸出,要注意在第一個元素前加上 “[”,在最后一個元素前加上 “]”
對于map結構,則加上 “{” 和 “}”
fs<<"strings"<<"["; //開始讀取名為string的文本序列
fs<<"imagel,jpg"<<"dadada"<<"baboon.jpg"; //這些啥意思?
fs<<"]"; //關閉操作
讀取這些結構時,會用到FileNode和FileNodeIterator數據結構。
對FileStorage類的"[“和”]"操作符會返回FileNode類型;
對于一連串的node可以使用FileNodeIterator;
FileNode n = fs["strings"]; //讀取字符串序列得到節點
if(n.type() != FileNode::SEQ)
{cerr<<"發生錯誤!字符串不是一個序列!"<<endl; //cerr什么意思?return 1;
}
FileNodeIterator it =n.begin(),it_end =n.end(); //遍歷節點
for(;it!=it_end ;++it)
{cout<<(string)*it<<endl;
}
參考鏈接:cerr什么意思?
【4】文件的關閉
調用過程:
fs.release();
示例程序:文件寫入
#include<opencv2/opencv.hpp>
#include <vector>
#include <time.h>using namespace std;
using namespace cv;int main()
{//初始化FileStorage fs("test.yaml", FileStorage::WRITE);//開始文件寫入fs << "frameCount" << 5;time_t rawtime;time(&rawtime);fs << "calibrationDate" << asctime(localtime(&rawtime));Mat cameraMatrix = (Mat_<double>(3, 3) << 1000, 0, 320, 0, 1000, 240, 0, 0, 1);Mat distCoeffs = (Mat_<double>(5, 1) << 0.1, 0.01, -0.001, 0, 0);fs << "cameraMatrix" << cameraMatrix << "distCoeffs" << distCoeffs;fs << "features" << "[";for (int i = 0; i < 3; ++i){int x = rand() % 640;int y = rand() % 480;uchar lbp = rand() % 256;fs << "{:" << "x" << x << "y" << y << "lbp" << "[:";for (int j = 0; j < 8; ++j){fs << ((lbp >> j) & 1);}fs << "]" << "}";}fs << "]";fs.release();printf("文件讀寫完畢,請在工程目錄下查看生成的文件~\n");waitKey(0);return 0;
}
運行時可能會報錯:
嚴重性 代碼 說明 項目 文件 行 禁止顯示狀態 禁止顯示狀態 錯誤 C4996 ‘asctime’,
解決方法:https://blog.csdn.net/tjylv/article/details/97397985
示例程序:XML和YAML文件的讀取
#include<opencv2/opencv.hpp>
#include <vector>
#include <time.h>using namespace std;
using namespace cv;int main()
{//改變console字體顏色system("color 6F");//初始化FileStorage fs2("test.yaml", FileStorage::READ);//第一種方法,對FileNode操作int frameCount = (int)fs2["frameCount"];std::string date;//第二種方法,使用FileNode運算符>>fs2["calibrationDate"] >> date;Mat cameraMatrix, distCoeffs2;fs2["cameraMatrix"] >> cameraMatrix;fs2["distCoeffs"] >> distCoeffs2;cout << "frameCount: " << frameCount << endl<< "calibration date: " << date << endl<< "camera matrix:\n" << cameraMatrix << endl<< "distortion coeffs:\n" << distCoeffs2 << endl;FileNode features = fs2["features"];FileNodeIterator it = features.begin(), it_end = features.end();int idx = 0;std::vector<uchar> lbpval;//使用FileNodeIterator遍歷序列for (; it != it_end; ++it, ++idx){cout << "feature #" << idx << ":";cout << "x=" << (int)(*it)["x"] << ", y = " << (int)(*it)["*y"] << ", lbp: (";//我們也可以使用FileNode >> std::vector操作符來很容易地讀數值陣列(*it)["lbp"] >> lbpval;for (int i = 0; i < (int)lbpval.size(); ++i){cout << " " << (int)lbpval[i];}cout << ")" << endl;}fs2.release();//程序結束,輸出一些幫助文件printf("\n文件讀取完畢,請輸入任意鍵結束程序~\n");getchar();return 0;
}
文章代碼摘自淺墨的opencv3入門。