forked from edennz/artoolkit6-calibration
-
Notifications
You must be signed in to change notification settings - Fork 3
/
calc.cpp
395 lines (351 loc) · 14.3 KB
/
calc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
* calc.cpp
* artoolkitX
*
* This file is part of artoolkitX.
*
* artoolkitX is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* artoolkitX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with artoolkitX. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the copyright holders of this library give you
* permission to link this library with independent modules to produce an
* executable, regardless of the license terms of these independent modules, and to
* copy and distribute the resulting executable under terms of your choice,
* provided that you also meet, for each linked independent module, the terms and
* conditions of the license of that module. An independent module is a module
* which is neither derived from nor based on this library. If you modify this
* library, you may extend this exception to your version of the library, but you
* are not obligated to do so. If you do not wish to do so, delete this exception
* statement from your version.
*
* Copyright 2018 Realmax, Inc.
* Copyright 2015-2017 Daqri LLC. All Rights Reserved.
* Copyright 2012-2015 ARToolworks, Inc. All Rights Reserved.
*
* Author(s): Philip Lamb, Hirokazu Kato
*
*/
#include "calc.hpp"
#include <opencv2/calib3d.hpp>
static void convParam(const float intr[3][4], const float dist[AR_DIST_FACTOR_NUM_MAX], const int xsize, const int ysize, const int dist_function_version, ARParam *param);
static ARdouble getSizeFactor(ARdouble const dist_factor[AR_DIST_FACTOR_NUM_MAX], const int xsize, const int ysize, const int dist_function_version);
static void calcChessboardCorners(const Calibration::CalibrationPatternType patternType, cv::Size patternSize, float patternSpacing, std::vector<cv::Point3f>& corners)
{
corners.resize(0);
switch (patternType) {
case Calibration::CalibrationPatternType::CHESSBOARD:
case Calibration::CalibrationPatternType::CIRCLES_GRID:
for (int j = 0; j < patternSize.height; j++)
for (int i = 0; i < patternSize.width; i++)
corners.push_back(cv::Point3f(float(i*patternSpacing), float(j*patternSpacing), 0));
break;
case Calibration::CalibrationPatternType::ASYMMETRIC_CIRCLES_GRID:
for (int j = 0; j < patternSize.height; j++)
for (int i = 0; i < patternSize.width; i++)
corners.push_back(cv::Point3f(float((2*i + j % 2)*patternSpacing), float(j*patternSpacing), 0));
break;
default:
ARLOGe("Unknown pattern type.\n");
}
}
void calc(const int capturedImageNum,
const Calibration::CalibrationPatternType patternType,
const cv::Size patternSize,
const float patternSpacing,
const std::vector<std::vector<cv::Point2f> >& cornerSet,
const int width,
const int height,
const int dist_function_version,
ARParam *param_out,
ARdouble *err_min_out,
ARdouble *err_avg_out,
ARdouble *err_max_out)
{
if (dist_function_version != 5 && dist_function_version != 4) {
ARLOGe("Unsupported distortion function version %d.\n", dist_function_version);
return;
}
// Set version.
int flags = 0;
cv::Mat distortionCoeff;
if (dist_function_version == 5) {
distortionCoeff = cv::Mat::zeros(12, 1, CV_64F);
flags |= cv::CALIB_RATIONAL_MODEL|cv::CALIB_THIN_PRISM_MODEL;
} else /* dist_function_version == 4 */ {
distortionCoeff = cv::Mat::zeros(5, 1, CV_64F);
flags |= cv::CALIB_FIX_K3;
}
double aspectRatio = 1.0;
// Set up object points.
std::vector<std::vector<cv::Point3f> > objectPoints(1);
calcChessboardCorners(patternType, patternSize, patternSpacing, objectPoints[0]);
objectPoints.resize(capturedImageNum, objectPoints[0]);
cv::Mat intrinsics = cv::Mat::eye(3, 3, CV_64F);
if (flags & cv::CALIB_FIX_ASPECT_RATIO)
intrinsics.at<double>(0,0) = aspectRatio;
std::vector<cv::Mat> rotationVectors;
std::vector<cv::Mat> translationVectors;
double rms = calibrateCamera(objectPoints, cornerSet, cv::Size(width, height), intrinsics,
distortionCoeff, rotationVectors, translationVectors, flags);
ARLOGi("RMS error reported by calibrateCamera: %g\n", rms);
bool ok = checkRange(intrinsics) && checkRange(distortionCoeff);
if (!ok) ARLOGe("cv::checkRange(intrinsics) && cv::checkRange(distortionCoeff) reported not OK.\n");
float intr[3][4];
float dist[AR_DIST_FACTOR_NUM_MAX];
ARParam param;
int i, j, k;
for (j = 0; j < 3; j++) {
for (i = 0; i < 3; i++) {
intr[j][i] = (float)intrinsics.at<double>(j, i);
}
intr[j][3] = 0.0f;
}
if (dist_function_version == 5) {
for (i = 0; i < 12; i++) dist[i] = (float)distortionCoeff.at<double>(i);
} else /* dist_function_version == 4 */ {
for (i = 0; i < 4; i++) dist[i] = (float)distortionCoeff.at<double>(i);
}
convParam(intr, dist, width, height, dist_function_version, ¶m);
arParamDisp(¶m);
cv::Mat rotationVector;
cv::Mat rotationMatrix;
double trans[3][4];
ARdouble cx, cy, cz, hx, hy, h, sx, sy, ox, oy, err;
ARdouble err_min = 1000000.0f, err_avg = 0.0f, err_max = 0.0f;
rotationVector = cv::Mat(1, 3, CV_32FC1);
rotationMatrix = cv::Mat(3, 3, CV_32FC1);
for (k = 0; k < capturedImageNum; k++) {
for (i = 0; i < 3; i++) {
((float *)(rotationVector.data))[i] = (float)rotationVectors.at(k).at<double>(i);
}
cv::Rodrigues(rotationVector, rotationMatrix);
for (j = 0; j < 3; j++) {
for (i = 0; i < 3; i++) {
trans[j][i] = rotationMatrix.at<float>(j, i);
}
trans[j][3] = (float)translationVectors.at(k).at<double>(j);
}
//arParamDispExt(trans);
err = 0.0;
for (i = 0; i < patternSize.width; i++) {
for (j = 0; j < patternSize.height; j++) {
float x = objectPoints[0][i * patternSize.height + j].x;
float y = objectPoints[0][i * patternSize.height + j].y;
cx = trans[0][0] * x + trans[0][1] * y + trans[0][3];
cy = trans[1][0] * x + trans[1][1] * y + trans[1][3];
cz = trans[2][0] * x + trans[2][1] * y + trans[2][3];
hx = param.mat[0][0] * cx + param.mat[0][1] * cy + param.mat[0][2] * cz + param.mat[0][3];
hy = param.mat[1][0] * cx + param.mat[1][1] * cy + param.mat[1][2] * cz + param.mat[1][3];
h = param.mat[2][0] * cx + param.mat[2][1] * cy + param.mat[2][2] * cz + param.mat[2][3];
if (h == 0.0) continue;
sx = hx / h;
sy = hy / h;
arParamIdeal2Observ(param.dist_factor, sx, sy, &ox, &oy, param.dist_function_version);
sx = (ARdouble)cornerSet[k][i * patternSize.height + j].x;
sy = (ARdouble)cornerSet[k][i * patternSize.height + j].y;
err += (ox - sx)*(ox - sx) + (oy - sy)*(oy - sy);
}
}
err = sqrtf(err/(patternSize.width*patternSize.height));
ARPRINT("Err[%2d]: %f[pixel]\n", k + 1, err);
// Track min, avg, and max error.
if (err < err_min) err_min = err;
err_avg += err;
if (err > err_max) err_max = err;
}
err_avg /= (ARdouble)capturedImageNum;
*err_min_out = err_min;
*err_avg_out = err_avg;
*err_max_out = err_max;
*param_out = param;
}
static void convParam(const float intr[3][4], const float dist[AR_DIST_FACTOR_NUM_MAX], const int xsize, const int ysize, const int dist_function_version, ARParam *param)
{
double s;
int i, j;
if (dist_function_version != 5 && dist_function_version != 4) {
ARLOGe("Unsupported distortion function version %d.\n", dist_function_version);
return;
}
param->dist_function_version = dist_function_version;
param->xsize = xsize;
param->ysize = ysize;
param->dist_factor[0] = (ARdouble)dist[0]; /* k1 */
param->dist_factor[1] = (ARdouble)dist[1]; /* k2 */
param->dist_factor[2] = (ARdouble)dist[2]; /* p1 */
param->dist_factor[3] = (ARdouble)dist[3]; /* p2 */
if (dist_function_version == 5) {
param->dist_factor[4] = (ARdouble)dist[4]; /* k3 */
param->dist_factor[5] = (ARdouble)dist[5]; /* k4 */
param->dist_factor[6] = (ARdouble)dist[6]; /* k5 */
param->dist_factor[7] = (ARdouble)dist[7]; /* k6 */
param->dist_factor[8] = (ARdouble)dist[8]; /* s1 */
param->dist_factor[9] = (ARdouble)dist[9]; /* s2 */
param->dist_factor[10] = (ARdouble)dist[10]; /* s3 */
param->dist_factor[11] = (ARdouble)dist[11]; /* s4 */
param->dist_factor[12] = (ARdouble)intr[0][0]; /* fx */
param->dist_factor[13] = (ARdouble)intr[1][1]; /* fy */
param->dist_factor[14] = (ARdouble)intr[0][2]; /* cx */
param->dist_factor[15] = (ARdouble)intr[1][2]; /* cy */
param->dist_factor[16] = (ARdouble)1.0; /* s */
} else /* dist_function_version == 4 */ {
param->dist_factor[4] = (ARdouble)intr[0][0]; /* fx */
param->dist_factor[5] = (ARdouble)intr[1][1]; /* fy */
param->dist_factor[6] = (ARdouble)intr[0][2]; /* cx */
param->dist_factor[7] = (ARdouble)intr[1][2]; /* cy */
param->dist_factor[8] = (ARdouble)1.0; /* s */
}
for (j = 0; j < 3; j++) {
for (i = 0; i < 4; i++) {
param->mat[j][i] = (ARdouble)intr[j][i];
}
}
s = getSizeFactor(param->dist_factor, xsize, ysize, param->dist_function_version);
param->mat[0][0] /= s;
param->mat[0][1] /= s;
param->mat[1][0] /= s;
param->mat[1][1] /= s;
if (dist_function_version == 5) {
param->dist_factor[16] = s;
} else /* dist_function_version == 4 */ {
param->dist_factor[8] = s;
}
}
static ARdouble getSizeFactor(const ARdouble dist_factor[AR_DIST_FACTOR_NUM_MAX], const int xsize, const int ysize, const int dist_function_version)
{
ARdouble ox, oy, ix, iy;
ARdouble olen, ilen;
ARdouble sf, sf1;
ARdouble cx, cy;
if (dist_function_version == 5) {
cx = dist_factor[14];
cy = dist_factor[15];
} else if (dist_function_version == 4) {
cx = dist_factor[6];
cy = dist_factor[7];
} else {
ARLOGe("Unsupported distortion function version %d.\n", dist_function_version);
return 1.0;
}
sf = 100.0f;
ox = 0.0f;
oy = cy;
olen = cx;
arParamObserv2Ideal(dist_factor, ox, oy, &ix, &iy, dist_function_version);
ilen = cx - ix;
//ARPRINT("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
if (ilen > 0.0f) {
sf1 = ilen / olen;
if (sf1 < sf) sf = sf1;
}
ox = xsize;
oy = cy;
olen = xsize - cx;
arParamObserv2Ideal(dist_factor, ox, oy, &ix, &iy, dist_function_version);
ilen = ix - cx;
//ARPRINT("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
if (ilen > 0.0f) {
sf1 = ilen / olen;
if (sf1 < sf) sf = sf1;
}
ox = cx;
oy = 0.0;
olen = cy;
arParamObserv2Ideal(dist_factor, ox, oy, &ix, &iy, dist_function_version);
ilen = cy - iy;
//ARPRINT("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
if (ilen > 0.0f) {
sf1 = ilen / olen;
if (sf1 < sf) sf = sf1;
}
ox = cx;
oy = ysize;
olen = ysize - cy;
arParamObserv2Ideal(dist_factor, ox, oy, &ix, &iy, dist_function_version);
ilen = iy - cy;
//ARPRINT("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
if (ilen > 0.0f) {
sf1 = ilen / olen;
if (sf1 < sf) sf = sf1;
}
ox = 0.0f;
oy = 0.0f;
arParamObserv2Ideal(dist_factor, ox, oy, &ix, &iy, dist_function_version);
ilen = cx - ix;
olen = cx;
//ARPRINT("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
if (ilen > 0.0f) {
sf1 = ilen / olen;
if (sf1 < sf) sf = sf1;
}
ilen = cy - iy;
olen = cy;
//ARPRINT("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
if (ilen > 0.0f) {
sf1 = ilen / olen;
if (sf1 < sf) sf = sf1;
}
ox = xsize;
oy = 0.0f;
arParamObserv2Ideal(dist_factor, ox, oy, &ix, &iy, dist_function_version);
ilen = ix - cx;
olen = xsize - cx;
//ARPRINT("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
if (ilen > 0.0f) {
sf1 = ilen / olen;
if (sf1 < sf) sf = sf1;
}
ilen = cy - iy;
olen = cy;
//ARPRINT("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
if (ilen > 0.0f) {
sf1 = ilen / olen;
if (sf1 < sf) sf = sf1;
}
ox = 0.0f;
oy = ysize;
arParamObserv2Ideal(dist_factor, ox, oy, &ix, &iy, dist_function_version);
ilen = cx - ix;
olen = cx;
//ARPRINT("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
if (ilen > 0.0f) {
sf1 = ilen / olen;
if (sf1 < sf) sf = sf1;
}
ilen = iy - cy;
olen = ysize - cy;
//ARPRINT("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
if (ilen > 0.0f) {
sf1 = ilen / olen;
if (sf1 < sf) sf = sf1;
}
ox = xsize;
oy = ysize;
arParamObserv2Ideal(dist_factor, ox, oy, &ix, &iy, dist_function_version);
ilen = ix - cx;
olen = xsize - cx;
//ARPRINT("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
if (ilen > 0.0f) {
sf1 = ilen / olen;
if (sf1 < sf) sf = sf1;
}
ilen = iy - cy;
olen = ysize - cy;
//ARPRINT("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
if (ilen > 0.0f) {
sf1 = ilen / olen;
if (sf1 < sf) sf = sf1;
}
if (sf == 100.0f) sf = 1.0f;
return sf;
}