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

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

1. 官方例程

1.1 写数据

cpp 复制代码
#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 :

cpp 复制代码
%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 读数据

cpp 复制代码
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

cpp 复制代码
#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

cpp 复制代码
#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. 保存矩阵与点集

cpp 复制代码
//写数据
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";
}
相关推荐
AI_NEW_COME9 分钟前
构建全方位大健康零售帮助中心:提升服务与体验
大数据·人工智能
IT古董15 分钟前
【机器学习】机器学习的基本分类-强化学习-Actor-Critic 方法
人工智能·机器学习·分类
martian66515 分钟前
【人工智能数学基础】——深入详解贝叶斯理论:掌握贝叶斯定理及其在分类和预测中的应用
人工智能·数学·分类·数据挖掘·贝叶斯
mingo_敏16 分钟前
深度学习中的并行策略概述:2 Data Parallelism
人工智能·深度学习
呆呆的猫22 分钟前
【LeetCode】227、基本计算器 II
算法·leetcode·职场和发展
Tisfy24 分钟前
LeetCode 1705.吃苹果的最大数目:贪心(优先队列) - 清晰题解
算法·leetcode·优先队列·贪心·
余额不足1213844 分钟前
C语言基础十六:枚举、c语言中文件的读写操作
linux·c语言·算法
終不似少年遊*1 小时前
美国加州房价数据分析01
人工智能·python·机器学习·数据挖掘·数据分析·回归算法
区块链小八歌1 小时前
链原生 Web3 AI 网络 Chainbase 推出 AVS 主网, 拓展 EigenLayer AVS 场景
人工智能
禾高网络1 小时前
租赁小程序成品|租赁系统搭建核心功能
java·人工智能·小程序