c++/python opencv读/写yaml文件(相机参数,外参矩阵等)

一. 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)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值