原创文章点击这里
1. yaml 文件的写入
#include <iostream>
#include<opencv2/opencv.hpp
int main()
{
std::string p = "default2.yaml";
cv::FileStorage fwrite(p, cv::FileStorage::WRITE);
int a = 100;
float b = 100.1;
std::string c = "abc";
cv::Mat d = cv::Mat::ones(3,3,CV_8U);
cv::Mat e = (cv::Mat_<double>(3,3) << 1000, 0, 320, 0, 1000, 240, 0, 0, 1);
fwrite << "a" << a;
fwrite << "b" << b;
fwrite << "c" << c;
fwrite << "d" << d;
fwrite << "e" << e;
fwrite << "title1" << "{";
fwrite << "title1-1" << 100;
fwrite << "title1-2" << 200;
fwrite << "}";
fwrite << "title2" << "[";
fwrite << "title2-1";
fwrite << "title2-2";
fwrite << "]";
fwrite.release();
return 0;
}
%YAML:1.0
a: 100
b: 100.1
c: abc
d: !!opencv-matrix
rows: 3
cols: 3
dt: i
data: [ 1, 1, 1, 1, 1, 1, 1, 1, 1 ]
e: !!opencv-matrix
rows: 3
cols: 3
dt: i
data: [ 1000, 0, 320, 0, 1000, 240, 0, 0, 1 ]
title1:
title1-1: 100
title1-2: 200
title2:
- title2-1
- title2-2
2. yaml 文件的读取
#include <iostream>
#include<opencv2/opencv.hpp
int main()
{
std::string p = "default2.yaml";
cv::FileStorage fread(p.c_str(), cv::FileStorage::READ);
if(!fread.isOpened())
return 0 ;
int a;
float b;
char c;
std::string d;
cv::Mat e;
cv::Mat f;
int title1_1;
fread["a"] >> a;
fread["b"] >> b;
fread["c"] >> c;
fread["d"] >> d;
fread["e"] >> e;
fread["title1"]["title1-1"] >> title1_1;
fread.release();
return 0;
}