/******************************************************
测试程序 - 【 File 输入输出】
时间:2016年8月24日
******************************************************/
static void help(string av)
{
cout << endl
<< av << " shows the usage of the OpenCV serialization functionality." << endl
<< "usage: " << endl
<< av << " outputfile.yml.gz" << endl
<< "The output file may be either XML (xml) or YAML (yml/yaml). You can even compress it by "
<< "specifying this in its extension like xml.gz yaml.gz etc... " << endl
<< "With FileStorage you can serialize objects in OpenCV by using the << and >> operators" << endl
<< "For example: - create a class and have it serialized" << endl
<< " - use it to read and write matrices." << endl;
}
class MyData
{
public:
//1、空构造
MyData() : A(0), X(0), id()
{}
//2、带参构造
explicit MyData(int) : A(97), X(CV_PI), id("mydata1234") // explicit to avoid implicit conversion
{}
void write(FileStorage& fs) const //Write serialization for this class
{
//根据文件读写的格式,输入映射的数据【集合名称已输入】
fs << "{" << "A" << A << "X" << X << "id" << id << "}";
}
void read(const FileNode& node) //Read serialization for this class
{
//读取结点信息
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
/*
参数分析:
FileStorage& fs//文件头
const std::string& //
const MyData& x //结点数据
*/
static void write(FileStorage& fs, const std::string&, const MyData& x)
{
x.write(fs);
}
/*
参数分析:
const FileNode& node, //文件结点
MyData& x, //MyDada x
const MyData& default_value = MyData() //MyDate 默认值【构造空间】
*/
static void read(const FileNode& node, MyData& x, const MyData& default_value = MyData())
{
if(node.empty())
x = default_value;
else
x.read(node);
}
/*
【自定义类的控制台输出——重载<<】
参数分析:
ostream& out, //输出流out
const MyData& m //MyData m
*/
static ostream& operator<<(ostream& out, const MyData& m)
{
out << "{ id = " << m.id << ", ";
out << "X = " << m.X << ", ";
out << "A = " << m.A << "}";
return out; //返回输出流
}
int main(void)
{
//【3、Opencv数据结构的输入输出】
//【1、打开,关闭XML、YAML文件】
string filename = "AV";
{
//write
Mat R = Mat_<uchar>::eye(3, 3),
T = Mat_<double>::zeros(3, 1);
MyData m(1);
//【**文件存储类型】 对象fs
FileStorage fs(filename, FileStorage::WRITE); //写方式打开
fs << "iterationNr" << 100;
fs << "strings" << "["; // 【文本格式 (开始)“name”+ “[”】 【文本——字符串】text - string sequence
fs << "image1.jpg" << "Awesomeness" << "baboon.jpg";
fs << "]"; // 【文本格式(结束)“]”】 close sequence
fs << "Mapping"; // 【文本格式 (开始)“name”+ “{”】 【文本——映射】 text - mapping
fs << "{" << "One" << 1; // 【文本格式 “name”+ i】
fs << "Two" << 2 << "}"; // 【文本格式(结束)“}”】
fs << "R" << R; // cv::Mat
fs << "T" << T;
fs << "MyData" << m; // 【自定义数据结构对象】
fs.release(); // explicit close
cout << "Write Done." << endl;
}
{//read
cout << endl << "Reading: " << endl; //显示
//*****************************************************
FileStorage fs; //文件存储类型 对象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;
help(filename);
return 1;
}
//*****************************************************
//【**获取结点】Read string sequence - Get node
FileNode n = fs["strings"];
if (n.type() != FileNode::SEQ)
{
cerr << "strings is not a sequence! FAIL" << endl;
return 1;
}
// 【**遍历结点,读取string】Go through the node
FileNodeIterator it = n.begin(), it_end = n.end();
for (; it != it_end; ++it)
cout << (string)*it << endl;
// 【读取映射】Read mappings from a sequence
n = fs["Mapping"];
cout << "Two " << (int)(n["Two"]) << "; ";
cout << "One " << (int)(n["One"]) << endl << endl;
//【定义新空间,接受读取的信息,Mat,自定义】
MyData m;
Mat R, T;
fs["R"] >> R; // Read cv::Mat
fs["T"] >> T;
fs["MyData"] >> m; // Read your own structure_
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)."<<endl;
cout << "试读取不存在的结点[\"NonExisting\"](应当使用默认值初始化该数据结构).";
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;
cout<< "小贴士: 使用文本编辑器打开 " << filename << "查看序列化的数据." << endl;
return 0;
}
【opencv练习13 - File 输入输出】
最新推荐文章于 2022-04-06 17:35:39 发布