用opencv对YAML或XML的操作分为两种操作,操作与所对应的opencv类如下:
输入数据 :FileStorag;
输出数据 :FileStorag ,FileNode ,FileNodeIterator;
因为对YAML与XML的操作都是一样的,只是读取文件时前者后缀是 .yaml 后者是 .xml,所以在这只举例对YAML操作,输入的数据类型有一般常类型(如int),矩阵,序列(vector),maps和自定义的类。不管是输入还是输出数据都是通过键值的方式,输入时先输入键再输入对应的值,输出时是由键索引值,通过下面代码会容易理解。
1.输入数据
数据有:
整型 int 100 ;
矩阵 Mat R = Mat_<uchar>::eye(3, 3),T = Mat_<double>::zeros(3, 1);
序列 ["image1.jpg" ,"Awesomeness" ,"baboon.jpg"];
maps {One:1,Two:2 };
自定义类 MyData m(1);//类的具体定义在下面代码中给出
代码:
string filename = "E:\\test.yaml";
FileStorage fs;
fs.open(filename,FileStorage::WRITE);
//输入100
fs << "iterationNr" << 100;
//输入序列
fs << "strings" << "[";
fs << "image1.jpg" << "Awesomeness" << "baboon.jpg";
fs << "]";
//输入maps
fs << "Mapping";
fs << "{" << "One" << 1;
fs << "Two" << 2 << "}";
//输入矩阵
fs << "R" << R;
fs << "T" << T;
//输入自定义类
fs << "MyData" << m;
fs.release();
2.输出数据
代码:
FileStorage fs;
fs.open(filename, FileStorage::READ);
int itNr;
//fs["iterationNr"] >> itNr;
itNr = (int) fs["iterationNr"];
cout << itNr;
if (!fs.isOpened())
{
cerr << "Failed to open " << filename << endl;
return 1;
}
//获取节点
FileNode n = fs["strings"];
if (n.type() != FileNode::SEQ)
{
cerr << "strings is not a sequence! FAIL" << endl;
return 1;
}
//遍历序列输出各元素
FileNodeIterator it = n.begin(), it_end = n.end();
for (; it != it_end; ++it)
cout << (string)*it << endl;
//遍历maps输出各元素
n = fs["Mapping"];
cout << "Two " << (int)(n["Two"]) << "; ";
cout << "One " << (int)(n["One"]) << endl << endl;
MyData m;
Mat R, T;
//输出矩阵
fs["R"] >> R;
fs["T"] >> T;
//输出自定义类
fs["MyData"] >> m;
当打开文件时,会使用FileStorag类的一个open()函数,这个函数中的第一个参数是文件名路径,第二个参数都以常量形式指定你要对文件进行操作的类型,包括:WRITE, READ 或 APPEND。
3.完整的代码如下:
using namespace cv;
using namespace std;
class MyData
{
public:
MyData() : A(0), X(0), id() {}
explicit MyData(int) : A(97), X(CV_PI), id("mydata1234") {}
void write(FileStorage& fs) const //将类的数据写入到fs
{
fs << "{" << "A" << A << "X" << X << "id" << id << "}";
}
void read(const FileNode& node) //将node的数据读入类中
{
A = (int)node["A"];
X = (double)node["X"];
id = (string)node["id"];
}
public: // Data Members
int A;
double X;
string id;
};
//These write and read functions must be defined for the serialization in FileStorage to work
void write(FileStorage& fs, const std::string&, const MyData& x)
{
x.write(fs);//将x类中的数据写入到fs中
}
void read(const FileNode& node, MyData& x, const MyData& default_value = MyData())
{
if(node.empty())
x = default_value;
else
x.read(node);//将node的数据读入到x类中
}
// 对<<的重载,将类的属性输出
ostream& operator<<(ostream& out, const MyData& m)
{
out << "{ id = " << m.id << ", ";
out << "X = " << m.X << ", ";
out << "A = " << m.A << "}";
return out;
}
int main()
{
string filename = "E:\\test.yaml";
{ //write
Mat R = Mat_<uchar>::eye(3, 3),
T = Mat_<double>::zeros(3, 1);
MyData m(1);
FileStorage fs(filename, FileStorage::WRITE);
fs << "iterationNr" << 100;
//输入序列
fs << "strings" << "[";
fs << "image1.jpg" << "Awesomeness" << "baboon.jpg";
fs << "]";
//输入maps
fs << "Mapping";
fs << "{" << "One" << 1;
fs << "Two" << 2 << "}";
//输入矩阵
fs << "R" << R;
fs << "T" << T;
//输入自定义类
fs << "MyData" << m;
fs.release();
cout << "Write Done." << endl;
}
{//read
cout << endl << "Reading: " << endl;
FileStorage fs;
fs.open(filename, FileStorage::READ);
int itNr;
//fs["iterationNr"] >> itNr;
itNr = (int) fs["iterationNr"];
cout << itNr;
if (!fs.isOpened())
{
cerr << "Failed to open " << filename << endl;
return 1;
}
//获取节点
FileNode n = fs["strings"];
if (n.type() != FileNode::SEQ)
{
cerr << "strings is not a sequence! FAIL" << endl;
return 1;
}
//遍历序列输出各元素
FileNodeIterator it = n.begin(), it_end = n.end();
for (; it != it_end; ++it)
cout << (string)*it << endl;
//遍历maps输出各元素
n = fs["Mapping"];
cout << "Two " << (int)(n["Two"]) << "; ";
cout << "One " << (int)(n["One"]) << endl << endl;
MyData m;
Mat R, T;
//输出矩阵
fs["R"] >> R;
fs["T"] >> T;
//输出自定义类
fs["MyData"] >> m;
cout << endl
<< "R = " << R << endl;
cout << "T = " << T << endl << endl;
cout << "MyData = " << endl << m << endl << endl;
//Show default behavior for non existing nodes
cout << "Attempt to read NonExisting (should initialize the data structure with its default).";
fs["NonExisting"] >> m;
cout << endl << "NonExisting = " << endl << m << endl;
}
cout << endl
<< "Tip: Open up " << filename << " with a text editor to see the serialized data." << endl;
return 0;
}
PS:本人是用Qtcreator创建的程序。