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 |
|
33 |
jakw |
10 |
QSettings settings;
|
|
|
11 |
|
22 |
jakw |
12 |
// Number of saddle points on calibration pattern
|
80 |
jakw |
13 |
int checkerCountX = settings.value("calibration/checkerCountX", 22).toInt();
|
|
|
14 |
int checkerCountY = settings.value("calibration/checkerCountY", 13).toInt();
|
33 |
jakw |
15 |
cv::Size checkerCount(checkerCountX, checkerCountY);
|
22 |
jakw |
16 |
|
25 |
jakw |
17 |
int nSets = calibrationData.size();
|
22 |
jakw |
18 |
|
148 |
jakw |
19 |
// 2D Points collected for OpenCV's calibration procedures
|
31 |
jakw |
20 |
std::vector< std::vector<cv::Point2f> > qc0, qc1;
|
137 |
jakw |
21 |
std::vector< std::vector<cv::Point2f> > qc0Stereo, qc1Stereo;
|
|
|
22 |
|
148 |
jakw |
23 |
std::vector<bool> success0(nSets), success1(nSets);
|
|
|
24 |
|
31 |
jakw |
25 |
std::vector<float> angles;
|
22 |
jakw |
26 |
|
|
|
27 |
// Loop through calibration sets
|
|
|
28 |
for(int i=0; i<nSets; i++){
|
|
|
29 |
|
27 |
jakw |
30 |
SMCalibrationSet SMCalibrationSetI = calibrationData[i];
|
25 |
jakw |
31 |
|
27 |
jakw |
32 |
if(!SMCalibrationSetI.checked)
|
22 |
jakw |
33 |
continue;
|
25 |
jakw |
34 |
|
|
|
35 |
// Camera 0
|
|
|
36 |
std::vector<cv::Point2f> qci0;
|
136 |
jakw |
37 |
|
|
|
38 |
// Convert to grayscale
|
123 |
jakw |
39 |
cv::Mat gray;
|
136 |
jakw |
40 |
if(SMCalibrationSetI.frame0.channels() == 1)
|
|
|
41 |
cv::cvtColor(SMCalibrationSetI.frame0, gray, CV_BayerBG2GRAY);
|
|
|
42 |
else
|
|
|
43 |
cv::cvtColor(SMCalibrationSetI.frame0, gray, CV_RGB2GRAY);
|
|
|
44 |
|
25 |
jakw |
45 |
// Extract checker corners
|
148 |
jakw |
46 |
success0[i] = cv::findChessboardCorners(gray, checkerCount, qci0, cv::CALIB_CB_ADAPTIVE_THRESH + cv::CALIB_CB_FAST_CHECK);
|
|
|
47 |
if(success0[i]){
|
134 |
jakw |
48 |
cv::cornerSubPix(gray, qci0, cv::Size(6, 6), cv::Size(1, 1),cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 20, 0.0001));
|
25 |
jakw |
49 |
// Draw colored chessboard
|
120 |
jakw |
50 |
cv::Mat color;
|
136 |
jakw |
51 |
if(SMCalibrationSetI.frame0.channels() == 1)
|
|
|
52 |
cv::cvtColor(SMCalibrationSetI.frame0, color, CV_BayerBG2RGB);
|
|
|
53 |
else
|
|
|
54 |
color = SMCalibrationSetI.frame0.clone();
|
|
|
55 |
|
148 |
jakw |
56 |
cvtools::drawChessboardCorners(color, checkerCount, qci0, success0[i], 10);
|
120 |
jakw |
57 |
SMCalibrationSetI.frame0Result = color;
|
22 |
jakw |
58 |
}
|
|
|
59 |
|
148 |
jakw |
60 |
emit newFrameResult(i, 0, success0[i], SMCalibrationSetI.frame0Result);
|
29 |
jakw |
61 |
|
25 |
jakw |
62 |
// Camera 1
|
|
|
63 |
std::vector<cv::Point2f> qci1;
|
136 |
jakw |
64 |
|
|
|
65 |
// Convert to grayscale
|
|
|
66 |
if(SMCalibrationSetI.frame1.channels() == 1)
|
|
|
67 |
cv::cvtColor(SMCalibrationSetI.frame1, gray, CV_BayerBG2GRAY);
|
|
|
68 |
else
|
|
|
69 |
cv::cvtColor(SMCalibrationSetI.frame1, gray, CV_RGB2GRAY);
|
|
|
70 |
|
25 |
jakw |
71 |
// Extract checker corners
|
148 |
jakw |
72 |
success1[i] = cv::findChessboardCorners(gray, checkerCount, qci1, cv::CALIB_CB_ADAPTIVE_THRESH + cv::CALIB_CB_FAST_CHECK);
|
|
|
73 |
if(success1[i]){
|
134 |
jakw |
74 |
cv::cornerSubPix(gray, qci1, cv::Size(6, 6), cv::Size(1, 1),cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 20, 0.0001));
|
25 |
jakw |
75 |
// Draw colored chessboard
|
120 |
jakw |
76 |
cv::Mat color;
|
136 |
jakw |
77 |
if(SMCalibrationSetI.frame1.channels() == 1)
|
|
|
78 |
cv::cvtColor(SMCalibrationSetI.frame1, color, CV_BayerBG2RGB);
|
|
|
79 |
else
|
|
|
80 |
color = SMCalibrationSetI.frame1.clone();
|
|
|
81 |
|
148 |
jakw |
82 |
cvtools::drawChessboardCorners(color, checkerCount, qci1, success1[i], 10);
|
120 |
jakw |
83 |
SMCalibrationSetI.frame1Result = color;
|
22 |
jakw |
84 |
}
|
|
|
85 |
|
148 |
jakw |
86 |
emit newFrameResult(i, 1, success1[i], SMCalibrationSetI.frame1Result);
|
29 |
jakw |
87 |
|
148 |
jakw |
88 |
if(success0[i])
|
137 |
jakw |
89 |
qc0.push_back(qci0);
|
25 |
jakw |
90 |
|
148 |
jakw |
91 |
if(success1[i])
|
31 |
jakw |
92 |
qc1.push_back(qci1);
|
137 |
jakw |
93 |
|
148 |
jakw |
94 |
if(success0[i] && success1[i]){
|
137 |
jakw |
95 |
qc0Stereo.push_back(qci0);
|
|
|
96 |
qc1Stereo.push_back(qci1);
|
31 |
jakw |
97 |
angles.push_back(SMCalibrationSetI.rotationAngle);
|
22 |
jakw |
98 |
}
|
|
|
99 |
|
27 |
jakw |
100 |
// Show progress
|
|
|
101 |
emit newSetProcessed(i);
|
22 |
jakw |
102 |
}
|
|
|
103 |
|
137 |
jakw |
104 |
int nValidSets = angles.size();
|
27 |
jakw |
105 |
if(nValidSets < 2){
|
22 |
jakw |
106 |
std::cerr << "Not enough valid calibration sequences!" << std::endl;
|
29 |
jakw |
107 |
emit done();
|
22 |
jakw |
108 |
return;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
// Generate world object coordinates [mm]
|
33 |
jakw |
112 |
float checkerSize = settings.value("calibration/checkerSize", 15.0).toFloat(); // width and height of one field in mm
|
22 |
jakw |
113 |
std::vector<cv::Point3f> Qi;
|
33 |
jakw |
114 |
for (int h=0; h<checkerCount.height; h++)
|
|
|
115 |
for (int w=0; w<checkerCount.width; w++)
|
|
|
116 |
Qi.push_back(cv::Point3f(checkerSize * w, checkerSize* h, 0.0));
|
137 |
jakw |
117 |
|
|
|
118 |
std::vector< std::vector<cv::Point3f> > Q0, Q1, QStereo;
|
31 |
jakw |
119 |
for(int i=0; i<qc0.size(); i++)
|
137 |
jakw |
120 |
Q0.push_back(Qi);
|
|
|
121 |
for(int i=0; i<qc1.size(); i++)
|
|
|
122 |
Q1.push_back(Qi);
|
|
|
123 |
for(int i=0; i<nValidSets; i++)
|
|
|
124 |
QStereo.push_back(Qi);
|
22 |
jakw |
125 |
|
|
|
126 |
// calibrate the cameras
|
31 |
jakw |
127 |
SMCalibrationParameters cal;
|
|
|
128 |
cal.frameWidth = calibrationData[0].frame0.cols;
|
|
|
129 |
cal.frameHeight = calibrationData[0].frame0.rows;
|
|
|
130 |
cv::Size frameSize(cal.frameWidth, cal.frameHeight);
|
22 |
jakw |
131 |
|
68 |
jakw |
132 |
// determine only k1, k2 for lens distortion
|
140 |
jakw |
133 |
int flags = cv::CALIB_FIX_ASPECT_RATIO + cv::CALIB_FIX_K3 + cv::CALIB_ZERO_TANGENT_DIST + cv::CALIB_FIX_PRINCIPAL_POINT;
|
33 |
jakw |
134 |
// Note: several of the output arguments below must be cv::Mat, otherwise segfault
|
|
|
135 |
std::vector<cv::Mat> cam_rvecs0, cam_tvecs0;
|
137 |
jakw |
136 |
cal.cam0_error = cv::calibrateCamera(Q0, qc0, frameSize, cal.K0, cal.k0, cam_rvecs0, cam_tvecs0, flags,
|
134 |
jakw |
137 |
cv::TermCriteria(cv::TermCriteria::COUNT+cv::TermCriteria::EPS, 100, DBL_EPSILON));
|
|
|
138 |
//std::cout << cal.k0 << std::endl;
|
120 |
jakw |
139 |
// // refine extrinsics for camera 0
|
|
|
140 |
// for(int i=0; i<Q.size(); i++)
|
|
|
141 |
// cv::solvePnPRansac(Q[i], qc0[i], cal.K0, cal.k0, cam_rvecs0[i], cam_tvecs0[i], true, 100, 0.05, 100, cv::noArray(), CV_ITERATIVE);
|
86 |
jakw |
142 |
|
33 |
jakw |
143 |
std::vector<cv::Mat> cam_rvecs1, cam_tvecs1;
|
137 |
jakw |
144 |
cal.cam1_error = cv::calibrateCamera(Q1, qc1, frameSize, cal.K1, cal.k1, cam_rvecs1, cam_tvecs1, flags,
|
134 |
jakw |
145 |
cv::TermCriteria(cv::TermCriteria::COUNT+cv::TermCriteria::EPS, 100, DBL_EPSILON));
|
|
|
146 |
//std::cout << cal.k1 << std::endl;
|
111 |
jakw |
147 |
// stereo calibration
|
136 |
jakw |
148 |
int flags_stereo = cv::CALIB_FIX_INTRINSIC;// + cv::CALIB_FIX_K2 + cv::CALIB_FIX_K3 + cv::CALIB_ZERO_TANGENT_DIST + cv::CALIB_FIX_PRINCIPAL_POINT + cv::CALIB_FIX_ASPECT_RATIO;
|
33 |
jakw |
149 |
cv::Mat E, F, R1, T1;
|
137 |
jakw |
150 |
cal.stereo_error = cv::stereoCalibrate(QStereo, qc0Stereo, qc1Stereo, cal.K0, cal.k0, cal.K1, cal.k1,
|
33 |
jakw |
151 |
frameSize, R1, T1, E, F,
|
134 |
jakw |
152 |
cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 200, DBL_EPSILON),
|
22 |
jakw |
153 |
flags_stereo);
|
|
|
154 |
|
33 |
jakw |
155 |
cal.R1 = R1;
|
|
|
156 |
cal.T1 = T1;
|
|
|
157 |
cal.E = E;
|
|
|
158 |
cal.F = F;
|
|
|
159 |
|
148 |
jakw |
160 |
// Determine per-view reprojection errors:
|
|
|
161 |
cal.cam0_errors_per_view.resize(nSets);
|
|
|
162 |
int idx = 0;
|
|
|
163 |
for(unsigned int i = 0; i < nSets; ++i){
|
|
|
164 |
if(success0[i]){
|
|
|
165 |
int n = (int)Q0[idx].size();
|
|
|
166 |
std::vector<cv::Point2f> qc_proj;
|
|
|
167 |
cv::projectPoints(cv::Mat(Q0[idx]), cam_rvecs0[idx], cam_tvecs0[idx], cal.K0, cal.k0, qc_proj);
|
|
|
168 |
float err = 0;
|
|
|
169 |
for(int j=0; j<qc_proj.size(); j++){
|
|
|
170 |
cv::Point2f d = qc0[idx][j] - qc_proj[j];
|
|
|
171 |
err += cv::sqrt(d.x*d.x + d.y*d.y);
|
|
|
172 |
}
|
|
|
173 |
cal.cam0_errors_per_view[i] = (float)err/n;
|
|
|
174 |
idx++;
|
|
|
175 |
} else
|
|
|
176 |
cal.cam0_errors_per_view[i] = NAN;
|
|
|
177 |
}
|
|
|
178 |
cal.cam1_errors_per_view.resize(nSets);
|
|
|
179 |
idx = 0;
|
|
|
180 |
for(unsigned int i = 0; i < nSets; ++i){
|
|
|
181 |
if(success1[i]){
|
|
|
182 |
int n = (int)Q1[idx].size();
|
|
|
183 |
std::vector<cv::Point2f> qc_proj;
|
|
|
184 |
cv::projectPoints(cv::Mat(Q1[idx]), cam_rvecs1[idx], cam_tvecs1[idx], cal.K1, cal.k1, qc_proj);
|
|
|
185 |
float err = 0;
|
|
|
186 |
for(int j=0; j<qc_proj.size(); j++){
|
|
|
187 |
cv::Point2f d = qc1[idx][j] - qc_proj[j];
|
|
|
188 |
err += cv::sqrt(d.x*d.x + d.y*d.y);
|
|
|
189 |
}
|
|
|
190 |
cal.cam1_errors_per_view[i] = (float)err/n;
|
|
|
191 |
idx++;
|
|
|
192 |
} else
|
|
|
193 |
cal.cam1_errors_per_view[i] = NAN;
|
|
|
194 |
}
|
|
|
195 |
|
91 |
jakw |
196 |
// // hand-eye calibration
|
|
|
197 |
// std::vector<cv::Matx33f> Rc(nValidSets - 1); // rotations/translations of the checkerboard in camera 0 reference frame
|
|
|
198 |
// std::vector<cv::Vec3f> Tc(nValidSets - 1);
|
|
|
199 |
// std::vector<cv::Matx33f> Rr(nValidSets - 1); // in rotation stage reference frame
|
|
|
200 |
// std::vector<cv::Vec3f> Tr(nValidSets - 1);
|
|
|
201 |
// for(int i=0; i<nValidSets-1; i++){
|
|
|
202 |
// // relative transformations in camera
|
|
|
203 |
// cv::Mat cRw1, cRw2;
|
|
|
204 |
// cv::Rodrigues(cam_rvecs0[i], cRw1);
|
|
|
205 |
// cv::Rodrigues(cam_rvecs0[i+1], cRw2);
|
|
|
206 |
// cv::Mat cTw1 = cam_tvecs0[i];
|
|
|
207 |
// cv::Mat cTw2 = cam_tvecs0[i+1];
|
|
|
208 |
// cv::Mat w1Rc = cRw1.t();
|
|
|
209 |
// cv::Mat w1Tc = -cRw1.t()*cTw1;
|
|
|
210 |
// Rc[i] = cv::Mat(cRw2*w1Rc);
|
|
|
211 |
// Tc[i] = cv::Mat(cRw2*w1Tc+cTw2);
|
31 |
jakw |
212 |
|
91 |
jakw |
213 |
// // relative transformations in rotation stage
|
|
|
214 |
// // we define the rotation axis to be in origo, pointing in positive y direction
|
|
|
215 |
// float angleRadians = (angles[i+1]-angles[i])/180.0*M_PI;
|
|
|
216 |
// cv::Vec3f rot_rvec(0.0, -angleRadians, 0.0);
|
|
|
217 |
// cv::Mat Rri;
|
|
|
218 |
// cv::Rodrigues(rot_rvec, Rri);
|
|
|
219 |
// Rr[i] = Rri;
|
|
|
220 |
// Tr[i] = 0.0;
|
33 |
jakw |
221 |
|
91 |
jakw |
222 |
//// std::cout << i << std::endl;
|
|
|
223 |
//// std::cout << "cTw1" << cTw1 << std::endl;
|
|
|
224 |
//// std::cout << "cTw2" << cTw2 << std::endl;
|
|
|
225 |
//// std::cout << "w2Rc" << w2Rc << std::endl;
|
|
|
226 |
//// std::cout << "w2Tc" << w2Tc << std::endl;
|
|
|
227 |
|
|
|
228 |
//// std::cout << "w2Rc" << w2Rc << std::endl;
|
|
|
229 |
//// std::cout << "w2Tc" << w2Tc << std::endl;
|
|
|
230 |
|
|
|
231 |
//// cv::Mat Rci;
|
|
|
232 |
//// cv::Rodrigues(Rc[i], Rci);
|
|
|
233 |
//// std::cout << "Rci" << Rci << std::endl;
|
|
|
234 |
//// std::cout << "Tc[i]" << Tc[i] << std::endl;
|
|
|
235 |
|
|
|
236 |
//// std::cout << "rot_rvec" << rot_rvec << std::endl;
|
|
|
237 |
//// std::cout << "Tr[i]" << Tr[i] << std::endl;
|
|
|
238 |
//// std::cout << std::endl;
|
|
|
239 |
// }
|
|
|
240 |
|
|
|
241 |
// // determine the transformation from rotation stage to camera 0
|
|
|
242 |
// cvtools::handEyeCalibrationTsai(Rc, Tc, Rr, Tr, cal.Rr, cal.Tr);
|
|
|
243 |
|
|
|
244 |
// for(int i=0; i<nValidSets-1; i++){
|
81 |
jakw |
245 |
// std::cout << i << std::endl;
|
33 |
jakw |
246 |
|
81 |
jakw |
247 |
// cv::Mat Rci;
|
|
|
248 |
// cv::Rodrigues(Rc[i], Rci);
|
91 |
jakw |
249 |
// std::cout << "Rc[i]" << Rci << std::endl;
|
81 |
jakw |
250 |
// std::cout << "Tc[i]" << Tc[i] << std::endl;
|
|
|
251 |
|
91 |
jakw |
252 |
// cv::Mat Rri;
|
|
|
253 |
// cv::Rodrigues(Rr[i], Rri);
|
|
|
254 |
// std::cout << "Rr[i]" << Rri << std::endl;
|
81 |
jakw |
255 |
// std::cout << "Tr[i]" << Tr[i] << std::endl;
|
91 |
jakw |
256 |
|
|
|
257 |
// cv::Mat Rcr = cv::Mat(cal.Rr)*cv::Mat(Rc[i])*cv::Mat(cal.Rr.t());
|
|
|
258 |
// cv::Rodrigues(Rcr, Rcr);
|
|
|
259 |
// cv::Mat Tcr = -cv::Mat(cal.Rr)*cv::Mat(Rc[i])*cv::Mat(cal.Rr.t())*cv::Mat(cal.Tr) + cv::Mat(cal.Rr)*cv::Mat(Tc[i]) + cv::Mat(cal.Tr);
|
|
|
260 |
// std::cout << "Rcr[i]" << Rcr << std::endl;
|
|
|
261 |
// std::cout << "Tcr[i]" << Tcr << std::endl;
|
81 |
jakw |
262 |
// std::cout << std::endl;
|
91 |
jakw |
263 |
// }
|
81 |
jakw |
264 |
|
|
|
265 |
|
91 |
jakw |
266 |
// Direct rotation axis calibration //
|
|
|
267 |
// full camera matrices
|
|
|
268 |
cv::Matx34f P0 = cv::Matx34f::eye();
|
|
|
269 |
cv::Mat RT1(3, 4, CV_32F);
|
|
|
270 |
cv::Mat(cal.R1).copyTo(RT1(cv::Range(0, 3), cv::Range(0, 3)));
|
|
|
271 |
cv::Mat(cal.T1).copyTo(RT1(cv::Range(0, 3), cv::Range(3, 4)));
|
|
|
272 |
cv::Matx34f P1 = cv::Matx34f(RT1);
|
81 |
jakw |
273 |
|
91 |
jakw |
274 |
// calibration points in camera 0 frame
|
|
|
275 |
std::vector< std::vector<cv::Point3f> > Qcam;
|
33 |
jakw |
276 |
|
91 |
jakw |
277 |
for(int i=0; i<nValidSets; i++){
|
|
|
278 |
std::vector<cv::Point2f> qc0i, qc1i;
|
81 |
jakw |
279 |
|
91 |
jakw |
280 |
cv::undistortPoints(qc0[i], qc0i, cal.K0, cal.k0);
|
|
|
281 |
cv::undistortPoints(qc1[i], qc1i, cal.K1, cal.k1);
|
88 |
jakw |
282 |
// qc0i = qc0[i];
|
|
|
283 |
// qc1i = qc1[i];
|
84 |
jakw |
284 |
|
91 |
jakw |
285 |
cv::Mat Qhom, Qcami;
|
|
|
286 |
cv::triangulatePoints(P0, P1, qc0i, qc1i, Qhom);
|
|
|
287 |
cvtools::convertMatFromHomogeneous(Qhom, Qcami);
|
|
|
288 |
std::vector<cv::Point3f> QcamiPoints;
|
|
|
289 |
cvtools::matToPoints3f(Qcami, QcamiPoints);
|
84 |
jakw |
290 |
|
91 |
jakw |
291 |
Qcam.push_back(QcamiPoints);
|
|
|
292 |
}
|
84 |
jakw |
293 |
|
91 |
jakw |
294 |
cv::Vec3f axis, point;
|
|
|
295 |
cvtools::rotationAxisCalibration(Qcam, Qi, axis, point);
|
84 |
jakw |
296 |
|
91 |
jakw |
297 |
// construct transformation matrix
|
|
|
298 |
cv::Vec3f ex = axis.cross(cv::Vec3f(0,0,1.0));
|
|
|
299 |
ex = cv::normalize(ex);
|
|
|
300 |
cv::Vec3f ez = ex.cross(axis);
|
|
|
301 |
ez = cv::normalize(ez);
|
84 |
jakw |
302 |
|
91 |
jakw |
303 |
cv::Mat RrMat(3, 3, CV_32F);
|
|
|
304 |
cv::Mat(ex).copyTo(RrMat.col(0));
|
|
|
305 |
cv::Mat(axis).copyTo(RrMat.col(1));
|
|
|
306 |
cv::Mat(ez).copyTo(RrMat.col(2));
|
84 |
jakw |
307 |
|
91 |
jakw |
308 |
cal.Rr = cv::Matx33f(RrMat).t();
|
|
|
309 |
cal.Tr = -cv::Matx33f(RrMat).t()*point;
|
84 |
jakw |
310 |
|
27 |
jakw |
311 |
// Print to std::cout
|
|
|
312 |
cal.print();
|
|
|
313 |
|
|
|
314 |
// save to (reentrant qsettings object)
|
33 |
jakw |
315 |
settings.setValue("calibration/parameters", QVariant::fromValue(cal));
|
27 |
jakw |
316 |
|
|
|
317 |
emit done();
|
|
|
318 |
|
22 |
jakw |
319 |
}
|