C++使用opencv库实现读写yaml文件参数
opencv
OpenCV: 开源计算机视觉库
项目地址:https://gitcode.com/gh_mirrors/opencv31/opencv
·
1. 写入文件
1.1 代码
#include <opencv2/opencv.hpp>
#include <time.h>
#include <iostream>
int main(int argc, char** argv)
{
//write a yaml file
cv::FileStorage fs_write("./test.yaml", cv::FileStorage::WRITE);
time_t rawtime;
time(&rawtime);
fs_write << "Time" << asctime(localtime(&rawtime));
std::vector<int> image_size = {1280, 800};
fs_write << "imageSize" << image_size;
cv::Mat camera_matrix = (cv::Mat_<double>(3, 3) << 190, 0, 640,
0, 190, 400,
0, 0, 1);
cv::Mat distort_coefficient = (cv::Mat_<double>(5, 1) << 0.1, 0.01, -0.001, 0, 0);
fs_write << "cameraMatrix" << camera_matrix << "distCoeffs" << distort_coefficient;
fs_write.release();
return 0;
}
1.2 说明
- 使用
cv::FileStorage创建文件对象,打开文件 - 使用
<<符号向文件中写入信息 release()关闭文件
1.3 输出文件结果
%YAML:1.0
---
Time: "Thu Nov 28 14:02:31 2019\n"
imageSize: [ 1280, 800 ]
cameraMatrix: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ 190., 0., 640., 0., 190., 400., 0., 0., 1. ]
distCoeffs: !!opencv-matrix
rows: 5
cols: 1
dt: d
data: [ 1.0000000000000001e-01, 1.0000000000000000e-02,
-1.0000000000000000e-03, 0., 0. ]
2. 读取文件
2.1 代码
#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
int main(int argc, char **argv)
{
//read a yaml file
cv::FileStorage fs_read("./test.yaml", cv::FileStorage::READ);
std::string time;
//second method:use FileNode::operator >>
fs_read["Time"] >> time;
std::vector<int> image_size;
fs_read["imageSize"] >> image_size;
cv::Mat camera_matrix, distort_coefficient;
fs_read["cameraMatrix"] >> camera_matrix;
fs_read["distCoeffs"] >> distort_coefficient;
std::cout << "Time: " << time << std::endl
<< "cameraMatrix: " << camera_matrix << std::endl
<< "distCoeffs: " << distort_coefficient << std::endl;
fs_read.release();
return 0;
}
REF:https://blog.csdn.net/feiyang_luo/article/details/103291475
OpenCV: 开源计算机视觉库
最近提交(Master分支:4 个月前 )
74addff3
docs: fix spelling errors in code and comments 11 小时前
4cfd9689
Fix UB in cv::error when breakOnError set to true 13 小时前
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐


所有评论(0)