https://blog.csdn.net/A_L_A_N/article/details/83272772
FileStorage类
FileStorage类将各种OpenCV数据结构的数据存储为XML或YML格式。
构造函数:cv::FileStorage(const string& source, int flags, const string& encoding=string());
参数说明:
source:存储或读取数据的文件名(字符串),其扩展名(.xml 或 .yml或者.yaml)决定文件格式。
flags:操作方式,包括:FileStorage::READ、FileStorage::WRITE、FileStorage::APPEND。
encoding:编码方式,用默认值就好。
写xml/ymal文件:
#include<iostream> #include<opencv2/opencv.hpp> using namespace std; using namespace cv; void main() { //标定结果保存 FileStorage fs("caliResult.xml", FileStorage::WRITE); int cameraId = 0; Mat intrMatrix = (Mat_<double>(3, 3) << 7.7881772950073355e+002, 0, 3.1562441595543476e+002, 0, 7.8624564811643825e+002, 2.5630331974129393e+002, 0, 0, 1); Mat distCoeffs = (Mat_<double>(1, 5) << -7.2660835182078581e-002, 2.0765291395491934e+000, 5.9477659924542790e-004, -8.2981148319346263e-004, -7.0307616798578119e+000); fs << "cameraId" << cameraId; fs << "intrinsic_parameters" << intrMatrix; fs << "distortion_parametes" << distCoeffs; fs.release(); }
保存的.xml文件:
<?xml version="1.0"?> <opencv_storage> <cameraId>0</cameraId> <intrinsic_parameters type_id="opencv-matrix"> <rows>3</rows> <cols>3</cols> <dt>d</dt> <data> 7.7881772950073355e+02 0. 3.1562441595543476e+02 0. 7.8624564811643825e+02 2.5630331974129393e+02 0. 0. 1.</data></intrinsic_parameters> <distortion_parametes type_id="opencv-matrix"> <rows>1</rows> <cols>5</cols> <dt>d</dt> <data> -7.2660835182078581e-02 2.0765291395491934e+00 5.9477659924542790e-04 -8.2981148319346263e-04 -7.0307616798578119e+00</data></distortion_parametes> </opencv_storage>
读xml/ymal文件:
#include<iostream> #include<opencv2/opencv.hpp> using namespace std; using namespace cv; void main() { //FileStorage fs("caliResult.xml",FileStorage::READ); FileStorage fs; fs.open("caliResult.xml",FileStorage::READ); if(!fs.isOpened()) { return; } int cameraId; Mat intrMatrix(3,3,CV_64F); Mat distCoeffs(5,1,CV_64F); fs["cameraId"]>>cameraId; fs["intrinsic_parameters"]>>intrMatrix; fs["distortion_parametes"]>>distCoeffs; fs.release(); }