Opencv将数据保存到xml、yaml / 从xml、yaml读取数据

1. 官方例程

1.1 写数据

#include "opencv2/core.hpp"
#include <time.h>
using namespace cv;
int main(int, char** argv)
{
     FileStorage fs("test.yml", FileStorage::WRITE);
     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;
}

output :

%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 ] }

1.2 读数据

FileStorage fs2("test.yml", FileStorage::READ);

// first method: use (type) operator on FileNode.
int frameCount = (int)fs2["frameCount"];
String date;

// second method: use 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;

// iterate through a sequence using FileNodeIterator
for( ; it != it_end; ++it, idx++ )
{
     cout << "feature #" << idx << ": ";
     cout << "x=" << (int)(*it)["x"] << ", y=" << (int)(*it)["y"] << ", lbp: (";

     // you can also easily read numerical arrays using FileNode >> std::vector operator.
     (*it)["lbp"] >> lbpval;
     for( int i = 0; i < (int)lbpval.size(); i++ )
         cout << " " << (int)lbpval[i];
         cout << ")" << endl;
}
fs2.release();

2. 读写xml

#include <iostream>
#include "opencv2/opencv.hpp"
 
using namespace std;
 
#define WRITE_OR_READ
 
int main() {
//===========将数据写入到xml文件中================
#ifdef WRITE_OR_READ 
    string name = "insomnia";
    int age = 18;
    float height = 1.83;
    char sex = 'M';
    cv::Mat matrix_eye = cv::Mat::eye(3, 3, CV_64F);
 
    cv::FileStorage fs("./test.xml", cv::FileStorage::WRITE);//会覆盖当前文件,不存在则新建文件
    if (fs.isOpened())
    {
        fs << "name" << name << "age" << age << "height" << height << "sex" << sex;//可以连续写入
        fs << "matrix_eye" << matrix_eye;//也可以依次写入
        fs.release();//release after used
    }
//===========从xml文件中读取数据================
#else 
    string name;
    int age;
    float height;
    char sex;
    cv::Mat matrix_eye;
 
    cv::FileStorage fs("./test.xml", cv::FileStorage::READ);
    if (fs.isOpened()) {
        fs["name"] >> name;
        fs["age"] >> age;
        fs["height"] >> height;
        int temp;
        fs["sex"] >> temp;//这里不能直接读到char,所以转换了一下
        sex = (char)temp;
        fs["matrix_eye"] >> matrix_eye;
 
        fs.release();
 
        cout << "name: " << name << endl;
        cout << "age: " << age << endl;
        cout << "height: " << height << endl;
        cout << "sex: " << sex << endl;
        cout << "matrix_eye: " << endl << matrix_eye << endl;
 
        cout << "matrix_eye.size().height: " << matrix_eye.size().height << endl;
        cout << "matrix_eye.at<double>(1, 0): " << matrix_eye.at<double>(1, 0) << endl;
    }
#endif
 
    return 0;
}

将数据写入到xml文件后,打开查看一下在这里插入图片描述
格式是自动生成的,只是将数据填充了进去。可以看到第一行是xml版本信息,不用考虑。第二行和最后一行是最外层的标签。

然后所有保存的数据都是一个并列的关系,同等级。只是像Mat这种数据类型,又有细分属性而已。

从xml文件中读取一下数据,看下输出结果在这里插入图片描述

3. 读写yaml

#include<opencv2/opencv.hpp>
#include<iostream>
 
using namespace std;
using namespace cv;
 
//#define WRITE_OR_READ
 
int main()
{
#ifdef WRITE_OR_READ
	//1.创建文件
	cv::FileStorage fwrite("./test.yaml", cv::FileStorage::WRITE);
	//2.写入数据
	string name = "insomnia";
	int age = 18;
	float height = 1.83;
	char sex = 'M';
	cv::Mat matrix_eye = cv::Mat::eye(3, 3, CV_64F);
	fwrite << "name " << name;
	fwrite << "age " << age;
	fwrite << "height " << height;
	fwrite << "sex " << sex;
	fwrite << "matrix_eye " << matrix_eye;
	//3.关闭文件
	fwrite.release();
	return 0;
#else
	//1.读取文件指针	
	string strSettingsFile = "./test.yaml";
	cv::FileStorage fread(strSettingsFile.c_str(), cv::FileStorage::READ);
 
	//2.判断是否打开成功
	if (!fread.isOpened())
	{
		cout << "Failed to open settings file at: " << strSettingsFile << endl;
		return 0;
	}
	else cout << "success to open file at: " << strSettingsFile << endl;
 
	//3.打开文件后读取数据
	string name;
	int age;
	float height;
	char sex;
	cv::Mat matrix_eye;
 
	fread["name"] >> name;
	fread["age"] >> age;
	fread["height"] >> height;
	int temp;
	fread["sex"] >> temp;
	sex = (char)temp;
	fread["matrix_eye"] >> matrix_eye;
	cout << "name=" << name << endl;
	cout << "age=" << age << endl;
	cout << "height=" << height << endl;
	cout << "sex=" << sex << endl;
	cout << "matrix_eye=" << endl << matrix_eye << endl;
 
	cout << matrix_eye.size().height << endl;
 
	//4.关闭文件
	fread.release();
	return 0;
#endif
 
}
 

