c语言怎么定义相机内参数矩阵,c++ opencv读/写yaml文件(相机参数,外参矩阵等)

参考博文:"OpenCV —数据持久化: FileStorage类的数据存取操作与示例"

https://blog.csdn.net/iracer/article/details/51339377

注意:使用FileStorage的前提是yaml文件必须遵循以下格式:

%YAML:1.0

fx: 100.

fy: 101.

否则无法使用,会提示找不到输入文件

使用FileStorage读取yaml存储的外参矩阵:

b4a614901a7a142d26bfb3e37f57ab4c.png

写入操作:

cv::FileStorage fs("test.yml", FileStorage::WRITE);

int imageWidth= 5;

int imageHeight= 10;

fs << "imageWidth" << imageWidth;

fs << "imageHeight" << imageHeight;

cv::Mat m1= Mat::eye(3,3, CV_8U);

cv::Mat m2= Mat::ones(3,3, CV_8U);

cv::Mat resultMat= (m1+1).mul(m1+2);

fs << "resultMat" << resultMat;

cv::Mat cameraMatrix = (Mat_(3,3) << 1000, 0, 320, 0, 1000, 240, 0, 0, 1);

cv::Mat distCoeffs = (Mat_(5,1) << 0.1, 0.01, -0.001, 0, 0);

fs << "cameraMatrix" << cameraMatrix << "distCoeffs" << distCoeffs;

time_t rawtime; time(&rawtime); //#include fs << "calibrationDate" << asctime(localtime(&rawtime));

fs.release(); //close the file opened

fa4a73acb2055274006b3bfdf0333f96.png

打开文件的两种方式:

1.

cv::FileStorage fs;

fs.open("test.yml",FileStorage::WRITE);

......

fs.release()

2.

cv::FileStorage fs("test.yml", FileStorage::WRITE);

读取操作

// read data using operator []

cv::FileStorage fs("test.yml", FileStorage::READ);

int width;

int height;

fs["imageWidth"]>>width;

fs["imageHeight"]>>height;

cout<>resultMatRead;

cout<>cameraMatrixRead;

fs["distCoeffs"]>>distCoeffsRead;

cout<>timeRead;

cout<

dd5b3555a7aeba0977da2c17ca0a3d96.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值