在使用opencv进行图像处理的过程中,经常会涉及到将文件中的数据读入到cv::Mat中,或者将cv::Mat中的数据写入到txt,yml文件中。
通常是使用FileStorage类来处理,使用流程有3步,如下:
(1)实例化一个FileStorage类对象
写入(FileStorage::WRITE,覆盖写)
追加(FileStorage::APPEND,追加写)
读取(FileStorage::WRITE)
cv::FileStorage fw("calibrateResult.txt", cv::FileStorage::WRITE);
cv::FileStorage fr("calibrateResult.txt", cv::FileStorage::READ);
(2)使用流操作符<<进行文件写入,>>进行文件读取
//文本中的数据名为"cameraMatrix",再将矩阵cameraMatrix写入
fw << "cameraMatrix" << this->cameraMatrix;
(3)使用FileStorage::release()函数析构掉类对象,并关闭文件
下面具体看一下代码(2个独立的程序):
【1】写入参数/矩阵(部分相机标定代码)
这部分把相机标定省略了,因此内外参数没有赋值,你们运行不了,但下一个代码可以。
#include
#include
#include
#include
using namespace std;
int main()
{
//相机内参矩阵 3x3
cv::Mat cameraMatrix;
//畸变参数 1x5
cv::Mat distCoeffs;
//保存旋转向量的容器 Mat为3行1列
vector<cv::Mat> rVecs;
//保存平移向量的容器 Mat为3行1列
vector<cv::Mat> tVecs;
//旋转矩阵
cv::Mat rMatrix;
//定义字符串
string rotateVec = "rotVec";
string rotateMatrix = "rotMat";
string translate = "traVec";
//写入文本的话使用WRITE 这里的文本可以是txt yml xml
cv::FileStorage fw("calibrateResult.txt", cv::FileStorage::WRITE);
//文本中添加一个"cameraMatrix"的字符串,并将cameraMatrix参数保存到"cameraMatrix"中
fw << "cameraMatrix" << cameraMatrix;
fw << "distCoeffs" << distCoeffs;
//我这里有8张图片
for (int i = 0; i < 8; i++)
{
//将旋转向量转换为相对应的旋转矩阵
cv::Rodrigues(rVecs[i], rMatrix);
//保存旋转向量
fw << rotateVec + to_string(i + 1) << rVecs[i];
//保存旋转矩阵
fw << rotateMatrix + to_string(i + 1) << rMatrix;
//保存平移向量
fw << translate + to_string(i + 1) << tVecs[i];
}
//关闭文件
fw.release();
return 0;
}
【2】读取参数/矩阵(部分相机标定代码)
需要重新建一个工程,并将第1步生成的calibrateResult.txt文件放到与main函数同一目录下,这部分代码你们可以运行
#include
#include
#include
#include
using namespace std;
int main()
{
//内参
cv::Mat cameraMatrix;
//畸变
cv::Mat distCoeffs;
//保存旋转矩阵的容器 Mat为3行3列
vector<cv::Mat> rotMatrix;
//保存平移向量的容器 Mat为3行1列
vector<cv::Mat> traMatrix;
//从文本读取参数 这里的文本可以是txt yml xml
cv::FileStorage fr("calibrateResult.txt", cv::FileStorage::READ);
if (!fr.isOpened())
cout << "calibrateResult.txt文件打开失败!" << endl;
//将文本中名字为"cameraMatrix"的数据读给cameraMatrix,读入的时候要加[]
fr["cameraMatrix"] >> cameraMatrix;
fr["distCoeffs"] >> distCoeffs;
//定义字符串
string rName("rotMat");
string tName("traVec");
//读入8张图片的旋转矩阵和平移向量
for (int i = 0; i < 8; i++)
{
//定义两个临时变量
cv::Mat rMat;
cv::Mat tMat;
string rotName = rName + to_string(i + 1);
string traName = tName + to_string(i + 1);
//将文本中名为"rotMat"的数据读取并赋给rMat
fr[rotName] >> rMat;
fr[traName] >> tMat;
//将保存的rMat与tMat通过尾插法存储到名为rotMatrix和traMatrix的容器中
rotMatrix.push_back(rMat);
traMatrix.push_back(tMat);
}
//关闭文件
fr.release();
return 0;
}
下面的数据是第1步过程生成的文本文件结果,可以用于第2步的使用。
%YAML:1.0
---
cameraMatrix: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ 5.5656314582396835e+03, 0., 1.2229231977092950e+03, 0.,
5.5635200465907456e+03, 9.6373914589479955e+02, 0., 0., 1. ]
distCoeffs: !!opencv-matrix
rows: 1
cols: 5
dt: d
data: [ -1.6449756450459926e-01, 8.6541785695306417e-01,
3.6332338164954018e-04, -4.7251458379197125e-04,
-1.1365518697291174e+01 ]
rotVec1: !!opencv-matrix
rows: 3
cols: 1
dt: d
data: [ 3.7941354063604671e-01, 5.8810276029636809e-02,
6.8544687767575330e-01 ]
rotMat1: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ 7.7527685213062658e-01, -6.0648151149781304e-01,
1.7642556153164068e-01, 6.2767053537230333e-01,
7.0856871266117960e-01, -3.2242840827558122e-01,
7.0537235385808070e-02, 3.6070840806530519e-01,
9.3000749608544575e-01 ]
traVec1: !!opencv-matrix
rows: 3
cols: 1
dt: d
data: [ -2.2242588237238422e+01, -3.0435488494899150e+01,
2.6680330907120890e+02 ]
rotVec2: !!opencv-matrix
rows: 3
cols: 1
dt: d
data: [ 1.4961186389998318e-01, 4.7465707169770260e-01,
2.2423773693015874e+00 ]
rotMat2: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ -6.5693236013450840e-01, -7.0757968802921523e-01,
2.6032836821326816e-01, 7.5237447046378814e-01,
-5.9293428104561585e-01, 2.8698709823832291e-01,
-4.8708627597569887e-02, 3.8439552995501525e-01,
9.2188260974386727e-01 ]
traVec2: !!opencv-matrix
rows: 3
cols: 1
dt: d
data: [ 1.4375903305714012e+01, -1.6858126279024283e+01,
2.6771083226749818e+02 ]
rotVec3: !!opencv-matrix
rows: 3
cols: 1
dt: d
data: [ 3.6760145441854275e-01, -6.0762638182377604e-01,
-2.2087152288200689e+00 ]
rotMat3: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ -6.3890998788240005e-01, 6.2727387732740425e-01,
-4.4533303291666554e-01, -7.6679356773459895e-01,
-5.6580380618341120e-01, 3.0313969946084229e-01,
-6.1819510390785093e-02, 5.3515748684944076e-01,
8.4248727729505446e-01 ]
traVec3: !!opencv-matrix
rows: 3
cols: 1
dt: d
data: [ 3.6931719283613440e+01, 2.7500637137506775e+01,
2.6821080189580590e+02 ]
rotVec4: !!opencv-matrix
rows: 3
cols: 1
dt: d
data: [ 5.4698007983435146e-02, 1.6403368863855264e-01,
2.1702018511636592e+00 ]
rotMat4: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ -5.6882606064161700e-01, -8.1620386024726777e-01,
1.0123325171305164e-01, 8.2214726106512137e-01,
-5.6090516854150951e-01, 9.7258794083310757e-02,
-2.2600749059678092e-02, 1.3855197732576527e-01,
9.9009724558805901e-01 ]
traVec4: !!opencv-matrix
rows: 3
cols: 1
dt: d
data: [ 5.1781917984460499e+01, -9.5653665059897577e+00,
2.8227136969606414e+02 ]
rotVec5: !!opencv-matrix
rows: 3
cols: 1
dt: d
data: [ 2.0755534034653123e-01, -3.0226288357576353e-01,
-2.4420199197937809e+00 ]
rotMat5: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ -7.6986169844529229e-01, 5.9746741208472320e-01,
-2.2437837855665443e-01, -6.3414379734789494e-01,
-7.5574810108026069e-01, 1.6342108798674632e-01,
-7.1934758998148329e-02, 2.6809979337993439e-01,
9.6070177018547986e-01 ]
traVec5: !!opencv-matrix
rows: 3
cols: 1
dt: d
data: [ 2.8632431730717109e+01, 3.8421000693274252e+01,
2.8119574255158761e+02 ]
rotVec6: !!opencv-matrix
rows: 3
cols: 1
dt: d
data: [ -4.8627107746147763e-01, -2.8177976952282141e-01,
7.8363942404759201e-01 ]
rotMat6: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ 6.7930675118930872e-01, -6.0434259958218206e-01,
-4.1630801111542037e-01, 7.3106911467791069e-01,
6.0667708086959593e-01, 3.1221926447866982e-01,
6.3877126991454397e-02, -5.1644258333119297e-01,
8.5393604606523166e-01 ]
traVec6: !!opencv-matrix
rows: 3
cols: 1
dt: d
data: [ -1.1269422073497639e+01, -3.6890267537566615e+01,
2.8084310616221831e+02 ]
rotVec7: !!opencv-matrix
rows: 3
cols: 1
dt: d
data: [ -3.6654883437328739e-01, -1.1909878327701895e-01,
1.4643363993495367e-01 ]
rotMat7: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ 9.8243724755250850e-01, -1.2079985454024214e-01,
-1.4221269199515041e-01, 1.6384046806275646e-01,
9.2319683140578024e-01, 3.4765487413137813e-01,
8.9293648410332396e-02, -3.6484929166079499e-01,
9.2677485870527809e-01 ]
traVec7: !!opencv-matrix
rows: 3
cols: 1
dt: d
data: [ 4.6381644458905322e+00, -1.3983989750510041e+01,
2.8401280085250306e+02 ]
rotVec8: !!opencv-matrix
rows: 3
cols: 1
dt: d
data: [ 4.3201085429499669e-01, -1.4205658496125403e-01,
-2.5761572807677602e-01 ]
rotMat8: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ 9.5770321158010518e-01, 2.1605290307018885e-01,
-1.9006762376611064e-01, -2.7603846540120142e-01,
8.7635404039009557e-01, -3.9472314539548403e-01,
8.1285448563085047e-02, 4.3049359921710595e-01,
8.9892599077176527e-01 ]
traVec8: !!opencv-matrix
rows: 3
cols: 1
dt: d
data: [ -2.2614457056945270e+01, 4.0665059749313768e+00,
2.6709282364848053e+02 ]