700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > OpenCV学习笔记(四):XML YAML(.txt .doc)文件读写操作

OpenCV学习笔记(四):XML YAML(.txt .doc)文件读写操作

时间:2020-02-04 07:38:17

相关推荐

OpenCV学习笔记(四):XML YAML(.txt .doc)文件读写操作

OpenCV学习笔记(四):XML,YAML(.txt,.doc)文件读写操作

一、Write_XML_and_YAML_File(写入XML)

#include <opencv2/opencv.hpp>#include <time.h>using namespace cv;int main(){// 1、准备文件写操作FileStorage fs("F:/C++/2. OPENCV 3.1.0/TEST/test.txt", FileStorage::WRITE);// 2、开始文件写入fs << "frameCount" << 5;// 3、写入时间time_t rawtime; // 64-bit time valuetime(&rawtime);fs << "calibrationDate" << asctime(localtime(&rawtime));// 4、写入矩阵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" << "[";// 5、写入随机数for (int i = 0; i < 3; i++){int x = rand() % 640; // 生成 640 随机数 xint y = rand() % 480; // 生成 640 随机数 yfs << "{:" << "x" << x << "y" << y << "lbp" << "[:";uchar lbp = rand() % 256; // 生成 256以内 随机数 lbp//fs << "lbp" <<i<<lbp;for (int j = 0; j < 8; j++)fs << ((lbp >> j) & 1); // lbp 值右移 j 位(位运算)fs << "]" << "}";}fs << "]";fs.release();printf("\n文件读写完毕,请在工程目录下查看生成的文件~");getchar();return 0;}

运行结果:

1.写操作

二、Read_XML_and_YAML_File(读取XML)

#include <opencv2/opencv.hpp>#include <time.h>using namespace cv;int main(){// 1、准备文件读操作FileStorage fs2("F:/C++/2. OPENCV 3.1.0/TEST/test.yaml", FileStorage::READ);// 2、第一种方法,对FileNode操作(节点操作)// 获取整型数字int frameCount = (int)fs2["frameCount"];std::string date;// 3、第二种方法,使用FileNode运算符> >// 获取字符串fs2["calibrationDate"] >> date;// 4、获取矩阵Mat cameraMatrix2, distCoeffs2;fs2["cameraMatrix"] >> cameraMatrix2;fs2["distCoeffs"] >> distCoeffs2;// 5、控制台输出cout << "frameCount: " << frameCount << endl<< "calibration date: " << date << endl<< "camera matrix: " << cameraMatrix2 << endl<< "distortion coeffs: " << distCoeffs2 << endl;// 6、声明文件节点迭代器FileNode features = fs2["features"];FileNodeIterator it = features.begin(), it_end = features.end();std::vector<uchar> lbpval;// 7、使用FileNodeIterator遍历序列for(int idx = 0 ; it != it_end; ++it, idx++ ){cout << "feature #" << idx << ": ";cout << "x=" << (int)(*it)["x"] << ", y=" << (int)(*it)["y"] << ", lbp: (";// 我们也可以使用使用filenode > > std::vector操作符很容易的读数值阵列(*it)["lbp"] >> lbpval;// 读取数组// 根据 lbpval.size() 读取数组lbpfor( int i = 0; i < (int)lbpval.size(); i++ )cout << " " << (int)lbpval[i];cout << ")" << endl;}fs2.release();//程序结束,输出一些帮助文字printf("\n文件读取完毕,请输入任意键结束程序~");getchar();return a.exec();}

2.读操作:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。