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>
|
225 |
jakw |
7 |
#include <QTextStream>
|
22 |
jakw |
8 |
|
196 |
jakw |
9 |
#include <ceres/ceres.h>
|
|
|
10 |
|
|
|
11 |
// Closed form solution to solve for the rotation axis from sets of 3D point coordinates of flat pattern feature points
|
|
|
12 |
// Algorithm according to Chen et al., Rotation axis calibration of a turntable using constrained global optimization, Optik 2014
|
|
|
13 |
// DTU, 2014, Jakob Wilm
|
225 |
jakw |
14 |
static void rotationAxisEstimation(const std::vector< std::vector<cv::Point3f> > Qcam,
|
|
|
15 |
const std::vector<cv::Point3f> Qobj,
|
|
|
16 |
cv::Vec3f &axis, cv::Vec3f &point){
|
207 |
flgw |
17 |
assert(Qobj.size() == Qcam[0].size());
|
196 |
jakw |
18 |
|
|
|
19 |
// number of frames (points on each arch)
|
|
|
20 |
int l = Qcam.size();
|
|
|
21 |
|
|
|
22 |
// number of points in each frame
|
|
|
23 |
size_t mn = Qobj.size();
|
|
|
24 |
|
|
|
25 |
// construct matrix for axis determination
|
|
|
26 |
cv::Mat M(6, 6, CV_32F, cv::Scalar(0));
|
|
|
27 |
|
|
|
28 |
for(int k=0; k<l; k++){
|
|
|
29 |
for(unsigned int idx=0; idx<mn; idx++){
|
|
|
30 |
|
207 |
flgw |
31 |
// float i = Qobj[idx].x+4;
|
|
|
32 |
// float j = Qobj[idx].y+4;
|
196 |
jakw |
33 |
float i = Qobj[idx].x;
|
|
|
34 |
float j = Qobj[idx].y;
|
|
|
35 |
|
|
|
36 |
float x = Qcam[k][idx].x;
|
|
|
37 |
float y = Qcam[k][idx].y;
|
|
|
38 |
float z = Qcam[k][idx].z;
|
|
|
39 |
|
|
|
40 |
M += (cv::Mat_<float>(6,6) << x*x, x*y, x*z, x, i*x, j*x,
|
207 |
flgw |
41 |
0, y*y, y*z, y, i*y, j*y,
|
|
|
42 |
0, 0, z*z, z, i*z, j*z,
|
|
|
43 |
0, 0, 0, 1, i, j,
|
|
|
44 |
0, 0, 0, 0, i*i, i*j,
|
|
|
45 |
0, 0, 0, 0, 0, j*j);
|
196 |
jakw |
46 |
|
|
|
47 |
}
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
cv::completeSymm(M, false);
|
|
|
51 |
|
|
|
52 |
// solve for axis
|
|
|
53 |
std::vector<float> lambda;
|
|
|
54 |
cv::Mat u;
|
|
|
55 |
cv::eigen(M, lambda, u);
|
|
|
56 |
|
|
|
57 |
float minLambda = std::abs(lambda[0]);
|
|
|
58 |
int idx = 0;
|
|
|
59 |
for(unsigned int i=1; i<lambda.size(); i++){
|
|
|
60 |
if(abs(lambda[i]) < minLambda){
|
|
|
61 |
minLambda = lambda[i];
|
|
|
62 |
idx = i;
|
|
|
63 |
}
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
axis = u.row(idx).colRange(0, 3);
|
|
|
67 |
axis = cv::normalize(axis);
|
|
|
68 |
|
|
|
69 |
float nx = u.at<float>(idx, 0);
|
|
|
70 |
float ny = u.at<float>(idx, 1);
|
|
|
71 |
float nz = u.at<float>(idx, 2);
|
|
|
72 |
//float d = u.at<float>(idx, 3);
|
|
|
73 |
float dh = u.at<float>(idx, 4);
|
|
|
74 |
float dv = u.at<float>(idx, 5);
|
|
|
75 |
|
207 |
flgw |
76 |
// Paper version: c is initially eliminated
|
|
|
77 |
/*cv::Mat A(l*mn, mn+2, CV_32F, cv::Scalar(0.0));
|
|
|
78 |
cv::Mat bb(l*mn, 1, CV_32F);
|
196 |
jakw |
79 |
|
207 |
flgw |
80 |
for(int k=0; k<l; k++){
|
|
|
81 |
for(unsigned int idx=0; idx<mn; idx++){
|
196 |
jakw |
82 |
|
207 |
flgw |
83 |
float i = Qobj[idx].x;
|
|
|
84 |
float j = Qobj[idx].y;
|
196 |
jakw |
85 |
|
207 |
flgw |
86 |
float x = Qcam[k][idx].x;
|
|
|
87 |
float y = Qcam[k][idx].y;
|
|
|
88 |
float z = Qcam[k][idx].z;
|
196 |
jakw |
89 |
|
207 |
flgw |
90 |
float f = x*x + y*y + z*z + (2*x*nx + 2*y*ny + 2*z*nz)*(i*dh + j*dv);
|
196 |
jakw |
91 |
|
207 |
flgw |
92 |
int row = k*mn+idx;
|
|
|
93 |
A.at<float>(row, 0) = 2*x - (2*z*nx)/nz;
|
|
|
94 |
A.at<float>(row, 1) = 2*y - (2*z*ny)/nz;
|
|
|
95 |
A.at<float>(row, idx+2) = 1.0;
|
196 |
jakw |
96 |
|
207 |
flgw |
97 |
bb.at<float>(row, 0) = f + (2*z*d)/nz;
|
|
|
98 |
}
|
|
|
99 |
}
|
196 |
jakw |
100 |
|
207 |
flgw |
101 |
// solve for point
|
|
|
102 |
cv::Mat abe;
|
|
|
103 |
cv::solve(A, bb, abe, cv::DECOMP_SVD);
|
196 |
jakw |
104 |
|
207 |
flgw |
105 |
float a = abe.at<float>(0, 0);
|
|
|
106 |
float b = abe.at<float>(1, 0);
|
|
|
107 |
float c = -(nx*a+ny*b+d)/nz;
|
|
|
108 |
*/
|
196 |
jakw |
109 |
|
|
|
110 |
// Our version: solve simultanously for a,b,c
|
|
|
111 |
cv::Mat A(l*mn, mn+3, CV_32F, cv::Scalar(0.0));
|
|
|
112 |
cv::Mat bb(l*mn, 1, CV_32F);
|
|
|
113 |
|
|
|
114 |
for(int k=0; k<l; k++){
|
|
|
115 |
for(unsigned int idx=0; idx<mn; idx++){
|
|
|
116 |
|
|
|
117 |
float i = Qobj[idx].x;
|
|
|
118 |
float j = Qobj[idx].y;
|
|
|
119 |
|
|
|
120 |
float x = Qcam[k][idx].x;
|
|
|
121 |
float y = Qcam[k][idx].y;
|
|
|
122 |
float z = Qcam[k][idx].z;
|
|
|
123 |
|
|
|
124 |
float f = x*x + y*y + z*z + (2*x*nx + 2*y*ny + 2*z*nz)*(i*dh + j*dv);
|
|
|
125 |
|
|
|
126 |
int row = k*mn+idx;
|
|
|
127 |
A.at<float>(row, 0) = 2*x;
|
|
|
128 |
A.at<float>(row, 1) = 2*y;
|
|
|
129 |
A.at<float>(row, 2) = 2*z;
|
|
|
130 |
A.at<float>(row, idx+3) = 1.0;
|
|
|
131 |
|
|
|
132 |
bb.at<float>(row, 0) = f;
|
|
|
133 |
}
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
// solve for point
|
|
|
137 |
cv::Mat abe;
|
|
|
138 |
cv::solve(A, bb, abe, cv::DECOMP_SVD);
|
|
|
139 |
|
|
|
140 |
float a = abe.at<float>(0, 0);
|
|
|
141 |
float b = abe.at<float>(1, 0);
|
|
|
142 |
float c = abe.at<float>(2, 0);
|
|
|
143 |
|
|
|
144 |
point[0] = a;
|
|
|
145 |
point[1] = b;
|
|
|
146 |
point[2] = c;
|
|
|
147 |
|
225 |
jakw |
148 |
}
|
|
|
149 |
|
|
|
150 |
struct CircleResidual {
|
|
|
151 |
CircleResidual(std::vector<cv::Point3f> _pointsOnArc)
|
|
|
152 |
: pointsOnArc(_pointsOnArc) {}
|
|
|
153 |
|
|
|
154 |
template <typename T>
|
|
|
155 |
bool operator()(const T* point, const T* axis, T* residual) const {
|
|
|
156 |
|
|
|
157 |
T axisSqNorm = axis[0]*axis[0] + axis[1]*axis[1] + axis[2]*axis[2];
|
|
|
158 |
|
|
|
159 |
unsigned int l = pointsOnArc.size();
|
|
|
160 |
std::vector<T> dI(l);
|
|
|
161 |
|
|
|
162 |
// note, this is automatically initialized to 0
|
|
|
163 |
T sum(0.0);
|
|
|
164 |
|
|
|
165 |
for(unsigned int i=0; i<l; i++){
|
|
|
166 |
cv::Point3d p = pointsOnArc[i];
|
|
|
167 |
//T p[3] = {pointsOnArc[i].x, pointsOnArc[i].y, pointsOnArc[i].z};
|
|
|
168 |
|
|
|
169 |
// point to line distance
|
|
|
170 |
T dotProd = (point[0]-p.x)*axis[0] + (point[1]-p.y)*axis[1] + (point[2]-p.z)*axis[2];
|
|
|
171 |
T dIx = point[0] - p.x - (dotProd*axis[0]/axisSqNorm);
|
|
|
172 |
T dIy = point[1] - p.y - (dotProd*axis[1]/axisSqNorm);
|
|
|
173 |
T dIz = point[2] - p.z - (dotProd*axis[2]/axisSqNorm);
|
|
|
174 |
dI[i] = ceres::sqrt(dIx*dIx + dIy*dIy + dIz*dIz);
|
|
|
175 |
|
|
|
176 |
sum += dI[i];
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
T mean = sum / double(l);
|
|
|
180 |
|
|
|
181 |
for(unsigned int i=0; i<l; i++){
|
|
|
182 |
residual[i] = dI[i] - mean;
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
return true;
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
private:
|
|
|
189 |
// Observations for one checkerboard corner.
|
|
|
190 |
const std::vector<cv::Point3f> pointsOnArc;
|
|
|
191 |
};
|
|
|
192 |
|
|
|
193 |
static void rotationAxisOptimization(const std::vector< std::vector<cv::Point3f> > Qcam, const std::vector<cv::Point3f> Qobj, cv::Vec3f &axis, cv::Vec3f &point, float &error){
|
|
|
194 |
|
|
|
195 |
// number of frames (points on each arch)
|
|
|
196 |
size_t l = Qcam.size();
|
|
|
197 |
|
|
|
198 |
// number of points in each frame
|
|
|
199 |
size_t mn = Qobj.size();
|
|
|
200 |
|
|
|
201 |
// read initial guess
|
197 |
jakw |
202 |
double pointArray[] = {point[0], point[1], point[2]};
|
|
|
203 |
double axisArray[] = {axis[0], axis[1], axis[2]};
|
196 |
jakw |
204 |
|
|
|
205 |
ceres::Problem problem;
|
225 |
jakw |
206 |
|
196 |
jakw |
207 |
// loop through saddle points
|
|
|
208 |
for(unsigned int idx=0; idx<mn; idx++){
|
|
|
209 |
std::vector<cv::Point3f> pointsOnArch(l);
|
225 |
jakw |
210 |
for(unsigned int k=0; k<l; k++){
|
196 |
jakw |
211 |
pointsOnArch[k] = Qcam[k][idx];
|
|
|
212 |
}
|
|
|
213 |
ceres::CostFunction* cost_function =
|
207 |
flgw |
214 |
new ceres::AutoDiffCostFunction<CircleResidual, ceres::DYNAMIC, 3, 3>(
|
|
|
215 |
new CircleResidual(pointsOnArch), l);
|
197 |
jakw |
216 |
problem.AddResidualBlock(cost_function, NULL, pointArray, axisArray);
|
196 |
jakw |
217 |
}
|
|
|
218 |
|
|
|
219 |
// Run the solver!
|
|
|
220 |
ceres::Solver::Options options;
|
|
|
221 |
options.linear_solver_type = ceres::DENSE_QR;
|
|
|
222 |
options.minimizer_progress_to_stdout = true;
|
|
|
223 |
ceres::Solver::Summary summary;
|
|
|
224 |
ceres::Solve(options, &problem, &summary);
|
|
|
225 |
|
|
|
226 |
std::cout << summary.BriefReport() << "\n";
|
|
|
227 |
|
197 |
jakw |
228 |
point = cv::Vec3f(pointArray[0], pointArray[1], pointArray[2]);
|
|
|
229 |
axis = cv::Vec3f(axisArray[0], axisArray[1], axisArray[2]);
|
|
|
230 |
axis /= cv::norm(axis);
|
196 |
jakw |
231 |
|
|
|
232 |
|
197 |
jakw |
233 |
// Error estimate (sum of squared differences)
|
196 |
jakw |
234 |
error = 0;
|
|
|
235 |
// loop through saddle points
|
|
|
236 |
for(unsigned int idx=0; idx<mn; idx++){
|
|
|
237 |
|
|
|
238 |
// vector of distances from rotation axis
|
|
|
239 |
std::vector<float> dI(l);
|
|
|
240 |
// loop through angular positions
|
225 |
jakw |
241 |
for(unsigned int k=0; k<l; k++){
|
196 |
jakw |
242 |
cv::Vec3f p = cv::Vec3f(Qcam[k][idx]);
|
|
|
243 |
// point to line distance
|
|
|
244 |
dI[k] = cv::norm((point-p)-(point-p).dot(axis)*axis);
|
|
|
245 |
}
|
|
|
246 |
float sum = std::accumulate(dI.begin(), dI.end(), 0.0);
|
|
|
247 |
float mean = sum / dI.size();
|
198 |
jakw |
248 |
float meanDev = 0;
|
225 |
jakw |
249 |
for(unsigned int k=0; k<l; k++){
|
198 |
jakw |
250 |
meanDev += std::abs(dI[k] - mean);
|
196 |
jakw |
251 |
}
|
198 |
jakw |
252 |
meanDev /= l;
|
|
|
253 |
error += meanDev;
|
196 |
jakw |
254 |
}
|
198 |
jakw |
255 |
error /= mn;
|
196 |
jakw |
256 |
}
|
|
|
257 |
|
225 |
jakw |
258 |
static std::vector<cv::Point3f> generateWorldCoords(const cv::Size checkerCount, const float checkerSize){
|
|
|
259 |
|
|
|
260 |
std::vector<cv::Point3f> Qi;
|
|
|
261 |
for (int h=0; h<checkerCount.height; h++)
|
|
|
262 |
for (int w=0; w<checkerCount.width; w++)
|
|
|
263 |
Qi.push_back(cv::Point3f(checkerSize * w, checkerSize* h, 0.0));
|
|
|
264 |
|
|
|
265 |
return Qi;
|
|
|
266 |
}
|
|
|
267 |
|
|
|
268 |
static bool detectCheckerBoardCorners(const cv::Size & checkerCount,
|
|
|
269 |
const cv::Mat & frame,
|
|
|
270 |
cv::Mat & frameResult,
|
|
|
271 |
std::vector<cv::Point2f> & qc){
|
206 |
flgw |
272 |
// Convert to grayscale
|
|
|
273 |
cv::Mat gray;
|
225 |
jakw |
274 |
if(frame.channels() == 1)
|
|
|
275 |
cv::cvtColor(frame, gray, CV_BayerBG2GRAY);
|
206 |
flgw |
276 |
else
|
225 |
jakw |
277 |
cv::cvtColor(frame, gray, CV_RGB2GRAY);
|
206 |
flgw |
278 |
|
|
|
279 |
// Extract checker corners
|
225 |
jakw |
280 |
bool success = cv::findChessboardCorners(gray, checkerCount, qc, cv::CALIB_CB_ADAPTIVE_THRESH + cv::CALIB_CB_FAST_CHECK);
|
206 |
flgw |
281 |
if(success){
|
225 |
jakw |
282 |
cv::cornerSubPix(gray, qc, cv::Size(6, 6), cv::Size(1, 1), cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 20, 0.0001));
|
206 |
flgw |
283 |
// Draw colored chessboard
|
225 |
jakw |
284 |
if(frame.channels() == 1)
|
|
|
285 |
cv::cvtColor(frame, frameResult, CV_BayerBG2RGB);
|
206 |
flgw |
286 |
else
|
225 |
jakw |
287 |
frameResult = frame.clone();
|
206 |
flgw |
288 |
|
225 |
jakw |
289 |
cvtools::drawChessboardCorners(frameResult, checkerCount, qc, success, 10);
|
228 |
jakw |
290 |
} else {
|
|
|
291 |
qc.clear();
|
206 |
flgw |
292 |
}
|
|
|
293 |
return success;
|
|
|
294 |
}
|
|
|
295 |
|
225 |
jakw |
296 |
void SMCalibrationWorker::checkerboardDetection(SMCalibrationSet calibrationSet){
|
22 |
jakw |
297 |
|
207 |
flgw |
298 |
QSettings settings;
|
225 |
jakw |
299 |
cv::Size checkerCount(cv::Size(settings.value("calibration/patternSizeX", 22).toInt(), settings.value("calibration/patternSizeY", 13).toInt()));
|
22 |
jakw |
300 |
|
225 |
jakw |
301 |
bool success0 = detectCheckerBoardCorners(checkerCount, calibrationSet.frame0, calibrationSet.frame0Result, calibrationSet.qc0);
|
228 |
jakw |
302 |
if(!success0){
|
|
|
303 |
// calibrationSet.qc0.clear();
|
225 |
jakw |
304 |
emit logMessage(QString("Could not detect checkerboard on set %1 camera0").arg(calibrationSet.id));
|
242 |
jakw |
305 |
std::cerr << "Could not detect checkerboard on set " << calibrationSet.id << " camera0" << std::endl;
|
228 |
jakw |
306 |
}
|
22 |
jakw |
307 |
|
225 |
jakw |
308 |
bool success1 = detectCheckerBoardCorners(checkerCount, calibrationSet.frame1, calibrationSet.frame1Result, calibrationSet.qc1);
|
228 |
jakw |
309 |
if(!success1){
|
|
|
310 |
// calibrationSet.qc1.clear();
|
225 |
jakw |
311 |
emit logMessage(QString("Could not detect checkerboard on set %1 camera1").arg(calibrationSet.id));
|
242 |
jakw |
312 |
std::cerr << "Could not detect checkerboard on set " << calibrationSet.id << " camera1" << std::endl;
|
228 |
jakw |
313 |
}
|
25 |
jakw |
314 |
|
225 |
jakw |
315 |
emit newCheckerboardResult(calibrationSet.id, calibrationSet);
|
25 |
jakw |
316 |
|
207 |
flgw |
317 |
}
|
25 |
jakw |
318 |
|
225 |
jakw |
319 |
void SMCalibrationWorker::cameraCalibration(std::vector<SMCalibrationSet> calibrationData){
|
137 |
jakw |
320 |
|
225 |
jakw |
321 |
QSettings settings;
|
|
|
322 |
cv::Size checkerCount(cv::Size(settings.value("calibration/patternSizeX", 22).toInt(), settings.value("calibration/patternSizeY", 13).toInt()));
|
22 |
jakw |
323 |
|
225 |
jakw |
324 |
unsigned int nSets = calibrationData.size();
|
22 |
jakw |
325 |
|
225 |
jakw |
326 |
// 2D Points collected for OpenCV's calibration procedures
|
|
|
327 |
std::vector< std::vector<cv::Point2f> > qc0, qc1, qc0Stereo, qc1Stereo;
|
207 |
flgw |
328 |
|
236 |
jakw |
329 |
for(unsigned int i=0; i<nSets; i++){
|
207 |
flgw |
330 |
|
225 |
jakw |
331 |
if(!calibrationData[i].selected)
|
|
|
332 |
continue;
|
207 |
flgw |
333 |
|
225 |
jakw |
334 |
// Note: avoiding push_back has only minor theoretical value
|
|
|
335 |
if(!calibrationData[i].qc0.empty())
|
|
|
336 |
qc0.push_back(calibrationData[i].qc0);
|
207 |
flgw |
337 |
|
225 |
jakw |
338 |
if(!calibrationData[i].qc1.empty())
|
|
|
339 |
qc1.push_back(calibrationData[i].qc1);
|
207 |
flgw |
340 |
|
225 |
jakw |
341 |
if(!calibrationData[i].qc0.empty() && !calibrationData[i].qc1.empty()){
|
|
|
342 |
qc0Stereo.push_back(calibrationData[i].qc0);
|
|
|
343 |
qc1Stereo.push_back(calibrationData[i].qc1);
|
207 |
flgw |
344 |
}
|
22 |
jakw |
345 |
}
|
|
|
346 |
|
|
|
347 |
// Generate world object coordinates [mm]
|
225 |
jakw |
348 |
std::vector<cv::Point3f> Qi = generateWorldCoords(checkerCount, settings.value("calibration/squareSize", 10.0).toFloat());
|
137 |
jakw |
349 |
|
225 |
jakw |
350 |
std::vector< std::vector<cv::Point3f> > Q0(qc0.size(), Qi), Q1(qc1.size(), Qi), QStereo(qc0Stereo.size(), Qi);
|
22 |
jakw |
351 |
|
225 |
jakw |
352 |
// Calibrate the cameras
|
31 |
jakw |
353 |
SMCalibrationParameters cal;
|
|
|
354 |
cal.frameWidth = calibrationData[0].frame0.cols;
|
|
|
355 |
cal.frameHeight = calibrationData[0].frame0.rows;
|
|
|
356 |
cv::Size frameSize(cal.frameWidth, cal.frameHeight);
|
22 |
jakw |
357 |
|
68 |
jakw |
358 |
// determine only k1, k2 for lens distortion
|
242 |
jakw |
359 |
int flags = cv::CALIB_FIX_ASPECT_RATIO + cv::CALIB_FIX_K2 + cv::CALIB_FIX_K3 + cv::CALIB_ZERO_TANGENT_DIST + cv::CALIB_FIX_PRINCIPAL_POINT;
|
225 |
jakw |
360 |
|
33 |
jakw |
361 |
std::vector<cv::Mat> cam_rvecs0, cam_tvecs0;
|
225 |
jakw |
362 |
|
|
|
363 |
cal.cam0_error = cv::calibrateCamera(Q0, qc0, frameSize, cal.K0, cal.k0, cam_rvecs0, cam_tvecs0, cal.cam0_intrinsic_std, cal.cam0_extrinsic_std, cal.cam0_errors_per_view, flags,
|
134 |
jakw |
364 |
cv::TermCriteria(cv::TermCriteria::COUNT+cv::TermCriteria::EPS, 100, DBL_EPSILON));
|
86 |
jakw |
365 |
|
33 |
jakw |
366 |
std::vector<cv::Mat> cam_rvecs1, cam_tvecs1;
|
225 |
jakw |
367 |
cal.cam1_error = cv::calibrateCamera(Q1, qc1, frameSize, cal.K1, cal.k1, cam_rvecs1, cam_tvecs1, cal.cam1_intrinsic_std, cal.cam1_extrinsic_std, cal.cam1_errors_per_view, flags,
|
134 |
jakw |
368 |
cv::TermCriteria(cv::TermCriteria::COUNT+cv::TermCriteria::EPS, 100, DBL_EPSILON));
|
225 |
jakw |
369 |
|
|
|
370 |
// Stereo calibration
|
136 |
jakw |
371 |
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 |
372 |
cv::Mat E, F, R1, T1;
|
22 |
jakw |
373 |
|
225 |
jakw |
374 |
#if CV_MAJOR_VERSION < 3
|
207 |
flgw |
375 |
cal.stereo_error = cv::stereoCalibrate(QStereo, qc0Stereo, qc1Stereo, cal.K0, cal.k0, cal.K1, cal.k1,
|
|
|
376 |
frameSize, R1, T1, E, F,
|
|
|
377 |
cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 200, DBL_EPSILON),
|
|
|
378 |
flags_stereo);
|
225 |
jakw |
379 |
#else
|
207 |
flgw |
380 |
cal.stereo_error = cv::stereoCalibrate(QStereo, qc0Stereo, qc1Stereo, cal.K0, cal.k0, cal.K1, cal.k1,
|
225 |
jakw |
381 |
frameSize, R1, T1, E, F, flags_stereo,
|
207 |
flgw |
382 |
cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 200, DBL_EPSILON));
|
225 |
jakw |
383 |
#endif
|
207 |
flgw |
384 |
|
33 |
jakw |
385 |
cal.R1 = R1;
|
|
|
386 |
cal.T1 = T1;
|
|
|
387 |
cal.E = E;
|
|
|
388 |
cal.F = F;
|
|
|
389 |
|
225 |
jakw |
390 |
// Print to log
|
|
|
391 |
std::stringstream out;
|
|
|
392 |
out << "## Camera Calibration ##" << std::endl
|
|
|
393 |
<< "No. images used for intrinsics of cam0: " << qc0.size() << std::endl
|
|
|
394 |
<< "No. images used for intrinsics of cam1: " << qc1.size() << std::endl
|
|
|
395 |
<< "No. images used for stereo calibration: " << qc0Stereo.size() << std::endl;
|
|
|
396 |
cal.printCamera(out);
|
|
|
397 |
out << std::endl << std::endl;
|
|
|
398 |
emit logMessage(QString::fromStdString(out.str()));
|
148 |
jakw |
399 |
|
225 |
jakw |
400 |
// save to (reentrant) qsettings object
|
|
|
401 |
settings.setValue("calibration/parameters", QVariant::fromValue(cal));
|
31 |
jakw |
402 |
|
225 |
jakw |
403 |
emit done();
|
|
|
404 |
}
|
33 |
jakw |
405 |
|
91 |
jakw |
406 |
|
225 |
jakw |
407 |
void SMCalibrationWorker::rotationStageCalibration(std::vector<SMCalibrationSet> calibrationData){
|
91 |
jakw |
408 |
|
225 |
jakw |
409 |
int nSets = calibrationData.size();
|
91 |
jakw |
410 |
|
225 |
jakw |
411 |
std::vector< std::vector<cv::Point2f> > qc0Stereo, qc1Stereo;
|
|
|
412 |
for(int i=0; i<nSets; i++){
|
91 |
jakw |
413 |
|
225 |
jakw |
414 |
if(!calibrationData[i].selected)
|
|
|
415 |
continue;
|
91 |
jakw |
416 |
|
225 |
jakw |
417 |
// Note: avoiding push_back has only minor theoretical value
|
|
|
418 |
if(!calibrationData[i].qc0.empty() && !calibrationData[i].qc1.empty()){
|
|
|
419 |
qc0Stereo.push_back(calibrationData[i].qc0);
|
|
|
420 |
qc1Stereo.push_back(calibrationData[i].qc1);
|
|
|
421 |
}
|
|
|
422 |
}
|
33 |
jakw |
423 |
|
225 |
jakw |
424 |
QSettings settings;
|
|
|
425 |
SMCalibrationParameters cal = settings.value("calibration/parameters").value<SMCalibrationParameters>();
|
81 |
jakw |
426 |
|
225 |
jakw |
427 |
if(qc0Stereo.size() > 2){
|
207 |
flgw |
428 |
// Generate world object coordinates [mm]
|
225 |
jakw |
429 |
const cv::Size checkerCount(cv::Size(settings.value("calibration/patternSizeX", 22).toInt(),settings.value("calibration/patternSizeY", 13).toInt()));
|
|
|
430 |
const float checkerSize = settings.value("calibration/squareSize", 10.0).toFloat();
|
|
|
431 |
std::vector<cv::Point3f> Qi = generateWorldCoords(checkerCount, checkerSize);
|
91 |
jakw |
432 |
|
207 |
flgw |
433 |
// Direct rotation axis calibration //
|
|
|
434 |
// full camera matrices
|
|
|
435 |
cv::Matx34f P0 = cv::Matx34f::eye();
|
|
|
436 |
cv::Mat RT1(3, 4, CV_32F);
|
|
|
437 |
cv::Mat(cal.R1).copyTo(RT1(cv::Range(0, 3), cv::Range(0, 3)));
|
|
|
438 |
cv::Mat(cal.T1).copyTo(RT1(cv::Range(0, 3), cv::Range(3, 4)));
|
|
|
439 |
cv::Matx34f P1 = cv::Matx34f(RT1);
|
81 |
jakw |
440 |
|
207 |
flgw |
441 |
// calibration points in camera 0 frame
|
|
|
442 |
std::vector< std::vector<cv::Point3f> > Qcam(qc0Stereo.size());
|
225 |
jakw |
443 |
|
|
|
444 |
#pragma omp parallel for
|
207 |
flgw |
445 |
for(unsigned int i=0; i<qc0Stereo.size(); i++){
|
|
|
446 |
std::vector<cv::Point2f> qc0i, qc1i;
|
81 |
jakw |
447 |
|
207 |
flgw |
448 |
cv::undistortPoints(qc0Stereo[i], qc0i, cal.K0, cal.k0);
|
|
|
449 |
cv::undistortPoints(qc1Stereo[i], qc1i, cal.K1, cal.k1);
|
81 |
jakw |
450 |
|
207 |
flgw |
451 |
cv::Mat Qhom, Qcami;
|
|
|
452 |
cv::triangulatePoints(P0, P1, qc0i, qc1i, Qhom);
|
|
|
453 |
cvtools::convertMatFromHomogeneous(Qhom, Qcami);
|
|
|
454 |
std::vector<cv::Point3f> QcamiPoints;
|
|
|
455 |
cvtools::matToPoints3f(Qcami, QcamiPoints);
|
33 |
jakw |
456 |
|
207 |
flgw |
457 |
Qcam[i] = QcamiPoints;
|
|
|
458 |
}
|
81 |
jakw |
459 |
|
207 |
flgw |
460 |
cv::Vec3f axis, point;
|
|
|
461 |
float rot_axis_error;
|
225 |
jakw |
462 |
rotationAxisEstimation(Qcam, Qi, axis, point);
|
|
|
463 |
rotationAxisOptimization(Qcam, Qi, axis, point, rot_axis_error);
|
84 |
jakw |
464 |
|
207 |
flgw |
465 |
// construct transformation matrix
|
|
|
466 |
cv::Vec3f ex = axis.cross(cv::Vec3f(0,0,1.0));
|
|
|
467 |
ex = cv::normalize(ex);
|
|
|
468 |
cv::Vec3f ez = ex.cross(axis);
|
|
|
469 |
ez = cv::normalize(ez);
|
84 |
jakw |
470 |
|
207 |
flgw |
471 |
cv::Mat RrMat(3, 3, CV_32F);
|
|
|
472 |
cv::Mat(ex).copyTo(RrMat.col(0));
|
|
|
473 |
cv::Mat(axis).copyTo(RrMat.col(1));
|
|
|
474 |
cv::Mat(ez).copyTo(RrMat.col(2));
|
|
|
475 |
|
|
|
476 |
cal.Rr = cv::Matx33f(RrMat).t();
|
|
|
477 |
cal.Tr = -cv::Matx33f(RrMat).t()*point;
|
|
|
478 |
cal.rot_axis_error = rot_axis_error;
|
225 |
jakw |
479 |
} else {
|
207 |
flgw |
480 |
cal.Rr = cv::Matx33f::eye();
|
|
|
481 |
cal.Tr = cv::Vec3f(0,0,0);
|
|
|
482 |
cal.rot_axis_error = -1;
|
225 |
jakw |
483 |
return;
|
91 |
jakw |
484 |
}
|
225 |
jakw |
485 |
|
|
|
486 |
// Print to log
|
|
|
487 |
std::stringstream out;
|
|
|
488 |
out << "## Rotation Stage Calibration ##" << std::endl
|
|
|
489 |
<< "No. images used for calibration: " << qc0Stereo.size() << std::endl;
|
|
|
490 |
cal.printRotationStage(out);
|
|
|
491 |
out << std::endl << std::endl;
|
|
|
492 |
emit logMessage(QString::fromStdString(out.str()));
|
|
|
493 |
|
|
|
494 |
// Save to (reentrant) qsettings object
|
|
|
495 |
settings.setValue("calibration/parameters", QVariant::fromValue(cal));
|
|
|
496 |
|
|
|
497 |
emit done();
|
22 |
jakw |
498 |
}
|