27 |
jakw |
1 |
#include "SMCalibrationWorker.h"
|
|
|
2 |
#include "SMCalibrationParameters.h"
|
22 |
jakw |
3 |
|
31 |
jakw |
4 |
#include "cvtools.h"
|
|
|
5 |
|
22 |
jakw |
6 |
#include <QSettings>
|
|
|
7 |
|
27 |
jakw |
8 |
void SMCalibrationWorker::performCalibration(std::vector<SMCalibrationSet> calibrationData){
|
22 |
jakw |
9 |
|
|
|
10 |
// Number of saddle points on calibration pattern
|
|
|
11 |
cv::Size patternSize(10, 9);
|
|
|
12 |
|
25 |
jakw |
13 |
int nSets = calibrationData.size();
|
22 |
jakw |
14 |
|
31 |
jakw |
15 |
std::vector< std::vector<cv::Point2f> > qc0, qc1;
|
|
|
16 |
std::vector<float> angles;
|
22 |
jakw |
17 |
|
|
|
18 |
// Loop through calibration sets
|
|
|
19 |
for(int i=0; i<nSets; i++){
|
|
|
20 |
|
27 |
jakw |
21 |
SMCalibrationSet SMCalibrationSetI = calibrationData[i];
|
25 |
jakw |
22 |
|
27 |
jakw |
23 |
if(!SMCalibrationSetI.checked)
|
22 |
jakw |
24 |
continue;
|
25 |
jakw |
25 |
|
|
|
26 |
// Camera 0
|
|
|
27 |
std::vector<cv::Point2f> qci0;
|
|
|
28 |
// Extract checker corners
|
27 |
jakw |
29 |
bool success0 = cv::findChessboardCorners(SMCalibrationSetI.frame0, patternSize, qci0, cv::CALIB_CB_ADAPTIVE_THRESH);
|
25 |
jakw |
30 |
if(success0){
|
26 |
jakw |
31 |
cv::Mat gray;
|
27 |
jakw |
32 |
cv::cvtColor(SMCalibrationSetI.frame0, gray, CV_RGB2GRAY);
|
|
|
33 |
cv::cornerSubPix(gray, qci0, cv::Size(5, 5), cv::Size(-1, -1),cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 20, 0.001));
|
25 |
jakw |
34 |
// Draw colored chessboard
|
27 |
jakw |
35 |
SMCalibrationSetI.frame0Result = SMCalibrationSetI.frame0.clone();
|
|
|
36 |
cv::drawChessboardCorners(SMCalibrationSetI.frame0Result, patternSize, qci0, success0);
|
22 |
jakw |
37 |
}
|
|
|
38 |
|
29 |
jakw |
39 |
emit newFrameResult(i, 0, success0, SMCalibrationSetI.frame0Result);
|
|
|
40 |
|
25 |
jakw |
41 |
// Camera 1
|
|
|
42 |
std::vector<cv::Point2f> qci1;
|
|
|
43 |
// Extract checker corners
|
27 |
jakw |
44 |
bool success1 = cv::findChessboardCorners(SMCalibrationSetI.frame1, patternSize, qci1, cv::CALIB_CB_ADAPTIVE_THRESH);
|
25 |
jakw |
45 |
if(success1){
|
26 |
jakw |
46 |
cv::Mat gray;
|
27 |
jakw |
47 |
cv::cvtColor(SMCalibrationSetI.frame1, gray, CV_RGB2GRAY);
|
|
|
48 |
cv::cornerSubPix(gray, qci1, cv::Size(5, 5), cv::Size(-1, -1),cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 20, 0.001));
|
25 |
jakw |
49 |
// Draw colored chessboard
|
27 |
jakw |
50 |
SMCalibrationSetI.frame1Result = SMCalibrationSetI.frame1.clone();
|
|
|
51 |
cv::drawChessboardCorners(SMCalibrationSetI.frame1Result, patternSize, qci1, success1);
|
22 |
jakw |
52 |
}
|
|
|
53 |
|
29 |
jakw |
54 |
emit newFrameResult(i, 1, success1, SMCalibrationSetI.frame1Result);
|
|
|
55 |
|
27 |
jakw |
56 |
SMCalibrationSetI.success = success0 && success1;
|
25 |
jakw |
57 |
|
22 |
jakw |
58 |
// Add to whole set
|
27 |
jakw |
59 |
if(SMCalibrationSetI.success){
|
31 |
jakw |
60 |
qc0.push_back(qci0);
|
|
|
61 |
qc1.push_back(qci1);
|
|
|
62 |
angles.push_back(SMCalibrationSetI.rotationAngle);
|
22 |
jakw |
63 |
}
|
|
|
64 |
|
27 |
jakw |
65 |
// Show progress
|
|
|
66 |
emit newSetProcessed(i);
|
22 |
jakw |
67 |
}
|
|
|
68 |
|
31 |
jakw |
69 |
int nValidSets = qc0.size();
|
27 |
jakw |
70 |
if(nValidSets < 2){
|
22 |
jakw |
71 |
std::cerr << "Not enough valid calibration sequences!" << std::endl;
|
29 |
jakw |
72 |
emit done();
|
22 |
jakw |
73 |
return;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
// Generate world object coordinates [mm]
|
|
|
77 |
std::vector<cv::Point3f> Qi;
|
|
|
78 |
for (int h=0; h<patternSize.height; h++)
|
|
|
79 |
for (int w=0; w<patternSize.width; w++)
|
|
|
80 |
Qi.push_back(cv::Point3f(5 * w, 5* h, 0.0)); // 5mm chess field size
|
|
|
81 |
std::vector< std::vector<cv::Point3f> > Q;
|
31 |
jakw |
82 |
for(int i=0; i<qc0.size(); i++)
|
22 |
jakw |
83 |
Q.push_back(Qi);
|
|
|
84 |
|
|
|
85 |
// calibrate the cameras
|
31 |
jakw |
86 |
SMCalibrationParameters cal;
|
|
|
87 |
cal.frameWidth = calibrationData[0].frame0.cols;
|
|
|
88 |
cal.frameHeight = calibrationData[0].frame0.rows;
|
|
|
89 |
cv::Size frameSize(cal.frameWidth, cal.frameHeight);
|
22 |
jakw |
90 |
|
31 |
jakw |
91 |
int flags = 0; //cv::CALIB_FIX_K3;
|
22 |
jakw |
92 |
|
31 |
jakw |
93 |
std::vector<cv::Vec3f> cam_rvecs0, cam_tvecs0;
|
|
|
94 |
cal.cam0_error = cv::calibrateCamera(Q, qc0, frameSize, cal.K0, cal.k0, cam_rvecs0, cam_tvecs0, flags);
|
27 |
jakw |
95 |
|
31 |
jakw |
96 |
std::vector<cv::Vec3f> cam_rvecs1, cam_tvecs1;
|
|
|
97 |
cal.cam1_error = cv::calibrateCamera(Q, qc1, frameSize, cal.K1, cal.k1, cam_rvecs1, cam_tvecs1, flags);
|
22 |
jakw |
98 |
|
31 |
jakw |
99 |
// stereo calibration (fix K0, K1, k0, k1)
|
|
|
100 |
int flags_stereo = cv::CALIB_FIX_INTRINSIC;
|
|
|
101 |
cal.stereo_error = cv::stereoCalibrate(Q, qc0, qc1, cal.K0, cal.k0, cal.K1, cal.k1,
|
|
|
102 |
frameSize, cal.R1, cal.T1, cal.E, cal.F,
|
22 |
jakw |
103 |
cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 50, DBL_EPSILON),
|
|
|
104 |
flags_stereo);
|
|
|
105 |
|
31 |
jakw |
106 |
// calibrate rotation axis
|
|
|
107 |
std::vector<cv::Matx33f> Rc(nValidSets - 1); // rotations/translations of the checkerboard in camera 0 reference frame
|
|
|
108 |
std::vector<cv::Vec3f> Tc(nValidSets - 1);
|
|
|
109 |
std::vector<cv::Matx33f> Rr(nValidSets - 1); // in rotation stage reference frame
|
|
|
110 |
std::vector<cv::Vec3f> Tr(nValidSets - 1);
|
|
|
111 |
for(int i=0; i<nValidSets-1; i++){
|
|
|
112 |
// relative transformations in camera
|
|
|
113 |
cv::Matx33f cRw1, cRw2;
|
|
|
114 |
cv::Rodrigues(cam_rvecs0[i], cRw1);
|
|
|
115 |
cv::Rodrigues(cam_rvecs0[i+1], cRw2);
|
|
|
116 |
cv::Vec3f cTw1 = cam_tvecs0[i];
|
|
|
117 |
cv::Vec3f cTw2 = cam_tvecs0[i+1];
|
|
|
118 |
|
|
|
119 |
cv::composeRT(cRw1, cTw1, cRw2.t(), -cRw2.t()*cTw2, Rc[i], Tc[i]);
|
|
|
120 |
|
|
|
121 |
// relative transformations in rotation stage
|
|
|
122 |
// we define the rotation axis to be in origo, pointing in positive y direction
|
|
|
123 |
float angleRadians = angles[i]/180.0*M_PI;
|
|
|
124 |
cv::Vec3f rot_rvec(0.0, angleRadians, 0.0);
|
|
|
125 |
cv::Rodrigues(rot_rvec, Rr[i]);
|
|
|
126 |
Tr[i] = 0.0;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
cvtools::fitSixDofData(Rc, Tc, Rr, Tr, cal.Rr, cal.Tr);
|
|
|
130 |
|
27 |
jakw |
131 |
// Print to std::cout
|
|
|
132 |
cal.print();
|
|
|
133 |
|
|
|
134 |
// save to (reentrant qsettings object)
|
|
|
135 |
QSettings settings;
|
|
|
136 |
settings.setValue("calibration", QVariant::fromValue(cal));
|
|
|
137 |
|
|
|
138 |
emit done();
|
|
|
139 |
|
22 |
jakw |
140 |
}
|