OpenCV 4.12.0
开源计算机视觉
加载中...
搜索中...
无匹配项
XML/YAML/JSON 持久化

详细描述

XML/YAML/JSON 文件存储。

写入文件存储。

您可以将各种 OpenCV 数据结构存储到 XML (http://www.w3c.org/XML)、YAML (http://www.yaml.org) 或 JSON (http://www.json.org/) 格式,然后再从这些格式中恢复。此外,还可以存储和加载任意复杂的数据结构,这些数据结构包括 OpenCV 数据结构,以及基本数据类型(整数和浮点数以及文本字符串)作为其元素。

使用以下步骤将内容写入 XML、YAML 或 JSON

  1. 创建新的 FileStorage 并打开它以进行写入。可以使用对 FileStorage::FileStorage 构造函数的单个调用来完成,该构造函数采用文件名,也可以使用默认构造函数,然后调用 FileStorage::open。文件格式(XML、YAML 或 JSON)由文件名扩展名决定(分别为“.xml”、“.yml”/“.yaml”和“.json”)
  2. 使用流操作符 << 写入所有要写入的数据,就像 STL 流的情况一样。
  3. 使用 FileStorage::release 关闭文件。FileStorage 析构函数也会关闭文件。

这是一个例子

#include "opencv2/core.hpp"
#include <time.h>
using namespace cv;
int main(int, char** argv)
{
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();
return 0;
}
XML/YAML/JSON 文件存储类,封装了写入或读取数据所需的所有信息...
定义 persistence.hpp:261
@ WRITE
value,打开文件以进行写入
定义 persistence.hpp:267
从 Mat 派生的模板矩阵类。
定义 mat.hpp:2257
n 维密集数组类
定义 mat.hpp:830
void release()
递减引用计数器并在需要时释放矩阵。
I.at<uchar>(y, x) = saturate_cast<uchar>(r);
uchar
unsigned char uchar
int main(int argc, char *argv[])
定义 highgui_qt.cpp:3
定义 core.hpp:107

上面的示例将整数、文本字符串(校准日期)、2 个矩阵和一个自定义结构“feature”存储到 YML,其中包含特征坐标和 LBP(局部二值模式)值。以下是示例的输出

%YAML:1.0
frameCount: 5
calibrationDate: "Fri Jun 17 14:09:29 2011\n"
cameraMatrix: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ 1000., 0., 320., 0., 1000., 240., 0., 0., 1. ]
distCoeffs: !!opencv-matrix
rows: 5
cols: 1
dt: d
data: [ 1.0000000000000001e-01, 1.0000000000000000e-02,
-1.0000000000000000e-03, 0., 0. ]
features
- { x:167, y:49, lbp:[ 1, 0, 0, 1, 1, 0, 1, 1 ] }
- { x:298, y:130, lbp:[ 0, 0, 0, 1, 0, 0, 1, 1 ] }
- { x:344, y:158, lbp:[ 1, 1, 0, 0, 0, 0, 1, 0 ] }

作为练习,您可以将示例中的“.yml”替换为“.xml”或“.json”,并查看相应的 XML 文件会是什么样子。

通过查看示例代码和输出,可以注意到以下几点

从文件存储中读取数据。

要读取先前写入的 XML、YAML 或 JSON 文件,请执行以下操作

  1. 使用 FileStorage::FileStorage 构造函数或 FileStorage::open 方法打开文件存储。在当前的实现中,整个文件被解析,并且文件存储的整个表示在内存中构建为文件节点的层次结构(参见 FileNode
  2. 读取您感兴趣的数据。使用 FileStorage::operator []FileNode::operator [] 和/或 FileNodeIterator
  3. 使用 FileStorage::release 关闭存储。

以下是如何读取由上面的代码示例创建的文件

FileStorage fs2("test.yml", FileStorage::READ);
// 第一种方法:在 FileNode 上使用 (type) 运算符。
int frameCount = (int)fs2["frameCount"];
String date;
// 第二种方法:使用 FileNode::operator >>
fs2["calibrationDate"] >> date;
Mat cameraMatrix2, distCoeffs2;
fs2["cameraMatrix"] >> cameraMatrix2;
fs2["distCoeffs"] >> distCoeffs2;
cout << "frameCount: " << frameCount << endl
<< "calibration date: " << date << endl
<< "camera matrix: " << cameraMatrix2 << endl
<< "distortion coeffs: " << 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();
用于迭代序列和映射。
定义 persistence.hpp:595
文件存储节点类。
定义 persistence.hpp:441
FileNodeIterator begin() const
返回指向第一个节点元素的迭代器
FileNodeIterator end() const
返回指向最后一个节点元素之后的元素的迭代器
@ READ
值,打开文件进行读取
定义 persistence.hpp:266
std::string String
定义 cvstd.hpp:151

格式规范

([count]{u|c|w|s|i|f|d})... 其中字符对应于基本 C++ 类型

count 是给定类型的可选值计数器。例如,2if 表示每个数组元素是一个包含 2 个整数的结构,后跟一个单精度浮点数。上述规范的等效表示法是 iif2i1f 等。其他示例:u 表示数组由字节组成,2d 表示数组由双精度对组成。

另请参见
samples/cpp/tutorial_code/core/file_input_output/file_input_output.cpp

类  cv::FileNode
 文件存储 Node 类。更多...
 
类  cv::FileNodeIterator
 用于迭代序列和映射。更多...
 
类  cv::FileStorage
 XML/YAML/JSON 文件存储类,封装了将数据写入文件或从文件读取数据所需的所有信息。更多...