OpenCV 4.13.0
开源计算机视觉库 (Open Source Computer Vision)
正在加载...
正在搜索...
未找到匹配项
samples/cpp/tutorial_code/core/file_input_output/file_input_output.cpp

一个使用 FileStorage 接口的完整示例。有关更多详细信息,请参见 相应的教程

#include <opencv2/core.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
static void help(char** av)
{
cout << endl
<< av[0] << " 展示了 OpenCV 序列化功能的用法。" << endl << endl
<< "用法: " << endl
<< av[0] << " [输出文件名] (默认输出文件为 outputfile.yml.gz)" << endl << endl
<< "输出文件可以是 XML (xml)、YAML (yml/yaml) 或 JSON (json)。" << endl
<< "您甚至可以通过在其扩展名中指定来压缩它,例如 xml.gz yaml.gz 等..." << endl
<< "使用 FileStorage,您可以通过使用 << 和 >> 运算符来序列化 OpenCV 中的对象" << endl
<< "例如: - 创建一个类并对其进行序列化" << endl
<< " - 使用它来读写矩阵。" << endl << endl;
}
class MyData
{
public:
MyData() : A(0), X(0), id()
{}
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
static void write(FileStorage& fs, const std::string&, const MyData& x)
{
x.write(fs);
}
static void read(const FileNode& node, MyData& x, const MyData& default_value = MyData()){
if(node.empty())
x = default_value;
else
x.read(node);
}
// This function will print our custom class to the console
static ostream& operator<<(ostream& out, const MyData& m)
{
out << "{ id = " << m.id << ", ";
out << "X = " << m.X << ", ";
out << "A = " << m.A << "}";
return out;
}
int main(int ac, char** av)
{
string filename;
if (ac != 2)
{
help(av);
filename = "outputfile.yml.gz";
}
else
filename = av[1];
{ //write
Mat R = Mat_<uchar>::eye(3, 3),
MyData m(1);
FileStorage fs(filename, FileStorage::WRITE);
// 或
// FileStorage fs;
// fs.open(filename, FileStorage::WRITE);
fs << "iterationNr" << 100;
fs << "strings" << "["; // text - string sequence
fs << "image1.jpg" << "Awesomeness" << "../data/baboon.jpg";
fs << "]"; // close sequence
fs << "Mapping"; // text - mapping
fs << "{" << "One" << 1;
fs << "Two" << 2 << "}";
fs << "R" << R; // cv::Mat
fs << "T" << T;
fs << "MyData" << m; // your own data structures
fs.release(); // explicit close
cout << "写入文件:" << filename << " 操作已成功完成。" << endl;
}
{//read
cout << endl << "正在读取: " << endl;
fs.open(filename, FileStorage::READ);
int itNr;
//fs["iterationNr"] >> itNr;
itNr = (int) fs["iterationNr"];
cout << itNr;
if (!fs.isOpened())
{
cerr << "无法打开 " << filename << endl;
help(av);
return 1;
}
FileNode n = fs["strings"]; // Read string sequence - Get node
if (n.type() != FileNode::SEQ)
{
cerr << "strings 不是序列!失败" << endl;
return 1;
}
FileNodeIterator it = n.begin(), it_end = n.end(); // Go through the node
for (; it != it_end; ++it)
cout << (string)*it << endl;
n = fs["Mapping"]; // Read mappings from a sequence
cout << "Two " << (int)(n["Two"]) << "; ";
cout << "One " << (int)(n["One"]) << endl << endl;
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 << "尝试读取 NonExisting (应该使用其默认值初始化数据结构)。";
fs["NonExisting"] >> m;
cout << endl << "NonExisting = " << endl << m << endl;
}
cout << endl
<< "提示: 用文本编辑器打开 " << filename << " 来查看序列化数据。" << endl;
return 0;
}
用于迭代序列和映射。
定义 persistence.hpp:595
文件存储节点类。
定义 persistence.hpp:441
FileNodeIterator begin() const
返回指向第一个节点元素的迭代器
FileNodeIterator end() const
返回指向最后一个节点元素之后的元素的迭代器
bool empty() const
如果节点为空,则返回 true
int type() const
返回节点的类型。
XML/YAML/JSON 文件存储类,封装了写入或读取所需的所有信息...
定义 persistence.hpp:261
virtual bool open(const String &filename, int flags, const String &encoding=String())
打开文件。
virtual bool isOpened() const
检查文件是否已打开。
virtual void release()
关闭文件并释放所有内存缓冲区。
派生自 Mat 的模板矩阵类。
定义于 mat.hpp:2296
n 维密集数组类
定义于 mat.hpp:840
std::ostream & operator<<(std::ostream &, const DualQuat< _Tp > &)
#define CV_PI
定义见 cvdef.h:382
int main(int argc, char *argv[])
定义 highgui_qt.cpp:3
定义 core.hpp:107
void write(FileStorage &fs, const String &name, int value)
void read(const FileNode &node, int &value, int default_value)
STL 命名空间。