保存的yaml文件
在这里插入图片描述
读取文件的结果在这里插入图片描述

4. 保存矩阵与点集

//写数据
cv::FileStorage fs;
std::string label_ = "abc.xml";
fs.open(label_.c_str(), cv::FileStorage::WRITE);

std::string str_ = "image" + std::to_string(i+1);
cv::Mat _pts(p_result); //p_result define:std::vector<cv::Point2f>p_result;
fs << str_ << _pts;
fs.release();

//**************************
//读数据
cv::FileStorage fs;
fs.open( "abc.xml", cv::FileStorage::READ);
cv::Mat m_pts;
fs[str] >> m_pts;
std::vector<cv::Point2f>pts;
for (int i = 1; i < m_pts.rows; ++i)
{
	cv::Point2f _pt(m_pts.ptr<float>(i, 0)[0], m_pts.ptr<float>(i, 0)[1]);
	pts.push_back(_pt);
	std::cout << _pt << "\n";
}

5.读写 xml/yaml使用实例

5.1 写入

使用以下过程将内容写入 XML、YAML 或 JSON:

- 创建新的 FileStorage 并打开它进行写入。可以通过对采用文件名的 FileStorage::FileStorage 构造函数的单个调用来完成此操作,
也可以使用默认构造函数,然后调用 FileStorage::open。文件的格式(XML、YAML 或 JSON)由文件扩展名确定(分别为“.xml”、
“.yml”/“.yaml”和“.json”)。
- 使用 streaming operator 写入所需的所有数据,就像在 STL 流中一样。
- 使用 FileStorage::release 关闭文件。FileStorage 析构函数也会关闭该文件。
#include<opencv2\opencv.hpp>
#include<opencv2\highgui\highgui.hpp>
 
using namespace std;
using namespace cv;
 
typedef struct
{
    int x;
    int y;
    string s;
}test_t;
 
 
int main(int argc, char** argv)
{
    FileStorage fs("test.xml", FileStorage::WRITE); //填入写操作
 
    //测试数据
    int a1 = 2;
    char a2 = -1;
    string str = "hello sysu!";
    int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
    test_t t = { 3,4,"hi sysu" };
    map<string, int> m;
    m["kobe"] = 100;
    m["james"] = 99;
    m["curry"] = 98;
 
    //写入文件操作,先写标注在写数据
    fs << "int_data" << a1;
    fs << "char_data" << a2;
    fs << "string_data" << str;
 
    //写入数组
    fs <<"array_data"<< "["; //数组开始
    for (int i = 0; i < 10; i++)
    {
        fs << arr[i];
    }
    fs << "]"; //数组结束
 
    //写入结构体
    fs << "struct_data" << "{"; //结构体开始
    fs << "x" << t.x;
    fs << "y" << t.y;
    fs << "s" << t.s;
    fs << "}";  //结构结束
 
 
    //map的写入
    fs << "map_data" << "{";  //map的开始写入
    map<string, int>::iterator it = m.begin();
    for (; it != m.end(); it++)
    {
        fs << it->first << it->second;
    }
    fs << "}";  //map写入结束
 
 
    return 0;
}
  • 其结果:
%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 ] }

5.2 读取

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

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

#include<opencv2\opencv.hpp>
#include<opencv2\highgui\highgui.hpp>
 
using namespace std;
using namespace cv;
 
typedef struct
{
    int x;
    int y;
    string s;
}test_t;
 
 
int main(int argc, char** argv)
{
    FileStorage fs2("test.yml", FileStorage::READ);
    // first method: use (type) operator on FileNode.
    int frameCount = (int)fs2["frameCount"];
    String date;
    // second method: use 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;
    // iterate through a sequence using FileNodeIterator
    for( ; it != it_end; ++it, idx++ )
    {
        cout << "feature #" << idx << ": ";
        cout << "x=" << (int)(*it)["x"] << ", y=" << (int)(*it)["y"] << ", lbp: (";
        // you can also easily read numerical arrays using FileNode >> std::vector operator.
        (*it)["lbp"] >> lbpval;
        for( int i = 0; i < (int)lbpval.size(); i++ )
            cout << " " << (int)lbpval[i];
        cout << ")" << endl;
    }
    fs2.release();
    return 0;
}

参考:

1.OpenCV 读写xml和yml文件

GitHub 加速计划 / opencv31 / opencv
77.37 K
55.71 K
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:2 个月前 )
2756c20e WinRT/UWP build: fix more warnings in media part 1 天前
7654d06b - 2 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