1 目标 @H_301_2@ (1)在OpenCV中怎样使用XML和YAML文件打印和输出文本 @H_301_2@ (2)怎样对OpenCV数据结构进行输入和输出 @H_301_2@ (3)自定义数据结构怎样操作 @H_301_2@ (4)OpenCV数据结构,诸如FileStorage,FileNode或FileNodeIterator的使用。 @H_301_2@ 2 源代码
#include <opencv2/core/core.hpp>@H_502_10@
#include <iostream>@H_502_10@
#include <string>@H_502_10@
using@H_502_10@ namespace@H_502_10@ cv;
using@H_502_10@ namespace@H_502_10@ std@H_502_10@;
class@H_502_10@ MyData
{
public@H_502_10@:
MyData() : A(0@H_502_10@),X(0@H_502_10@),id()
{}
// 显式声明,避免隐式转换@H_502_10@
explicit@H_502_10@ MyData(int@H_502_10@) : A(97@H_502_10@),X(CV_PI),id("mydata1234"@H_502_10@)
{}
// 为该类写序列化实现@H_502_10@
void@H_502_10@ write(FileStorage& fs) const@H_502_10@
{
fs << "{"@H_502_10@ << "A"@H_502_10@ << A << "X"@H_502_10@ << X << "id"@H_502_10@ << id << "}"@H_502_10@;
}
// 为该类实现读取序列化@H_502_10@
void@H_502_10@ read(const@H_502_10@ FileNode& node)
{
A = (int@H_502_10@)node["A"@H_502_10@];
X = (double@H_502_10@)node["X"@H_502_10@];
id = (string@H_502_10@)node["id"@H_502_10@];
}
// Data Members@H_502_10@
public@H_502_10@:
int@H_502_10@ A;
double@H_502_10@ X;
string@H_502_10@ id;
};
// 这些读写函数必须用FileStorage定义读写序列化@H_502_10@
static@H_502_10@ void@H_502_10@ write(FileStorage& fs,const@H_502_10@ std@H_502_10@::string@H_502_10@&,const@H_502_10@ MyData& x)
{
x.write(fs);
}
static@H_502_10@ void@H_502_10@ read(const@H_502_10@ FileNode& node,MyData& x,const@H_502_10@ MyData& default_value = MyData()){
if@H_502_10@(node.empty())
x = default_value;
else@H_502_10@
x.read(node);
}
// 实现用户自定义类打印到控制台@H_502_10@
static@H_502_10@ ostream& operator@H_502_10@<<(ostream& out,const@H_502_10@ MyData& m)
{
out << "{ id = "@H_502_10@ << m.id << ","@H_502_10@;
out << "X = "@H_502_10@ << m.X << ","@H_502_10@;
out << "A = "@H_502_10@ << m.A << "}"@H_502_10@;
return@H_502_10@ out;
}
int@H_502_10@ main(int@H_502_10@ ac,char@H_502_10@** av)
{
if@H_502_10@ (ac != 2@H_502_10@)
{
help(av);
return@H_502_10@ 1@H_502_10@;
}
string@H_502_10@ filename = av[1@H_502_10@];
{ //write@H_502_10@
Mat R = Mat_<uchar>::eye(3@H_502_10@,3@H_502_10@),T = Mat_<double@H_502_10@>::zeros(3@H_502_10@,1@H_502_10@);
MyData m(1@H_502_10@);
FileStorage fs(filename,FileStorage::WRITE);
fs << "iterationNr"@H_502_10@ << 100@H_502_10@;
fs << "strings"@H_502_10@ << "["@H_502_10@;
// text - string sequence@H_502_10@
fs << "image1.jpg"@H_502_10@ << "Awesomeness"@H_502_10@ << "baboon.jpg"@H_502_10@;
fs << "]"@H_502_10@; // close sequence@H_502_10@
fs << "Mapping"@H_502_10@;// text - mapping@H_502_10@
fs << "{"@H_502_10@ << "One"@H_502_10@ << 1@H_502_10@;
fs << "Two"@H_502_10@ << 2@H_502_10@ << "}"@H_502_10@;
fs << "R"@H_502_10@ << R;// cv::Mat@H_502_10@
fs << "T"@H_502_10@ << T;
fs << "MyData"@H_502_10@ << m; // your own data structures@H_502_10@
fs.release(); // explicit close@H_502_10@
cout@H_502_10@ << "Write Done."@H_502_10@ << endl;
}
{//read@H_502_10@
cout@H_502_10@ << endl << "Reading: "@H_502_10@ << endl;
FileStorage fs;
fs.open(filename,FileStorage::READ);
int@H_502_10@ itNr;
//fs["iterationNr"] >> itNr;@H_502_10@
itNr = (int@H_502_10@) fs["iterationNr"@H_502_10@];
cout@H_502_10@ << itNr;
if@H_502_10@ (!fs.isOpened())
{
cerr@H_502_10@ << "Failed to open "@H_502_10@ << filename << endl;
help(av);
return@H_502_10@ 1@H_502_10@;
}
FileNode n = fs["strings"@H_502_10@]; // Read string sequence - Get node@H_502_10@
if@H_502_10@ (n.type() != FileNode::SEQ)
{
cerr@H_502_10@ << "strings is not a sequence! FAIL"@H_502_10@ << endl;
return@H_502_10@ 1@H_502_10@;
}
FileNodeIterator it = n.begin(),it_end = n.end(); // Go through the node@H_502_10@
for@H_502_10@ (; it != it_end; ++it)
cout@H_502_10@ << (string@H_502_10@)*it << endl;
n = fs["Mapping"@H_502_10@]; // Read mappings from a sequence@H_502_10@
cout@H_502_10@ << "Two "@H_502_10@ << (int@H_502_10@)(n["Two"@H_502_10@]) << "; "@H_502_10@;
cout@H_502_10@ << "One "@H_502_10@ << (int@H_502_10@)(n["One"@H_502_10@]) << endl << endl;
MyData m;
Mat R,T;
fs["R"@H_502_10@] >> R; // Read cv::Mat@H_502_10@
fs["T"@H_502_10@] >> T;
fs["MyData"@H_502_10@] >> m; // Read your own structure_@H_502_10@
cout@H_502_10@ << endl
<< "R = "@H_502_10@ << R << endl;
cout@H_502_10@ << "T = "@H_502_10@ << T << endl << endl;
cout@H_502_10@ << "MyData = "@H_502_10@ << endl << m << endl << endl;
//Show default behavior for non existing nodes@H_502_10@
cout@H_502_10@ << "Attempt to read NonExisting (should initialize the data structure with its default)."@H_502_10@;
fs["NonExisting"@H_502_10@] >> m;
cout@H_502_10@ << endl << "NonExisting = "@H_502_10@ << endl << m << endl;
}
cout@H_502_10@ << endl
<< "Tip: Open up "@H_502_10@ << filename << " with a text editor to see the serialized data."@H_502_10@ << endl;
return@H_502_10@ 0@H_502_10@;
}
3 解释 @H_301_2@ 在这里,我们只讨论XML和YAML文件的输入。它们有两类你可以序列化的数据结构:映射(像STL map)和元素序列(像STL vector)。它们的不同在于,对于映射map,每一个元素都有唯一的名称,你可以通过它访问。对于序列,你需要遍历才能访问指定的一个。 @H_301_2@ (1)XML/YAML文件的打开和关闭。 @H_301_2@ 同所有的文件读写一样。首先,你必须先打开文件;末尾,还要关闭文件。OpenCV有专门的控制这类文件的类FileStorage。使用其open()函数或者构造函数打开你硬件驱动上对应的文件。
string filename = "I.xml"@H_502_10@;
FileStorage fs(filename,FileStorage::WRITE);
\\...@H_502_10@
fs.open(filename,FileStorage::READ);
上面的两种方法,同普通文件的读写控制没有什么大的区别。第二个参数指定对文件的操作类型:读,写和附加。文件名中的扩展格式决定了输出格式。如果使用诸如.xml.gz的扩展格式,那么输出就会被压缩。 @H_301_2@ 当FileStorage对象被销毁时,文件会自动关闭。当然了,你也可以显式地调用release函数进行释放:
fs.release(); // 显式地关闭@H_502_10@
(2)文本和数字的输入输出。 @H_301_2@ 该数据结构使用和STL标准模板库相同的”<<”输出操作符。为了输出任何类型的数据结构,我们首先需要指定它的名称。在这里,我们只需简单的打印输出它的名称就可以了。对于基本的数据类型,只需按照下面的格式输出就OK:
fs << "iterationNr"@H_502_10@ << 100@H_502_10@;
读取:通过[]操作符,访问其地址;然后通过>>操作符或者转换操作,得到想要的值。
int@H_502_10@ itNr;
fs["iterationNr"@H_502_10@] >> itNr;
itNr = (int@H_502_10@) fs["iterationNr"@H_502_10@];
(3)OpenCV数据结构的输入输出。 @H_301_2@ 行为类似基本的C++类型:
Mat@H_502_10@ R@H_502_10@ = Mat_@H_502_10@<uchar >:@H_502_10@:eye@H_502_10@ (3@H_502_10@,T@H_502_10@ = Mat_@H_502_10@<double>:@H_502_10@:zeros@H_502_10@(3@H_502_10@,1@H_502_10@);
fs << "R"@H_502_10@ << R@H_502_10@; //@H_502_10@ Write@H_502_10@ cv:@H_502_10@:Mat@H_502_10@
fs << "T"@H_502_10@ << T@H_502_10@;
fs["R"@H_502_10@] >> R@H_502_10@; //@H_502_10@ Read@H_502_10@ cv:@H_502_10@:Mat@H_502_10@
fs["T"@H_502_10@] >> T@H_502_10@;
(4)矢量 (数组) 和 关联映射的输入输出: @H_301_2@ 正如我们事先提到的,我们当然也能输出映射和序列(矢量,数组)。首先,我们打印变量的名称,然后我们必须指定我们的输出是序列还是映射。 @H_301_2@ 对于序列,第一个元素之前打印“[“,以“]“字符结束:
fs << "strings"@H_502_10@ << "["@H_502_10@; // 字符串序列@H_502_10@
fs << "image1.jpg"@H_502_10@ << "Awesomeness"@H_502_10@ << "baboon.jpg"@H_502_10@;
fs << "]"@H_502_10@; // 关闭序列@H_502_10@
对于映射是一样的,只是用“{“,“}“指定而已:
fs << "Mapping"@H_502_10@; // 映射@H_502_10@
fs << "{"@H_502_10@ << "One"@H_502_10@ << 1@H_502_10@;
fs << "Two"@H_502_10@ << 2@H_502_10@ << "}"@H_502_10@;
当需要读取时,我们使用 FileNode 和 FileNodeIterator 数据结构进行操作。类FileStorage 的操作符[] 返回FileNode数据类型。如果是序列,可以使用 FileNodeIterator 去迭代所有的项:
// 读字符串序列 - 首先获取节点@H_502_10@
FileNode n = fs["strings"@H_502_10@];
if@H_502_10@ (n.type@H_502_10@() != FileNode::SEQ)
{
cerr << "strings is not a sequence! FAIL"@H_502_10@ << endl;
return@H_502_10@ 1@H_502_10@;
}
// 遍历整个节点@H_502_10@
FileNodeIterator it = n.begin@H_502_10@(),it_end = n.end@H_502_10@();
for@H_502_10@ (; it != it_end; ++it)
cout << (string)*it << endl;
对于映射,可以使用[]操作符访问指定的项,
n = fs["Mapping"@H_502_10@]; // 从一个序列中读取映射@H_502_10@
cout@H_502_10@ << "Two "@H_502_10@ << (int@H_502_10@)(n["Two"@H_502_10@]) << "; "@H_502_10@;
cout@H_502_10@ << "One "@H_502_10@ << (int@H_502_10@)(n["One"@H_502_10@]) << endl << endl;
(5)读写自定义数据结构 @H_301_2@ 假设你有如下的自定义数据结构:
class MyData
{
public@H_502_10@:
MyData@H_502_10@() : A@H_502_10@(0),X@H_502_10@(0),id@H_502_10@() {}
public@H_502_10@: // Data Members
int@H_502_10@ A;
double@H_502_10@ X;
string@H_502_10@ id;
};
// 该类的写入序列化实现@H_502_10@
void@H_502_10@ write(FileStorage& fs) const@H_502_10@
{
fs << "{"@H_502_10@ << "A"@H_502_10@ << A << "X"@H_502_10@ << X << "id"@H_502_10@ << id@H_502_10@ << "}"@H_502_10@;
}
// 该类的读取序列化实现@H_502_10@
void@H_502_10@ read(const@H_502_10@ FileNode& node)
{
A = (int@H_502_10@)node["A"@H_502_10@];
X = (double@H_502_10@)node["X"@H_502_10@];
id@H_502_10@ = (string)node["id"@H_502_10@];
}
void@H_502_10@ write(FileStorage& fs,const@H_502_10@ MyData& x)
{
x.write(fs);
}
void@H_502_10@ read(const@H_502_10@ FileNode& node,const@H_502_10@ MyData& default_value = MyData())
{
if@H_502_10@(node.empty())
x = default_value;
else@H_502_10@
x.read(node);
}
具体实例:
MyData m(1@H_502_10@);
fs << "MyData"@H_502_10@ << m; // your own data structures@H_502_10@
fs["MyData"@H_502_10@] >> m; // Read your own structure_@H_502_10@
或者尝试读取不存在的节点:
fs["NonExisting"@H_502_10@] >> m;
//@H_502_10@ Do@H_502_10@ not@H_502_10@ add a fs << "NonExisting"@H_502_10@ << m command for@H_502_10@ this to work
cout << endl << "NonExisting = "@H_502_10@ << endl << m << endl;
Write Done.
Reading:
100@H_502_10@image1.jpg
Awesomeness
baboon.jpg
Two 2@H_502_10@; One 1@H_502_10@
R = [1@H_502_10@,0@H_502_10@,0@H_502_10@;
0@H_502_10@,1@H_502_10@,1@H_502_10@]
T = [0@H_502_10@; 0@H_502_10@; 0@H_502_10@]
MyData =
{ id@H_502_10@ = mydata1234,X = 3.14159@H_502_10@,A = 97@H_502_10@}
Attempt to@H_502_10@ read@H_502_10@ NonExisting (should initialize the@H_502_10@ data structure with@H_502_10@ its@H_502_10@ default).
NonExisting =
{ id@H_502_10@ =,X = 0@H_502_10@,A = 0@H_502_10@}
Tip: Open up output.xml with@H_502_10@ a text@H_502_10@ editor to@H_502_10@ see the@H_502_10@ serialized data.
XML格式:
<?xml version="1.0"?>@H_502_10@
<opencv_storage@H_502_10@>@H_502_10@
<iterationNr@H_502_10@>@H_502_10@100</iterationNr@H_502_10@>@H_502_10@
<strings@H_502_10@>@H_502_10@
image1.jpg Awesomeness baboon.jpg</strings@H_502_10@>@H_502_10@
<Mapping@H_502_10@>@H_502_10@
<One@H_502_10@>@H_502_10@1</One@H_502_10@>@H_502_10@
<Two@H_502_10@>@H_502_10@2</Two@H_502_10@>@H_502_10@</Mapping@H_502_10@>@H_502_10@
<R@H_502_10@ type_id@H_502_10@="opencv-matrix"@H_502_10@>@H_502_10@
<rows@H_502_10@>@H_502_10@3</rows@H_502_10@>@H_502_10@
<cols@H_502_10@>@H_502_10@3</cols@H_502_10@>@H_502_10@
<dt@H_502_10@>@H_502_10@u</dt@H_502_10@>@H_502_10@
<data@H_502_10@>@H_502_10@
1 0 0 0 1 0 0 0 1</data@H_502_10@>@H_502_10@</R@H_502_10@>@H_502_10@
<T@H_502_10@ type_id@H_502_10@="opencv-matrix"@H_502_10@>@H_502_10@
<rows@H_502_10@>@H_502_10@3</rows@H_502_10@>@H_502_10@
<cols@H_502_10@>@H_502_10@1</cols@H_502_10@>@H_502_10@
<dt@H_502_10@>@H_502_10@d</dt@H_502_10@>@H_502_10@
<data@H_502_10@>@H_502_10@
0. 0. 0.</data@H_502_10@>@H_502_10@</T@H_502_10@>@H_502_10@
<MyData@H_502_10@>@H_502_10@
<A@H_502_10@>@H_502_10@97</A@H_502_10@>@H_502_10@
<X@H_502_10@>@H_502_10@3.1415926535897931e+000</X@H_502_10@>@H_502_10@
<id@H_502_10@>@H_502_10@mydata1234</id@H_502_10@>@H_502_10@</MyData@H_502_10@>@H_502_10@
</opencv_storage@H_502_10@>@H_502_10@
YAML文件格式:
%YAML@H_502_10@@H_502_10@:1.0
iterationNr: 100
strings:
- "image1.jpg"@H_502_10@ @H_502_10@ - Awesomeness@H_502_10@ @H_502_10@ - "baboon.jpg"@H_502_10@ @H_502_10@Mapping:
One: 1
Two: 2
R: !!opencv-matrix
rows: 3
cols: 3
dt: u
data: [ 1,1,1 ]
T: !!opencv-matrix
rows: 3
cols: 1
dt: d
data: [ 0.,0.,0. ]
MyData:
A: 97
X: 3.1415926535897931e+000
id: mydata1234