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 |
|
196 |
jakw |
8 |
#include <ceres/ceres.h>
|
|
|
9 |
|
|
|
10 |
|
|
|
11 |
struct CircleResidual {
|
|
|
12 |
CircleResidual(std::vector<cv::Point3f> _pointsOnArc)
|
|
|
13 |
: pointsOnArc(_pointsOnArc) {}
|
|
|
14 |
|
|
|
15 |
template <typename T>
|
197 |
jakw |
16 |
bool operator()(const T* point, const T* axis, T* residual) const {
|
196 |
jakw |
17 |
|
197 |
jakw |
18 |
T axisSqNorm = axis[0]*axis[0] + axis[1]*axis[1] + axis[2]*axis[2];
|
196 |
jakw |
19 |
|
|
|
20 |
unsigned int l = pointsOnArc.size();
|
197 |
jakw |
21 |
std::vector<T> dI(l);
|
|
|
22 |
|
|
|
23 |
// note, this is automatically initialized to 0
|
|
|
24 |
T sum(0.0);
|
|
|
25 |
|
196 |
jakw |
26 |
for(unsigned int i=0; i<l; i++){
|
197 |
jakw |
27 |
cv::Point3d p = pointsOnArc[i];
|
|
|
28 |
//T p[3] = {pointsOnArc[i].x, pointsOnArc[i].y, pointsOnArc[i].z};
|
|
|
29 |
|
196 |
jakw |
30 |
// point to line distance
|
197 |
jakw |
31 |
T dotProd = (point[0]-p.x)*axis[0] + (point[1]-p.y)*axis[1] + (point[2]-p.z)*axis[2];
|
|
|
32 |
T dIx = point[0] - p.x - (dotProd*axis[0]/axisSqNorm);
|
|
|
33 |
T dIy = point[1] - p.y - (dotProd*axis[1]/axisSqNorm);
|
|
|
34 |
T dIz = point[2] - p.z - (dotProd*axis[2]/axisSqNorm);
|
|
|
35 |
dI[i] = ceres::sqrt(dIx*dIx + dIy*dIy + dIz*dIz);
|
|
|
36 |
|
|
|
37 |
sum += dI[i];
|
196 |
jakw |
38 |
}
|
197 |
jakw |
39 |
|
|
|
40 |
T mean = sum / double(l);
|
|
|
41 |
|
|
|
42 |
for(unsigned int i=0; i<l; i++){
|
|
|
43 |
residual[i] = dI[i] - mean;
|
196 |
jakw |
44 |
}
|
|
|
45 |
|
|
|
46 |
return true;
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
private:
|
|
|
50 |
|
|
|
51 |
// Observations for one checkerboard corner.
|
|
|
52 |
const std::vector<cv::Point3f> pointsOnArc;
|
|
|
53 |
};
|
|
|
54 |
|
|
|
55 |
|
|
|
56 |
// Closed form solution to solve for the rotation axis from sets of 3D point coordinates of flat pattern feature points
|
|
|
57 |
// Algorithm according to Chen et al., Rotation axis calibration of a turntable using constrained global optimization, Optik 2014
|
|
|
58 |
// DTU, 2014, Jakob Wilm
|
|
|
59 |
static void rotationAxisCalibration(const std::vector< std::vector<cv::Point3f> > Qcam, const std::vector<cv::Point3f> Qobj, cv::Vec3f &axis, cv::Vec3f &point, float &error){
|
|
|
60 |
|
|
|
61 |
// number of frames (points on each arch)
|
|
|
62 |
int l = Qcam.size();
|
|
|
63 |
|
|
|
64 |
// number of points in each frame
|
|
|
65 |
size_t mn = Qobj.size();
|
|
|
66 |
|
|
|
67 |
assert(mn == Qcam[0].size());
|
|
|
68 |
|
|
|
69 |
// construct matrix for axis determination
|
|
|
70 |
cv::Mat M(6, 6, CV_32F, cv::Scalar(0));
|
|
|
71 |
|
|
|
72 |
for(int k=0; k<l; k++){
|
|
|
73 |
for(unsigned int idx=0; idx<mn; idx++){
|
|
|
74 |
|
|
|
75 |
// float i = Qobj[idx].x+4;
|
|
|
76 |
// float j = Qobj[idx].y+4;
|
|
|
77 |
float i = Qobj[idx].x;
|
|
|
78 |
float j = Qobj[idx].y;
|
|
|
79 |
|
|
|
80 |
float x = Qcam[k][idx].x;
|
|
|
81 |
float y = Qcam[k][idx].y;
|
|
|
82 |
float z = Qcam[k][idx].z;
|
|
|
83 |
|
|
|
84 |
M += (cv::Mat_<float>(6,6) << x*x, x*y, x*z, x, i*x, j*x,
|
|
|
85 |
0, y*y, y*z, y, i*y, j*y,
|
|
|
86 |
0, 0, z*z, z, i*z, j*z,
|
|
|
87 |
0, 0, 0, 1, i, j,
|
|
|
88 |
0, 0, 0, 0, i*i, i*j,
|
|
|
89 |
0, 0, 0, 0, 0, j*j);
|
|
|
90 |
|
|
|
91 |
}
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
cv::completeSymm(M, false);
|
|
|
95 |
|
|
|
96 |
// solve for axis
|
|
|
97 |
std::vector<float> lambda;
|
|
|
98 |
cv::Mat u;
|
|
|
99 |
cv::eigen(M, lambda, u);
|
|
|
100 |
|
|
|
101 |
float minLambda = std::abs(lambda[0]);
|
|
|
102 |
int idx = 0;
|
|
|
103 |
for(unsigned int i=1; i<lambda.size(); i++){
|
|
|
104 |
if(abs(lambda[i]) < minLambda){
|
|
|
105 |
minLambda = lambda[i];
|
|
|
106 |
idx = i;
|
|
|
107 |
}
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
axis = u.row(idx).colRange(0, 3);
|
|
|
111 |
axis = cv::normalize(axis);
|
|
|
112 |
|
|
|
113 |
float nx = u.at<float>(idx, 0);
|
|
|
114 |
float ny = u.at<float>(idx, 1);
|
|
|
115 |
float nz = u.at<float>(idx, 2);
|
|
|
116 |
//float d = u.at<float>(idx, 3);
|
|
|
117 |
float dh = u.at<float>(idx, 4);
|
|
|
118 |
float dv = u.at<float>(idx, 5);
|
|
|
119 |
|
|
|
120 |
// // Paper version: c is initially eliminated
|
|
|
121 |
// cv::Mat A(l*mn, mn+2, CV_32F, cv::Scalar(0.0));
|
|
|
122 |
// cv::Mat bb(l*mn, 1, CV_32F);
|
|
|
123 |
|
|
|
124 |
// for(int k=0; k<l; k++){
|
|
|
125 |
// for(unsigned int idx=0; idx<mn; idx++){
|
|
|
126 |
|
|
|
127 |
// float i = Qobj[idx].x;
|
|
|
128 |
// float j = Qobj[idx].y;
|
|
|
129 |
|
|
|
130 |
// float x = Qcam[k][idx].x;
|
|
|
131 |
// float y = Qcam[k][idx].y;
|
|
|
132 |
// float z = Qcam[k][idx].z;
|
|
|
133 |
|
|
|
134 |
// float f = x*x + y*y + z*z + (2*x*nx + 2*y*ny + 2*z*nz)*(i*dh + j*dv);
|
|
|
135 |
|
|
|
136 |
// int row = k*mn+idx;
|
|
|
137 |
// A.at<float>(row, 0) = 2*x - (2*z*nx)/nz;
|
|
|
138 |
// A.at<float>(row, 1) = 2*y - (2*z*ny)/nz;
|
|
|
139 |
// A.at<float>(row, idx+2) = 1.0;
|
|
|
140 |
|
|
|
141 |
// bb.at<float>(row, 0) = f + (2*z*d)/nz;
|
|
|
142 |
// }
|
|
|
143 |
// }
|
|
|
144 |
|
|
|
145 |
// // solve for point
|
|
|
146 |
// cv::Mat abe;
|
|
|
147 |
// cv::solve(A, bb, abe, cv::DECOMP_SVD);
|
|
|
148 |
|
|
|
149 |
// float a = abe.at<float>(0, 0);
|
|
|
150 |
// float b = abe.at<float>(1, 0);
|
|
|
151 |
// float c = -(nx*a+ny*b+d)/nz;
|
|
|
152 |
|
|
|
153 |
// Our version: solve simultanously for a,b,c
|
|
|
154 |
cv::Mat A(l*mn, mn+3, CV_32F, cv::Scalar(0.0));
|
|
|
155 |
cv::Mat bb(l*mn, 1, CV_32F);
|
|
|
156 |
|
|
|
157 |
for(int k=0; k<l; k++){
|
|
|
158 |
for(unsigned int idx=0; idx<mn; idx++){
|
|
|
159 |
|
|
|
160 |
float i = Qobj[idx].x;
|
|
|
161 |
float j = Qobj[idx].y;
|
|
|
162 |
|
|
|
163 |
float x = Qcam[k][idx].x;
|
|
|
164 |
float y = Qcam[k][idx].y;
|
|
|
165 |
float z = Qcam[k][idx].z;
|
|
|
166 |
|
|
|
167 |
float f = x*x + y*y + z*z + (2*x*nx + 2*y*ny + 2*z*nz)*(i*dh + j*dv);
|
|
|
168 |
|
|
|
169 |
int row = k*mn+idx;
|
|
|
170 |
A.at<float>(row, 0) = 2*x;
|
|
|
171 |
A.at<float>(row, 1) = 2*y;
|
|
|
172 |
A.at<float>(row, 2) = 2*z;
|
|
|
173 |
A.at<float>(row, idx+3) = 1.0;
|
|
|
174 |
|
|
|
175 |
bb.at<float>(row, 0) = f;
|
|
|
176 |
}
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
// solve for point
|
|
|
180 |
cv::Mat abe;
|
|
|
181 |
cv::solve(A, bb, abe, cv::DECOMP_SVD);
|
|
|
182 |
|
|
|
183 |
float a = abe.at<float>(0, 0);
|
|
|
184 |
float b = abe.at<float>(1, 0);
|
|
|
185 |
float c = abe.at<float>(2, 0);
|
|
|
186 |
|
|
|
187 |
point[0] = a;
|
|
|
188 |
point[1] = b;
|
|
|
189 |
point[2] = c;
|
|
|
190 |
|
|
|
191 |
// Non-linear optimization using Ceres
|
197 |
jakw |
192 |
double pointArray[] = {point[0], point[1], point[2]};
|
|
|
193 |
double axisArray[] = {axis[0], axis[1], axis[2]};
|
196 |
jakw |
194 |
|
|
|
195 |
ceres::Problem problem;
|
|
|
196 |
// loop through saddle points
|
|
|
197 |
for(unsigned int idx=0; idx<mn; idx++){
|
|
|
198 |
std::vector<cv::Point3f> pointsOnArch(l);
|
|
|
199 |
for(int k=0; k<l; k++){
|
|
|
200 |
pointsOnArch[k] = Qcam[k][idx];
|
|
|
201 |
}
|
|
|
202 |
ceres::CostFunction* cost_function =
|
197 |
jakw |
203 |
new ceres::AutoDiffCostFunction<CircleResidual, ceres::DYNAMIC, 3, 3>(
|
|
|
204 |
new CircleResidual(pointsOnArch), l);
|
|
|
205 |
problem.AddResidualBlock(cost_function, NULL, pointArray, axisArray);
|
196 |
jakw |
206 |
}
|
|
|
207 |
|
|
|
208 |
// Run the solver!
|
|
|
209 |
ceres::Solver::Options options;
|
|
|
210 |
options.linear_solver_type = ceres::DENSE_QR;
|
|
|
211 |
options.minimizer_progress_to_stdout = true;
|
|
|
212 |
ceres::Solver::Summary summary;
|
|
|
213 |
ceres::Solve(options, &problem, &summary);
|
|
|
214 |
|
|
|
215 |
std::cout << summary.BriefReport() << "\n";
|
|
|
216 |
|
197 |
jakw |
217 |
point = cv::Vec3f(pointArray[0], pointArray[1], pointArray[2]);
|
|
|
218 |
axis = cv::Vec3f(axisArray[0], axisArray[1], axisArray[2]);
|
|
|
219 |
axis /= cv::norm(axis);
|
196 |
jakw |
220 |
|
|
|
221 |
|
197 |
jakw |
222 |
// Error estimate (sum of squared differences)
|
196 |
jakw |
223 |
error = 0;
|
|
|
224 |
// loop through saddle points
|
|
|
225 |
for(unsigned int idx=0; idx<mn; idx++){
|
|
|
226 |
|
|
|
227 |
// vector of distances from rotation axis
|
|
|
228 |
std::vector<float> dI(l);
|
|
|
229 |
// loop through angular positions
|
|
|
230 |
for(int k=0; k<l; k++){
|
|
|
231 |
cv::Vec3f p = cv::Vec3f(Qcam[k][idx]);
|
|
|
232 |
// point to line distance
|
|
|
233 |
dI[k] = cv::norm((point-p)-(point-p).dot(axis)*axis);
|
|
|
234 |
}
|
|
|
235 |
float sum = std::accumulate(dI.begin(), dI.end(), 0.0);
|
|
|
236 |
float mean = sum / dI.size();
|
197 |
jakw |
237 |
float sumSqDev = 0;
|
196 |
jakw |
238 |
for(int k=0; k<l; k++){
|
197 |
jakw |
239 |
sumSqDev += (dI[k] - mean)*(dI[k] - mean);
|
196 |
jakw |
240 |
}
|
197 |
jakw |
241 |
//meanDev /= l;
|
196 |
jakw |
242 |
//std::cout << meanDev << std::endl;
|
197 |
jakw |
243 |
error += sumSqDev;
|
196 |
jakw |
244 |
}
|
197 |
jakw |
245 |
//error /= mn;
|
196 |
jakw |
246 |
}
|
|
|
247 |
|
27 |
jakw |
248 |
void SMCalibrationWorker::performCalibration(std::vector<SMCalibrationSet> calibrationData){
|
22 |
jakw |
249 |
|
33 |
jakw |
250 |
QSettings settings;
|
|
|
251 |
|
22 |
jakw |
252 |
// Number of saddle points on calibration pattern
|
169 |
jakw |
253 |
int checkerCountX = settings.value("calibration/patternSizeX", 22).toInt();
|
|
|
254 |
int checkerCountY = settings.value("calibration/patternSizeY", 13).toInt();
|
33 |
jakw |
255 |
cv::Size checkerCount(checkerCountX, checkerCountY);
|
22 |
jakw |
256 |
|
167 |
jakw |
257 |
unsigned int nSets = calibrationData.size();
|
22 |
jakw |
258 |
|
148 |
jakw |
259 |
// 2D Points collected for OpenCV's calibration procedures
|
31 |
jakw |
260 |
std::vector< std::vector<cv::Point2f> > qc0, qc1;
|
137 |
jakw |
261 |
std::vector< std::vector<cv::Point2f> > qc0Stereo, qc1Stereo;
|
|
|
262 |
|
148 |
jakw |
263 |
std::vector<bool> success0(nSets), success1(nSets);
|
|
|
264 |
|
31 |
jakw |
265 |
std::vector<float> angles;
|
22 |
jakw |
266 |
|
|
|
267 |
// Loop through calibration sets
|
167 |
jakw |
268 |
for(unsigned int i=0; i<nSets; i++){
|
22 |
jakw |
269 |
|
27 |
jakw |
270 |
SMCalibrationSet SMCalibrationSetI = calibrationData[i];
|
25 |
jakw |
271 |
|
27 |
jakw |
272 |
if(!SMCalibrationSetI.checked)
|
22 |
jakw |
273 |
continue;
|
25 |
jakw |
274 |
|
|
|
275 |
// Camera 0
|
|
|
276 |
std::vector<cv::Point2f> qci0;
|
136 |
jakw |
277 |
|
|
|
278 |
// Convert to grayscale
|
123 |
jakw |
279 |
cv::Mat gray;
|
136 |
jakw |
280 |
if(SMCalibrationSetI.frame0.channels() == 1)
|
|
|
281 |
cv::cvtColor(SMCalibrationSetI.frame0, gray, CV_BayerBG2GRAY);
|
|
|
282 |
else
|
|
|
283 |
cv::cvtColor(SMCalibrationSetI.frame0, gray, CV_RGB2GRAY);
|
|
|
284 |
|
25 |
jakw |
285 |
// Extract checker corners
|
148 |
jakw |
286 |
success0[i] = cv::findChessboardCorners(gray, checkerCount, qci0, cv::CALIB_CB_ADAPTIVE_THRESH + cv::CALIB_CB_FAST_CHECK);
|
|
|
287 |
if(success0[i]){
|
134 |
jakw |
288 |
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 |
289 |
// Draw colored chessboard
|
120 |
jakw |
290 |
cv::Mat color;
|
136 |
jakw |
291 |
if(SMCalibrationSetI.frame0.channels() == 1)
|
|
|
292 |
cv::cvtColor(SMCalibrationSetI.frame0, color, CV_BayerBG2RGB);
|
|
|
293 |
else
|
|
|
294 |
color = SMCalibrationSetI.frame0.clone();
|
|
|
295 |
|
148 |
jakw |
296 |
cvtools::drawChessboardCorners(color, checkerCount, qci0, success0[i], 10);
|
120 |
jakw |
297 |
SMCalibrationSetI.frame0Result = color;
|
22 |
jakw |
298 |
}
|
|
|
299 |
|
148 |
jakw |
300 |
emit newFrameResult(i, 0, success0[i], SMCalibrationSetI.frame0Result);
|
29 |
jakw |
301 |
|
25 |
jakw |
302 |
// Camera 1
|
|
|
303 |
std::vector<cv::Point2f> qci1;
|
136 |
jakw |
304 |
|
|
|
305 |
// Convert to grayscale
|
|
|
306 |
if(SMCalibrationSetI.frame1.channels() == 1)
|
|
|
307 |
cv::cvtColor(SMCalibrationSetI.frame1, gray, CV_BayerBG2GRAY);
|
|
|
308 |
else
|
|
|
309 |
cv::cvtColor(SMCalibrationSetI.frame1, gray, CV_RGB2GRAY);
|
|
|
310 |
|
25 |
jakw |
311 |
// Extract checker corners
|
148 |
jakw |
312 |
success1[i] = cv::findChessboardCorners(gray, checkerCount, qci1, cv::CALIB_CB_ADAPTIVE_THRESH + cv::CALIB_CB_FAST_CHECK);
|
|
|
313 |
if(success1[i]){
|
134 |
jakw |
314 |
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 |
315 |
// Draw colored chessboard
|
120 |
jakw |
316 |
cv::Mat color;
|
136 |
jakw |
317 |
if(SMCalibrationSetI.frame1.channels() == 1)
|
|
|
318 |
cv::cvtColor(SMCalibrationSetI.frame1, color, CV_BayerBG2RGB);
|
|
|
319 |
else
|
|
|
320 |
color = SMCalibrationSetI.frame1.clone();
|
|
|
321 |
|
148 |
jakw |
322 |
cvtools::drawChessboardCorners(color, checkerCount, qci1, success1[i], 10);
|
120 |
jakw |
323 |
SMCalibrationSetI.frame1Result = color;
|
22 |
jakw |
324 |
}
|
|
|
325 |
|
148 |
jakw |
326 |
emit newFrameResult(i, 1, success1[i], SMCalibrationSetI.frame1Result);
|
29 |
jakw |
327 |
|
148 |
jakw |
328 |
if(success0[i])
|
137 |
jakw |
329 |
qc0.push_back(qci0);
|
25 |
jakw |
330 |
|
148 |
jakw |
331 |
if(success1[i])
|
31 |
jakw |
332 |
qc1.push_back(qci1);
|
137 |
jakw |
333 |
|
148 |
jakw |
334 |
if(success0[i] && success1[i]){
|
137 |
jakw |
335 |
qc0Stereo.push_back(qci0);
|
|
|
336 |
qc1Stereo.push_back(qci1);
|
31 |
jakw |
337 |
angles.push_back(SMCalibrationSetI.rotationAngle);
|
22 |
jakw |
338 |
}
|
|
|
339 |
|
27 |
jakw |
340 |
// Show progress
|
|
|
341 |
emit newSetProcessed(i);
|
22 |
jakw |
342 |
}
|
|
|
343 |
|
167 |
jakw |
344 |
size_t nValidSets = angles.size();
|
27 |
jakw |
345 |
if(nValidSets < 2){
|
22 |
jakw |
346 |
std::cerr << "Not enough valid calibration sequences!" << std::endl;
|
29 |
jakw |
347 |
emit done();
|
22 |
jakw |
348 |
return;
|
|
|
349 |
}
|
|
|
350 |
|
|
|
351 |
// Generate world object coordinates [mm]
|
166 |
jakw |
352 |
float checkerSize = settings.value("calibration/squareSize", 10.0).toFloat(); // width and height of one checker square in mm
|
22 |
jakw |
353 |
std::vector<cv::Point3f> Qi;
|
33 |
jakw |
354 |
for (int h=0; h<checkerCount.height; h++)
|
|
|
355 |
for (int w=0; w<checkerCount.width; w++)
|
|
|
356 |
Qi.push_back(cv::Point3f(checkerSize * w, checkerSize* h, 0.0));
|
137 |
jakw |
357 |
|
|
|
358 |
std::vector< std::vector<cv::Point3f> > Q0, Q1, QStereo;
|
167 |
jakw |
359 |
for(unsigned int i=0; i<qc0.size(); i++)
|
137 |
jakw |
360 |
Q0.push_back(Qi);
|
167 |
jakw |
361 |
for(unsigned int i=0; i<qc1.size(); i++)
|
137 |
jakw |
362 |
Q1.push_back(Qi);
|
167 |
jakw |
363 |
for(unsigned int i=0; i<nValidSets; i++)
|
137 |
jakw |
364 |
QStereo.push_back(Qi);
|
22 |
jakw |
365 |
|
|
|
366 |
// calibrate the cameras
|
31 |
jakw |
367 |
SMCalibrationParameters cal;
|
|
|
368 |
cal.frameWidth = calibrationData[0].frame0.cols;
|
|
|
369 |
cal.frameHeight = calibrationData[0].frame0.rows;
|
|
|
370 |
cv::Size frameSize(cal.frameWidth, cal.frameHeight);
|
22 |
jakw |
371 |
|
68 |
jakw |
372 |
// determine only k1, k2 for lens distortion
|
140 |
jakw |
373 |
int flags = cv::CALIB_FIX_ASPECT_RATIO + cv::CALIB_FIX_K3 + cv::CALIB_ZERO_TANGENT_DIST + cv::CALIB_FIX_PRINCIPAL_POINT;
|
33 |
jakw |
374 |
// Note: several of the output arguments below must be cv::Mat, otherwise segfault
|
|
|
375 |
std::vector<cv::Mat> cam_rvecs0, cam_tvecs0;
|
137 |
jakw |
376 |
cal.cam0_error = cv::calibrateCamera(Q0, qc0, frameSize, cal.K0, cal.k0, cam_rvecs0, cam_tvecs0, flags,
|
134 |
jakw |
377 |
cv::TermCriteria(cv::TermCriteria::COUNT+cv::TermCriteria::EPS, 100, DBL_EPSILON));
|
|
|
378 |
//std::cout << cal.k0 << std::endl;
|
120 |
jakw |
379 |
// // refine extrinsics for camera 0
|
|
|
380 |
// for(int i=0; i<Q.size(); i++)
|
|
|
381 |
// 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 |
382 |
|
33 |
jakw |
383 |
std::vector<cv::Mat> cam_rvecs1, cam_tvecs1;
|
137 |
jakw |
384 |
cal.cam1_error = cv::calibrateCamera(Q1, qc1, frameSize, cal.K1, cal.k1, cam_rvecs1, cam_tvecs1, flags,
|
134 |
jakw |
385 |
cv::TermCriteria(cv::TermCriteria::COUNT+cv::TermCriteria::EPS, 100, DBL_EPSILON));
|
|
|
386 |
//std::cout << cal.k1 << std::endl;
|
111 |
jakw |
387 |
// stereo calibration
|
136 |
jakw |
388 |
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 |
389 |
cv::Mat E, F, R1, T1;
|
137 |
jakw |
390 |
cal.stereo_error = cv::stereoCalibrate(QStereo, qc0Stereo, qc1Stereo, cal.K0, cal.k0, cal.K1, cal.k1,
|
33 |
jakw |
391 |
frameSize, R1, T1, E, F,
|
134 |
jakw |
392 |
cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 200, DBL_EPSILON),
|
22 |
jakw |
393 |
flags_stereo);
|
|
|
394 |
|
33 |
jakw |
395 |
cal.R1 = R1;
|
|
|
396 |
cal.T1 = T1;
|
|
|
397 |
cal.E = E;
|
|
|
398 |
cal.F = F;
|
|
|
399 |
|
148 |
jakw |
400 |
// Determine per-view reprojection errors:
|
|
|
401 |
cal.cam0_errors_per_view.resize(nSets);
|
|
|
402 |
int idx = 0;
|
|
|
403 |
for(unsigned int i = 0; i < nSets; ++i){
|
|
|
404 |
if(success0[i]){
|
|
|
405 |
int n = (int)Q0[idx].size();
|
|
|
406 |
std::vector<cv::Point2f> qc_proj;
|
|
|
407 |
cv::projectPoints(cv::Mat(Q0[idx]), cam_rvecs0[idx], cam_tvecs0[idx], cal.K0, cal.k0, qc_proj);
|
|
|
408 |
float err = 0;
|
167 |
jakw |
409 |
for(unsigned int j=0; j<qc_proj.size(); j++){
|
148 |
jakw |
410 |
cv::Point2f d = qc0[idx][j] - qc_proj[j];
|
|
|
411 |
err += cv::sqrt(d.x*d.x + d.y*d.y);
|
|
|
412 |
}
|
|
|
413 |
cal.cam0_errors_per_view[i] = (float)err/n;
|
|
|
414 |
idx++;
|
|
|
415 |
} else
|
|
|
416 |
cal.cam0_errors_per_view[i] = NAN;
|
|
|
417 |
}
|
|
|
418 |
cal.cam1_errors_per_view.resize(nSets);
|
|
|
419 |
idx = 0;
|
|
|
420 |
for(unsigned int i = 0; i < nSets; ++i){
|
|
|
421 |
if(success1[i]){
|
|
|
422 |
int n = (int)Q1[idx].size();
|
|
|
423 |
std::vector<cv::Point2f> qc_proj;
|
|
|
424 |
cv::projectPoints(cv::Mat(Q1[idx]), cam_rvecs1[idx], cam_tvecs1[idx], cal.K1, cal.k1, qc_proj);
|
|
|
425 |
float err = 0;
|
167 |
jakw |
426 |
for(unsigned int j=0; j<qc_proj.size(); j++){
|
148 |
jakw |
427 |
cv::Point2f d = qc1[idx][j] - qc_proj[j];
|
|
|
428 |
err += cv::sqrt(d.x*d.x + d.y*d.y);
|
|
|
429 |
}
|
|
|
430 |
cal.cam1_errors_per_view[i] = (float)err/n;
|
|
|
431 |
idx++;
|
|
|
432 |
} else
|
|
|
433 |
cal.cam1_errors_per_view[i] = NAN;
|
|
|
434 |
}
|
|
|
435 |
|
91 |
jakw |
436 |
// // hand-eye calibration
|
|
|
437 |
// std::vector<cv::Matx33f> Rc(nValidSets - 1); // rotations/translations of the checkerboard in camera 0 reference frame
|
|
|
438 |
// std::vector<cv::Vec3f> Tc(nValidSets - 1);
|
|
|
439 |
// std::vector<cv::Matx33f> Rr(nValidSets - 1); // in rotation stage reference frame
|
|
|
440 |
// std::vector<cv::Vec3f> Tr(nValidSets - 1);
|
|
|
441 |
// for(int i=0; i<nValidSets-1; i++){
|
|
|
442 |
// // relative transformations in camera
|
|
|
443 |
// cv::Mat cRw1, cRw2;
|
|
|
444 |
// cv::Rodrigues(cam_rvecs0[i], cRw1);
|
|
|
445 |
// cv::Rodrigues(cam_rvecs0[i+1], cRw2);
|
|
|
446 |
// cv::Mat cTw1 = cam_tvecs0[i];
|
|
|
447 |
// cv::Mat cTw2 = cam_tvecs0[i+1];
|
|
|
448 |
// cv::Mat w1Rc = cRw1.t();
|
|
|
449 |
// cv::Mat w1Tc = -cRw1.t()*cTw1;
|
|
|
450 |
// Rc[i] = cv::Mat(cRw2*w1Rc);
|
|
|
451 |
// Tc[i] = cv::Mat(cRw2*w1Tc+cTw2);
|
31 |
jakw |
452 |
|
91 |
jakw |
453 |
// // relative transformations in rotation stage
|
|
|
454 |
// // we define the rotation axis to be in origo, pointing in positive y direction
|
|
|
455 |
// float angleRadians = (angles[i+1]-angles[i])/180.0*M_PI;
|
|
|
456 |
// cv::Vec3f rot_rvec(0.0, -angleRadians, 0.0);
|
|
|
457 |
// cv::Mat Rri;
|
|
|
458 |
// cv::Rodrigues(rot_rvec, Rri);
|
|
|
459 |
// Rr[i] = Rri;
|
|
|
460 |
// Tr[i] = 0.0;
|
33 |
jakw |
461 |
|
91 |
jakw |
462 |
//// std::cout << i << std::endl;
|
|
|
463 |
//// std::cout << "cTw1" << cTw1 << std::endl;
|
|
|
464 |
//// std::cout << "cTw2" << cTw2 << std::endl;
|
|
|
465 |
//// std::cout << "w2Rc" << w2Rc << std::endl;
|
|
|
466 |
//// std::cout << "w2Tc" << w2Tc << std::endl;
|
|
|
467 |
|
|
|
468 |
//// std::cout << "w2Rc" << w2Rc << std::endl;
|
|
|
469 |
//// std::cout << "w2Tc" << w2Tc << std::endl;
|
|
|
470 |
|
|
|
471 |
//// cv::Mat Rci;
|
|
|
472 |
//// cv::Rodrigues(Rc[i], Rci);
|
|
|
473 |
//// std::cout << "Rci" << Rci << std::endl;
|
|
|
474 |
//// std::cout << "Tc[i]" << Tc[i] << std::endl;
|
|
|
475 |
|
|
|
476 |
//// std::cout << "rot_rvec" << rot_rvec << std::endl;
|
|
|
477 |
//// std::cout << "Tr[i]" << Tr[i] << std::endl;
|
|
|
478 |
//// std::cout << std::endl;
|
|
|
479 |
// }
|
|
|
480 |
|
|
|
481 |
// // determine the transformation from rotation stage to camera 0
|
|
|
482 |
// cvtools::handEyeCalibrationTsai(Rc, Tc, Rr, Tr, cal.Rr, cal.Tr);
|
|
|
483 |
|
|
|
484 |
// for(int i=0; i<nValidSets-1; i++){
|
81 |
jakw |
485 |
// std::cout << i << std::endl;
|
33 |
jakw |
486 |
|
81 |
jakw |
487 |
// cv::Mat Rci;
|
|
|
488 |
// cv::Rodrigues(Rc[i], Rci);
|
91 |
jakw |
489 |
// std::cout << "Rc[i]" << Rci << std::endl;
|
81 |
jakw |
490 |
// std::cout << "Tc[i]" << Tc[i] << std::endl;
|
|
|
491 |
|
91 |
jakw |
492 |
// cv::Mat Rri;
|
|
|
493 |
// cv::Rodrigues(Rr[i], Rri);
|
|
|
494 |
// std::cout << "Rr[i]" << Rri << std::endl;
|
81 |
jakw |
495 |
// std::cout << "Tr[i]" << Tr[i] << std::endl;
|
91 |
jakw |
496 |
|
|
|
497 |
// cv::Mat Rcr = cv::Mat(cal.Rr)*cv::Mat(Rc[i])*cv::Mat(cal.Rr.t());
|
|
|
498 |
// cv::Rodrigues(Rcr, Rcr);
|
|
|
499 |
// 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);
|
|
|
500 |
// std::cout << "Rcr[i]" << Rcr << std::endl;
|
|
|
501 |
// std::cout << "Tcr[i]" << Tcr << std::endl;
|
81 |
jakw |
502 |
// std::cout << std::endl;
|
91 |
jakw |
503 |
// }
|
81 |
jakw |
504 |
|
|
|
505 |
|
91 |
jakw |
506 |
// Direct rotation axis calibration //
|
|
|
507 |
// full camera matrices
|
|
|
508 |
cv::Matx34f P0 = cv::Matx34f::eye();
|
|
|
509 |
cv::Mat RT1(3, 4, CV_32F);
|
|
|
510 |
cv::Mat(cal.R1).copyTo(RT1(cv::Range(0, 3), cv::Range(0, 3)));
|
|
|
511 |
cv::Mat(cal.T1).copyTo(RT1(cv::Range(0, 3), cv::Range(3, 4)));
|
|
|
512 |
cv::Matx34f P1 = cv::Matx34f(RT1);
|
81 |
jakw |
513 |
|
91 |
jakw |
514 |
// calibration points in camera 0 frame
|
|
|
515 |
std::vector< std::vector<cv::Point3f> > Qcam;
|
33 |
jakw |
516 |
|
167 |
jakw |
517 |
for(unsigned int i=0; i<nValidSets; i++){
|
91 |
jakw |
518 |
std::vector<cv::Point2f> qc0i, qc1i;
|
81 |
jakw |
519 |
|
195 |
jakw |
520 |
cv::undistortPoints(qc0Stereo[i], qc0i, cal.K0, cal.k0);
|
|
|
521 |
cv::undistortPoints(qc1Stereo[i], qc1i, cal.K1, cal.k1);
|
88 |
jakw |
522 |
// qc0i = qc0[i];
|
|
|
523 |
// qc1i = qc1[i];
|
84 |
jakw |
524 |
|
91 |
jakw |
525 |
cv::Mat Qhom, Qcami;
|
|
|
526 |
cv::triangulatePoints(P0, P1, qc0i, qc1i, Qhom);
|
|
|
527 |
cvtools::convertMatFromHomogeneous(Qhom, Qcami);
|
|
|
528 |
std::vector<cv::Point3f> QcamiPoints;
|
|
|
529 |
cvtools::matToPoints3f(Qcami, QcamiPoints);
|
84 |
jakw |
530 |
|
91 |
jakw |
531 |
Qcam.push_back(QcamiPoints);
|
|
|
532 |
}
|
84 |
jakw |
533 |
|
176 |
jakw |
534 |
// cv::Mat QcamMat(Qcam.size(), Qcam[0].size(), CV_32FC3);
|
|
|
535 |
// for(int i=0; i<Qcam.size(); i++)
|
|
|
536 |
// for(int j=0; j<Qcam[0].size(); j++)
|
|
|
537 |
// QcamMat.at<cv::Point3f>(i,j) = Qcam[i][j];
|
|
|
538 |
// cvtools::writeMat(QcamMat, "Qcam.mat", "Qcam");
|
|
|
539 |
|
91 |
jakw |
540 |
cv::Vec3f axis, point;
|
176 |
jakw |
541 |
float rot_axis_error;
|
196 |
jakw |
542 |
rotationAxisCalibration(Qcam, Qi, axis, point, rot_axis_error);
|
84 |
jakw |
543 |
|
91 |
jakw |
544 |
// construct transformation matrix
|
|
|
545 |
cv::Vec3f ex = axis.cross(cv::Vec3f(0,0,1.0));
|
|
|
546 |
ex = cv::normalize(ex);
|
|
|
547 |
cv::Vec3f ez = ex.cross(axis);
|
|
|
548 |
ez = cv::normalize(ez);
|
84 |
jakw |
549 |
|
91 |
jakw |
550 |
cv::Mat RrMat(3, 3, CV_32F);
|
|
|
551 |
cv::Mat(ex).copyTo(RrMat.col(0));
|
|
|
552 |
cv::Mat(axis).copyTo(RrMat.col(1));
|
|
|
553 |
cv::Mat(ez).copyTo(RrMat.col(2));
|
84 |
jakw |
554 |
|
91 |
jakw |
555 |
cal.Rr = cv::Matx33f(RrMat).t();
|
|
|
556 |
cal.Tr = -cv::Matx33f(RrMat).t()*point;
|
176 |
jakw |
557 |
cal.rot_axis_error = rot_axis_error;
|
84 |
jakw |
558 |
|
27 |
jakw |
559 |
// Print to std::cout
|
|
|
560 |
cal.print();
|
|
|
561 |
|
|
|
562 |
// save to (reentrant qsettings object)
|
33 |
jakw |
563 |
settings.setValue("calibration/parameters", QVariant::fromValue(cal));
|
27 |
jakw |
564 |
|
|
|
565 |
emit done();
|
|
|
566 |
|
22 |
jakw |
567 |
}
|