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";
}
相关推荐
浮链序9 分钟前
怎么证明"你这个模型是偷我的"——把蒸馏变成取证工具
算法·安全·llm
小O的算法实验室10 分钟前
IEEE TMC,面向异构物联网的多目标量子行为拓扑优化算法
算法
IT_陈寒21 分钟前
SpringBoot自动配置差点让我加班到凌晨
前端·人工智能·后端
yumgpkpm35 分钟前
Acceldata ODP(Open Data Platform)3.3.6.4(RHEL9)保姆级完整安装手册
大数据·人工智能·hive·hadoop·kafka·hbase·cloudera
程序员cxuan37 分钟前
腾讯又来一王炸,开源版 WorkBuddy 太夯了!
人工智能·后端·程序员
邓工说电1 小时前
智慧断路器安全吗?数据加密、离线保护与合规认证全解读
大数据·数据库·人工智能·智能断路器·炜晔科技
阿里云大数据AI技术1 小时前
Lance 数据检索怎么选,当然阿里云 Milvus 向量湖
人工智能
出海客1 小时前
跨境电商多语言客服知识库怎么建:资料结构、检索边界与人工升级
大数据·人工智能
liliangcsdn1 小时前
派息率指标的应用探索和分析
大数据·算法
xsd202411181 小时前
从自主导航到视觉读表:一台工业巡检机器人的全栈技术链路拆解
人工智能