由于近期研究相机标定以及三维重建工作,因此需要将标定程序以及重建程序分开,这样只需运行标定程序一次,之后重建时将各个矩阵从txt中读出即可,因此记录一下提取opencv保存的Mat数据的过程(可以用于矩阵提取的其他场合)。
原理:txt中通过ofstream保存的符号无非“[”、“, ”、“;”、“]”,因此将这些符号拆分出来就可以了。
保存的矩阵如图所示:
话不多说,直接贴程序。
//拆分字符串的函数
std::vector<std::string> split(std::string str, std::string pattern)
{
std::vector<std::string> ret;
if (pattern.empty()) return ret;
size_t start = 0, index = str.find_first_of(pattern, 0);
while (index != str.npos)
{
if (start != index)
ret.push_back(str.substr(start, index - start));
start = index + 1;
index = str.find_first_of(pattern, start);
}
if (!str.substr(start).empty())
ret.push_back(str.substr(start));
return ret;
}
//从txt中获取矩阵的函数
void getMatrix(std::ifstream& ifs1,cv::Mat& cameraMatrix)
{
std::vector<double> matrix_cam;
std::string test_str;
while (std::getline(ifs1, test_str))//逐行读取数据
{
//std::cout << test << std::endl;
//string->char *
std::string split1 = "[";
//分割字符串
std::vector<std::string> str1 = split(test_str, split1);//第一次拆分
std::string split2 = ", ";
std::vector<std::string> str2;
for (int i = 0; i < str1.size(); i++)
{
std::vector<std::string> str_temp = split(str1[i], split2);//第二次拆分
for (int j = 0; j < str_temp.size(); j++)
{
str2.push_back(str_temp[j]);
}
}
//第三次拆分
std::string split3 = ";";
std::vector<std::string> str3;
for (int i = 0; i < str2.size(); i++)
{
std::vector<std::string> str_temp2 = split(str2[i], split3);
for (int j = 0; j < str_temp2.size(); j++)
{
str3.push_back(str_temp2[j]);
}
}
//第四次拆分
std::string split4 = "]";
std::vector<std::string> str4;
for (int i = 0; i < str3.size(); i++)
{
std::vector<std::string> str_temp3 = split(str3[i], split4);
for (int j = 0; j < str_temp3.size(); j++)
{
str4.push_back(str_temp3[j]);
}
}
for (int i = 0; i < str4.size(); i++)
{
double temp = std::stod(str4[i]);
matrix_cam.push_back(temp);
}
}
int k = 0;
if (matrix_cam.size() != cameraMatrix.rows * cameraMatrix.cols)
std::cout << "拆分的矩阵数据不对!" << std::endl;
for (int i = 0; i < cameraMatrix.rows; i++)
{
for (int j = 0; j < cameraMatrix.cols; j++)
{
double temp = matrix_cam[k];
cameraMatrix.at<double>(i, j) = temp;
k++;
}
}
}
main函数中的程序:
std::ifstream ifs1;
ifs1.open("../light_plan_calibration/camera_parameters.txt", std::ios::in);
if (!ifs1)
{
std::cout << "camera_parameters.txt 无法打开!" << std::endl;
exit(1);
}
//获取相机内参的矩阵
cv::Mat cameraMatrix = cv::Mat(3, 3, CV_64FC1, cv::Scalar::all(0));
getMatrix(ifs1, cameraMatrix);
//std::cout << cameraMatrix << std::endl;
//获取相机畸变矩阵
std::ifstream ifs2;
ifs2.open("../light_plan_calibration/discoeffs_parameters.txt", std::ios::in);
if (!ifs2)
{
std::cout << "discoeffs_parameters.txt 无法打开!" << std::endl;
exit(1);
}
cv::Mat distCoeffs = cv::Mat(1, 5, CV_64FC1, cv::Scalar::all(0));
getMatrix(ifs2, distCoeffs);
//std::cout << distCoeffs << std::endl;
//获取旋转平移矩阵
std::ifstream ifs3;
ifs3.open("../light_plan_calibration/rvecs_parameters.txt", std::ios::in);
if (!ifs3)
{
std::cout << "rvecs_parameters.txt 无法打开!" << std::endl;
exit(1);
}
cv::Mat tvecsMat = cv::Mat(3, 1, CV_64FC1, cv::Scalar::all(0));
getMatrix(ifs3, tvecsMat);
//std::cout << tvecsMat << std::endl;
std::ifstream ifs4;
ifs4.open("../light_plan_calibration/tvecs_parameters.txt", std::ios::in);
if (!ifs4)
{
std::cout << "tvecs_parameters.txt 无法打开!" << std::endl;
exit(1);
}
cv::Mat rvecsMat = cv::Mat(3, 1, CV_64FC1, cv::Scalar::all(0));
getMatrix(ifs4, rvecsMat);
//std::cout << rvecsMat << std::endl;
//获取光平面系数
std::vector<double> light_plane;
std::ifstream ifs5;
ifs5.open("../light_plan_calibration/plane_parameters.txt", std::ios::in);
if (!ifs5)
{
std::cout << "plane_parameters.txt 无法打开!" << std::endl;
exit(1);
}
cv::Mat planeMat = cv::Mat(1, 4, CV_64FC1, cv::Scalar::all(0));
getMatrix(ifs5, planeMat);
for (int i = 0; i < planeMat.rows; i++)
{
for (int j = 0; j < planeMat.cols; j++)
{
light_plane.push_back(planeMat.at<double>(i, j));
}
}
//获取传送带位移矩阵
std::ifstream ifs6;
ifs6.open("../light_plan_calibration/step_parameters.txt", std::ios::in);
if (!ifs6)
{
std::cout << "step_parameters.txt 无法打开!" << std::endl;
exit(1);
}
cv::Mat step = cv::Mat(3, 1, CV_64FC1, cv::Scalar::all(0));
getMatrix(ifs6, step);
std::cout << step << std::endl;
如果有错误,还请指正。