4 |
jakw |
1 |
#include "SMCalibrationParams.h"
|
|
|
2 |
|
|
|
3 |
#include <QFileInfo>
|
|
|
4 |
#include <QString>
|
|
|
5 |
#include <QDateTime>
|
|
|
6 |
|
|
|
7 |
#include <iostream>
|
|
|
8 |
#include <fstream>
|
|
|
9 |
#include <iomanip>
|
|
|
10 |
#include <opencv2/calib3d/calib3d.hpp>
|
|
|
11 |
|
|
|
12 |
|
|
|
13 |
bool SMCalibrationParams::load(const QString& filename){
|
|
|
14 |
QFileInfo info(filename);
|
|
|
15 |
// QString type = info.suffix();
|
|
|
16 |
|
|
|
17 |
if(info.exists() && info.suffix()=="yml")
|
|
|
18 |
return loadYML(filename);
|
|
|
19 |
else
|
|
|
20 |
{
|
|
|
21 |
std::cerr << "SMCalibrationParams error: no such .yml file: " << filename.toStdString() << std::endl;
|
|
|
22 |
return false;
|
|
|
23 |
}
|
|
|
24 |
}
|
|
|
25 |
|
|
|
26 |
bool SMCalibrationParams::save(const QString& filename){
|
|
|
27 |
QFileInfo info(filename);
|
|
|
28 |
QString type = info.suffix();
|
|
|
29 |
|
|
|
30 |
if (type=="slcalib") {return saveSLCALIB(filename);}
|
|
|
31 |
if (type=="yml") {return saveYML(filename);}
|
|
|
32 |
if (type=="m") {return saveMatlab(filename);}
|
|
|
33 |
|
|
|
34 |
return false;
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
bool SMCalibrationParams::loadYML(const QString& filename){
|
|
|
38 |
cv::FileStorage fs(filename.toStdString(), cv::FileStorage::READ); //
|
|
|
39 |
if (!fs.isOpened())
|
|
|
40 |
{
|
|
|
41 |
std::cerr << "SMCalibrationParams error: could not open file " << filename.toStdString() << std::endl;
|
|
|
42 |
return false;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
cv::Mat temp;
|
|
|
46 |
fs["Kc"] >> temp; Kc = temp;
|
|
|
47 |
fs["kc"] >> temp; kc = temp;
|
|
|
48 |
fs["Kp"] >> temp; Kp = temp;
|
|
|
49 |
fs["kp"] >> temp; kp = temp;
|
|
|
50 |
fs["Rp"] >> temp; Rp = temp;
|
|
|
51 |
fs["Tp"] >> temp; Tp = temp;
|
|
|
52 |
|
|
|
53 |
fs["cam_error"] >> cam_error;
|
|
|
54 |
fs["proj_error"] >> proj_error;
|
|
|
55 |
fs["stereo_error"] >> stereo_error;
|
|
|
56 |
|
|
|
57 |
fs.release();
|
|
|
58 |
|
|
|
59 |
return true;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
bool SMCalibrationParams::saveSLCALIB(const QString& filename){
|
|
|
63 |
|
|
|
64 |
|
|
|
65 |
FILE * fp = fopen(qPrintable(filename), "w");
|
|
|
66 |
if (!fp)
|
|
|
67 |
return false;
|
|
|
68 |
|
|
|
69 |
fprintf(fp, "#V1.0 SLStudio calibration\n");
|
|
|
70 |
fprintf(fp, "#Calibration time: %s\n\n", calibrationDateTime.c_str());
|
|
|
71 |
fprintf(fp, "Kc\n%f %f %f\n%f %f %f\n%f %f %f\n\n", Kc(0,0), Kc(0,1), Kc(0,2), Kc(1,0), Kc(1,1), Kc(1,2), Kc(2,0), Kc(2,1), Kc(2,2));
|
|
|
72 |
fprintf(fp, "kc\n%f %f %f %f %f\n\n", kc(0), kc(1), kc(2), kc(3), kc(4));
|
|
|
73 |
fprintf(fp, "Kp\n%f %f %f\n%f %f %f\n%f %f %f\n\n", Kp(0,0), Kp(0,1), Kp(0,2), Kp(1,0), Kp(1,1), Kp(1,2), Kp(2,0), Kp(2,1), Kp(2,2));
|
|
|
74 |
fprintf(fp, "kp\n%f %f %f %f %f\n\n", kp(0), kp(1), kp(2), kp(3), kp(4));
|
|
|
75 |
fprintf(fp, "Rp\n%f %f %f\n%f %f %f\n%f %f %f\n\n", Rp(0,0), Rp(0,1), Rp(0,2), Rp(1,0), Rp(1,1), Rp(1,2), Rp(2,0), Rp(2,1), Rp(2,2));
|
|
|
76 |
fprintf(fp, "Tp\n%f %f %f\n\n", Tp(0), Tp(1), Tp(2));
|
|
|
77 |
|
|
|
78 |
fprintf(fp, "cam_error: %f\n\n", cam_error);
|
|
|
79 |
fprintf(fp, "proj_error: %f\n\n", proj_error);
|
|
|
80 |
fprintf(fp, "stereo_error: %f\n\n", stereo_error);
|
|
|
81 |
|
|
|
82 |
fclose(fp);
|
|
|
83 |
|
|
|
84 |
return true;
|
|
|
85 |
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
bool SMCalibrationParams::saveYML(const QString& filename){
|
|
|
89 |
cv::FileStorage fs(filename.toStdString(), cv::FileStorage::WRITE);
|
|
|
90 |
if (!fs.isOpened())
|
|
|
91 |
return false;
|
|
|
92 |
|
|
|
93 |
fs << "Kc" << cv::Mat(Kc) << "kc" << cv::Mat(kc)
|
|
|
94 |
<< "Kp" << cv::Mat(Kp) << "kp" << cv::Mat(kp)
|
|
|
95 |
<< "Rp" << cv::Mat(Rp) << "Tp" << cv::Mat(Tp)
|
|
|
96 |
<< "cam_error" << cam_error
|
|
|
97 |
<< "proj_error" << proj_error
|
|
|
98 |
<< "stereo_error" << stereo_error;
|
|
|
99 |
fs.release();
|
|
|
100 |
|
|
|
101 |
return true;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
bool SMCalibrationParams::saveMatlab(const QString& filename){
|
|
|
105 |
|
|
|
106 |
std::ofstream file(qPrintable(filename));
|
|
|
107 |
if (!file)
|
|
|
108 |
return false;
|
|
|
109 |
|
|
|
110 |
file << "%%SLStudio calibration" << std::endl;
|
|
|
111 |
file << "Kc = " << Kc << ";" << std::endl;
|
|
|
112 |
file << "kc = " << kc << ";" << std::endl;
|
|
|
113 |
file << "Kp = " << Kp << ";" << std::endl;
|
|
|
114 |
file << "kp = " << kp << ";" << std::endl;
|
|
|
115 |
file << "Rp = " << Rp << ";" << std::endl;
|
|
|
116 |
file << "Tp = " << Tp << ";" << std::endl;
|
|
|
117 |
|
|
|
118 |
file.close();
|
|
|
119 |
|
|
|
120 |
return true;
|
|
|
121 |
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
void SMCalibrationParams::print(std::ostream &stream){
|
|
|
125 |
|
|
|
126 |
stream << std::setw(5) << std::setprecision(4)
|
|
|
127 |
<< "========================================\n"
|
|
|
128 |
<< "Camera Calibration: \n"
|
|
|
129 |
<< "- cam_error:\n" << cam_error << "\n"
|
|
|
130 |
<< "- Kc:\n" << Kc << "\n"
|
|
|
131 |
<< "- kc:\n" << kc << "\n"
|
|
|
132 |
<< "Projector Calibration: " << "\n"
|
|
|
133 |
<< "- proj_error: \n" << proj_error << "\n"
|
|
|
134 |
<< "- Kp: \n" << Kp << "\n"
|
|
|
135 |
<< "- kp: \n" << kp << "\n"
|
|
|
136 |
<< "Stereo Calibration: \n"
|
|
|
137 |
<< "- stereo_error:\n" << stereo_error << "\n"
|
|
|
138 |
<< "- Rp:\n" << Rp << "\n"
|
|
|
139 |
<< "- Tp:\n" << Tp << std::endl;
|
|
|
140 |
}
|