#include "opencv2/opencv.hpp"
#include <time.h>
using namespace cv;
using namespace std;
void read_ymal()
{
//初始化
FileStorage fs2("../test.yaml", FileStorage::READ);
// 第一种方法,对FileNode操作
int frameCount = (int)fs2["frameCount"];
// 第二种方法,使用FileNode运算符 >>
std::string date;
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;
//使用FileNodeIterator遍历序列
for( ; it != it_end; ++it, idx++ )
{
cout << "feature #" << idx << ": ";
cout << "x=" << (int)(*it)["x"] << ", y=" << (int)(*it)["y"] << ", lbp: (";
// 我们也可以使用使用filenode > > std::vector操作符很容易的读数值阵列
(*it)["lbp"] >> lbpval;
for( int i = 0; i < (int)lbpval.size(); i++ )
cout << " " << (int)lbpval[i];
cout << ")" << endl;
}
fs2.release();
}
void write_yaml()
{
//初始化,此文件由程序自动创建
FileStorage fs("../test.yaml", FileStorage::WRITE);
fs << "frameCount" << 4;
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, 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();
}
int main()
{
write_yaml();
read_ymal();
// getchar();
return 0;
}
#include<opencv/opencv2.hpp>
//读、写 YML 文件(首先将需要的数据通过 writeYML() 写入 YML 文件中,再读 YML 文件)
/*********** eg. paramters.yml ************************
%YAML:1.0(如果想要用 pyyaml 工具绘图,则需要删除这句)
---(如果想要用 pyyaml 工具绘图,则需要删除这句)
#number: 1 #int
#number2: 1.1000000238418579e+00 #float: 1.10
#number3: 1.1000000000000001e+00 #double: 1.10 由此可见,对YML文件写入数据一般不能手动键盘写入
#String: a #string
#mat: !!opencv-matrix #Mat (如果想要用 pyyaml 工具绘图,则需要删除!!opencv-matrix)
# rows: 3
# cols: 3
# dt: d
# data: [ 1000., 0., 320., 0., 1000., 240., 0., 0., 1. ]
************ paramters.yml *************************/
void writeYML() { //文件不存在则自动创建
FileStorage fs("./paramters.yml", FileStorage::WRITE);
if (fs.isOpened()) {
int number = 1;
string String = "a";
Mat mat = (Mat_<double>(3, 3) << 1000, 0, 320, 0, 1000, 240, 0, 0, 1);
fs << "number" << number;
fs << "String" << String;
fs << "mat" << mat;
fs.release();
}
else cout << " can't create YML file " << endl;
}
void readYML() {
FileStorage fs("./paramters.yml", FileStorage::READ);
if (fs.isOpened()) {
int number;
string String;
Mat mat;
fs["number"] >> number;
fs["String"] >> String;
fs["mat"] >> mat;
cout << number << endl << String << endl << mat << endl;
fs.release();
}
else cout << " can't open YML file " << endl;
}
int main(){
writeYML();
//readYML();
}
//读 txt 文件
/********** parameters.txt **********************
#注释只能单行显示,未读到的计为 0
string=ORB
number_float=325.5
num_int=123
********* parameters.txt ***********************/
#include<iostream>
#include <opencv2/opencv.hpp>
#include<fstream>
using namespace cv;
using namespace std;
class ParameterReader {
public:
ParameterReader(string filename = "./parameters.txt") {
ifstream fin(filename.c_str());
if (!fin) {
cerr << " parameters.txt does not exist. " << endl;
return;
}
while (!fin.eof()) {
string str;
getline(fin, str);
if (str[0] == '#') // 以‘#’开头的是注释
continue;
int pos = str.find("=");
if (pos == -1)
continue;
string key = str.substr(0, pos);
string value = str.substr(pos + 1, str.length());
data[key] = value;
if (!fin.good())
break;
}
}
string getData(string key) {
map<string, string>::iterator iter = data.find(key);
if (iter == data.end()) {
cerr << " parameter name '" << key << "' not found!" << endl;
return string("NOT_FOUND");
}
return iter->second;
}
public:
map<string, string> data; //类型要注意转换
};
struct PARAMETER {
int num_int;
float num_float;
string str;
};
void passParameter(PARAMETER& parameter) {
ParameterReader pd;
parameter.num_int = atoi(pd.getData("num_int").c_str()); //如果找不到参数,则返回一个空值
parameter.num_float = atof(pd.getData("num_float").c_str());
parameter.str = pd.getData("str");
}
int main() {
PARAMETER parameter;
passParameter(parameter);
//bool visualize = pd.getData("visualize_pointcloud")==string("yes");
cout << parameter.str << endl << parameter.num_float << endl << parameter.num_int << endl;
}
bin 文件读写
文本文件与二进制文件的区别:
写入数字 1,实际写入的是 字符 ‘1’
二进制文件:写数字1,实际写入的是整数 1 ;
1的二进制是0001 (所占4个字节,最低是1, 追高3个字节都是0);
https://blog.csdn.net/lightlater/article/details/6364931
// 写16位数据
std::ofstream ofstr("./data.bin");
int nNum = 20;
std::string str("Hello, world");
fout.write((char*)&nNum, sizeof(int)); // 传入类型必须为char,只要给定地址长度就能读到原类型值数据
fout.write(str.c_str(), sizeof(char) * (str.size()));
ofstr.close();