XML文件的写入--相机标定参数
#include "opencv2/opencv.hpp" #include <time.h> using namespace cv; int main() { //初始化 FileStorage fs("cameraParameter11_22.yaml", FileStorage::WRITE); //开始文件写入 fs << "frameCount" << 5; time_t rawtime; time(&rawtime); fs << "calibrationDate" << asctime(localtime(&rawtime)); Mat cameraMatrix1 = (Mat_<double>(3, 3) << 3395.978755, -1.475113111, 687.1030152, 0, 3399.204369, 497.2602408, 0, 0, 1); Mat distCoeffs1 = (Mat_<double>(5, 1) << 0.001971949, 0.582227252, 0.002695095, 0.001234254, 0); Mat cameraMatrix2 = (Mat_<double>(3, 3) << 3409.342742, -0.081049679, 628.5013544, 0, 3414.014063, 526.8518485, 0, 0, 1); Mat distCoeffs2 = (Mat_<double>(5, 1) << 0.031079601, -0.842390482, 0.003505497, -0.000433911, 0); Mat R = (Mat_<double>(3, 3) << 0.988334695, -0.008349413, -0.152068465 , 0.009110951, 0.999949198, 0.004311741 , 0.152024739, -0.005646931, 0.988360557); Mat T = (Mat_<double>(3, 1) << -85.46437915, 0.443745317, 1.79513736); fs << "cameraMatrix1" << cameraMatrix1 << "distCoeffs1" << distCoeffs1; fs << "cameraMatrix2" << cameraMatrix2 << "distCoeffs2" << distCoeffs2; fs << "R" << R << "T" << T; fs.release(); printf("文件读写完毕,请在工程目录下查看生成的文件~"); getchar(); return 0; }
文件参数的读取
bool readFile(string filename) { FileStorage fs(filename, FileStorage::READ); if (fs.isOpened()) { fs["cameraMatrix1"] >> cameraMatrix1; fs["distCoeffs1"] >> distCoeffs1; fs["cameraMatrix2"] >> cameraMatrix2; fs["distCoeffs2"] >> distCoeffs2; fs["R"] >> R; fs["T"] >> T; fs.release(); /* cout<<"Succeed to read the Calibration result!!!"<<endl; cout<<"左相机内参矩阵:"<<endl<<cameraMatrix1<<endl; cout<<"右相机内参矩阵:"<<endl<<cameraMatrix2<<endl; cout<<"distCoeffs1:"<<endl<< distCoeffs1 <<endl; cout<<"distCoeffs2:"<<endl<< distCoeffs2 <<endl; cout<<"R:"<<endl<<R<<endl; cout<<"T:"<<endl<<T<<endl;*/ return true; } else { cerr << "Error: can not open the Calibration result file!!!!!" << endl; return false; } }