什么是XML、YAML文件
XML(eXtensible Markup Language)是一种元标记语言。所谓“原标记”,就是开发者可以根据自身需要定义的标记,任何满足XML命名规则的名称都可以标记。此外,XML是一种语义/结构化语言,它描述了文档的结构和语义。
YAML(YAML Ain’t a Markup Language)也是一种置标语言,但它是以数据为中心,而不是以置标语言为重点,用来表达资料序列的格式。
为什么要使用这种文件格式?
实现写入和读取文件的步骤
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();
示例程序:文件写入
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’,
解决方法:javascript:void(0)
示例程序:XML和YAML文件的读取
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入门。