一. C++ 读写yaml文件
1. 写入yaml
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <vector>
#include <cmath>
using namespace std;
using namespace cv;
#define PI 3.14
int main()
{
cv::FileStorage fs("calibration_camera.yaml", FileStorage::WRITE);
int imageWidth= 1920;
int imageHeight= 1200;
fs << "imageWidth" << imageWidth;
fs << "imageHeight" << imageHeight;
cv::Mat cameraMatrix = (Mat_<double>(3,3) << 775.9, 0, 960, 0, 776.5, 540, 0, 0, 1);
cv::Mat distCoeffs = (Mat_<float>(5,1) << -0.159, -0.279, 0.012, 0.018, 1.641);
cv::Mat rvec = (Mat_<float>(3,3) << 1, 0, 0, 0, 1, 0, 0, 0, 1);
cv::Mat tvec = (Mat_<float>(1,3) << 5.2, -0.15, 2.13);
fs << "cameraMatrix" << cameraMatrix << "distCoeffs" << distCoeffs;
fs << "rvec" << rvec << "tvec" << tvec;
fs << "thetaX" << 0;
fs << "thetaY" << 0;
fs << "thetaZ" << 0;
fs.release(); //close the file opened
return 0;
}
2. 读yaml
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <vector>
#include <cmath>
using namespace std;
using namespace cv;
#define PI 3.14
int main()
{
// read data using operacout<<"rvec = "<<rvec<<endl;
cv::FileStorage fs("calibration_camera.yaml", FileStorage::READ);
int width;
int height;
fs["imageWidth"]>>width;
fs["imageHeight"]>>height;
cout<<"width readed = "<<width<<endl;
cout<<"height readed = "<<height<<endl<<endl;
cv::Mat cameraMatrixRead,distCoeffsRead, rvec, tvec;
fs["cameraMatrix"]>>cameraMatrixRead;
fs["distCoeffs"]>>distCoeffsRead;
fs["rvec"]>>rvec;
fs["tvec"]>>tvec;
// cout<<"cameraMatrix readed = "<<cameraMatrixRead<<endl;
// cout<<"distCoeffs readed = "<<distCoeffsRead<<endl;
// cout<<"rvec = "<<rvec<<endl;
// cout<<"tvec = "<<tvec<<endl<<endl;
fs.release();
return 0;
}
二. python 读写yaml文件
1. 写入yaml
#!/usr/bin/env python
# coding: utf-8
import cPickle
import cv2
data = cPickle.load(open('./ExteralCalibration_T5G.p', "rb")) # 加载镜头内参p文件,含有标定好的内参矩阵和畸变参数
mtx = data['mtx'] # 镜头内参矩阵
dist = data['dist'] # 镜头畸变参数
rvec=data['rvec']
tvec=data['tvec']
thetaX=data['thetaX']
thetaY=data['thetaY']
thetaZ=data['thetaZ']
fs = cv2.FileStorage('calibration_camera.yaml', cv2.FileStorage_WRITE)
fs.write('imageWidth', 1920)
fs.write('imageHeight', 1200)
fs.write('cameraMatrix', mtx)
fs.write('distCoeffs', dist)
fs.write('rvec', rvec)
fs.write('tvec', tvec)
fs.write('thetaX', thetaX)
fs.write('thetaY', thetaY)
fs.write('thetaZ', thetaZ)
# 关闭文件
fs.release()
2. 读入yaml
#!/usr/bin/env python
# coding: utf-8
import cPickle
import cv2
fs2 = cv2.FileStorage('calibration_camera.yaml', cv2.FileStorage_READ)
imageWidth = fs2.getNode('imageWidth').real()
imageHeight = fs2.getNode('imageHeight').real()
cameraMatrix = fs2.getNode('cameraMatrix').mat()
distCoeffs = fs2.getNode('distCoeffs').mat()
rvec = fs2.getNode('rvec').mat()
tvec = fs2.getNode('tvec').mat()
thetaX = fs2.getNode('thetaX').real()
thetaY = fs2.getNode('thetaY').real()
thetaZ= fs2.getNode('thetaZ').real()
# 关闭文件
fs2.release()
print("imageWidth= ", imageWidth)
print("imageHeight= ", imageHeight)
print("cameraMatrix= ", cameraMatrix)
print("distCoeffs= ", distCoeffs)
print("rvec=", rvec)
print("tvec=", tvec)