Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/calib3d/test/test_cameracalibration_artificial.cpp
16337 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15
// Third party copyrights are property of their respective owners.
16
//
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
19
//
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
22
//
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
26
//
27
// * The name of the copyright holders may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
29
//
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
40
//
41
//M*/
42
43
#include "test_precomp.hpp"
44
#include "test_chessboardgenerator.hpp"
45
46
namespace opencv_test { namespace {
47
48
//template<class T> ostream& operator<<(ostream& out, const Mat_<T>& mat)
49
//{
50
// for(Mat_<T>::const_iterator pos = mat.begin(), end = mat.end(); pos != end; ++pos)
51
// out << *pos << " ";
52
// return out;
53
//}
54
//ostream& operator<<(ostream& out, const Mat& mat) { return out << Mat_<double>(mat); }
55
56
Mat calcRvec(const vector<Point3f>& points, const Size& cornerSize)
57
{
58
Point3f p00 = points[0];
59
Point3f p10 = points[1];
60
Point3f p01 = points[cornerSize.width];
61
62
Vec3d ex(p10.x - p00.x, p10.y - p00.y, p10.z - p00.z);
63
Vec3d ey(p01.x - p00.x, p01.y - p00.y, p01.z - p00.z);
64
Vec3d ez = ex.cross(ey);
65
66
Mat rot(3, 3, CV_64F);
67
*rot.ptr<Vec3d>(0) = ex;
68
*rot.ptr<Vec3d>(1) = ey;
69
*rot.ptr<Vec3d>(2) = ez * (1.0/cv::norm(ez)); // TODO cvtest
70
71
Mat res;
72
cvtest::Rodrigues(rot.t(), res);
73
return res.reshape(1, 1);
74
}
75
76
class CV_CalibrateCameraArtificialTest : public cvtest::BaseTest
77
{
78
public:
79
CV_CalibrateCameraArtificialTest() :
80
r(0)
81
{
82
}
83
~CV_CalibrateCameraArtificialTest() {}
84
protected:
85
int r;
86
87
const static int JUST_FIND_CORNERS = 0;
88
const static int USE_CORNERS_SUBPIX = 1;
89
const static int USE_4QUAD_CORNERS = 2;
90
const static int ARTIFICIAL_CORNERS = 4;
91
92
93
bool checkErr(double a, double a0, double eps, double delta)
94
{
95
return fabs(a - a0) > eps * (fabs(a0) + delta);
96
}
97
98
void compareCameraMatrs(const Mat_<double>& camMat, const Mat& camMat_est)
99
{
100
if ( camMat_est.at<double>(0, 1) != 0 || camMat_est.at<double>(1, 0) != 0 ||
101
camMat_est.at<double>(2, 0) != 0 || camMat_est.at<double>(2, 1) != 0 ||
102
camMat_est.at<double>(2, 2) != 1)
103
{
104
ts->printf( cvtest::TS::LOG, "Bad shape of camera matrix returned \n");
105
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
106
}
107
108
double fx_e = camMat_est.at<double>(0, 0), fy_e = camMat_est.at<double>(1, 1);
109
double cx_e = camMat_est.at<double>(0, 2), cy_e = camMat_est.at<double>(1, 2);
110
111
double fx = camMat(0, 0), fy = camMat(1, 1), cx = camMat(0, 2), cy = camMat(1, 2);
112
113
const double eps = 1e-2;
114
const double dlt = 1e-5;
115
116
bool fail = checkErr(fx_e, fx, eps, dlt) || checkErr(fy_e, fy, eps, dlt) ||
117
checkErr(cx_e, cx, eps, dlt) || checkErr(cy_e, cy, eps, dlt);
118
119
if (fail)
120
{
121
ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
122
}
123
ts->printf( cvtest::TS::LOG, "%d) Expected [Fx Fy Cx Cy] = [%.3f %.3f %.3f %.3f]\n", r, fx, fy, cx, cy);
124
ts->printf( cvtest::TS::LOG, "%d) Estimated [Fx Fy Cx Cy] = [%.3f %.3f %.3f %.3f]\n", r, fx_e, fy_e, cx_e, cy_e);
125
}
126
127
void compareDistCoeffs(const Mat_<double>& distCoeffs, const Mat& distCoeffs_est)
128
{
129
const double *dt_e = distCoeffs_est.ptr<double>();
130
131
double k1_e = dt_e[0], k2_e = dt_e[1], k3_e = dt_e[4];
132
double p1_e = dt_e[2], p2_e = dt_e[3];
133
134
double k1 = distCoeffs(0, 0), k2 = distCoeffs(0, 1), k3 = distCoeffs(0, 4);
135
double p1 = distCoeffs(0, 2), p2 = distCoeffs(0, 3);
136
137
const double eps = 5e-2;
138
const double dlt = 1e-3;
139
140
const double eps_k3 = 5;
141
const double dlt_k3 = 1e-3;
142
143
bool fail = checkErr(k1_e, k1, eps, dlt) || checkErr(k2_e, k2, eps, dlt) || checkErr(k3_e, k3, eps_k3, dlt_k3) ||
144
checkErr(p1_e, p1, eps, dlt) || checkErr(p2_e, p2, eps, dlt);
145
146
if (fail)
147
{
148
// commented according to vp123's recomendation. TODO - improve accuaracy
149
//ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY); ss
150
}
151
ts->printf( cvtest::TS::LOG, "%d) DistCoeff exp=(%.2f, %.2f, %.4f, %.4f %.2f)\n", r, k1, k2, p1, p2, k3);
152
ts->printf( cvtest::TS::LOG, "%d) DistCoeff est=(%.2f, %.2f, %.4f, %.4f %.2f)\n", r, k1_e, k2_e, p1_e, p2_e, k3_e);
153
ts->printf( cvtest::TS::LOG, "%d) AbsError = [%.5f %.5f %.5f %.5f %.5f]\n", r, fabs(k1-k1_e), fabs(k2-k2_e), fabs(p1-p1_e), fabs(p2-p2_e), fabs(k3-k3_e));
154
}
155
156
void compareShiftVecs(const vector<Mat>& tvecs, const vector<Mat>& tvecs_est)
157
{
158
const double eps = 1e-2;
159
const double dlt = 1e-4;
160
161
int err_count = 0;
162
const int errMsgNum = 4;
163
for(size_t i = 0; i < tvecs.size(); ++i)
164
{
165
const Point3d& tvec = *tvecs[i].ptr<Point3d>();
166
const Point3d& tvec_est = *tvecs_est[i].ptr<Point3d>();
167
168
double n1 = cv::norm(tvec_est - tvec); // TODO cvtest
169
double n2 = cv::norm(tvec); // TODO cvtest
170
if (n1 > eps* (n2 + dlt))
171
{
172
if (err_count++ < errMsgNum)
173
{
174
if (err_count == errMsgNum)
175
ts->printf( cvtest::TS::LOG, "%d) ...\n", r);
176
else
177
{
178
ts->printf( cvtest::TS::LOG, "%d) Bad accuracy in returned tvecs. Index = %d\n", r, i);
179
ts->printf( cvtest::TS::LOG, "%d) norm(tvec_est - tvec) = %f, norm(tvec_exp) = %f \n", r, n1, n2);
180
}
181
}
182
ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
183
}
184
}
185
}
186
187
void compareRotationVecs(const vector<Mat>& rvecs, const vector<Mat>& rvecs_est)
188
{
189
const double eps = 2e-2;
190
const double dlt = 1e-4;
191
192
Mat rmat, rmat_est;
193
int err_count = 0;
194
const int errMsgNum = 4;
195
for(size_t i = 0; i < rvecs.size(); ++i)
196
{
197
cvtest::Rodrigues(rvecs[i], rmat);
198
cvtest::Rodrigues(rvecs_est[i], rmat_est);
199
200
if (cvtest::norm(rmat_est, rmat, NORM_L2) > eps* (cvtest::norm(rmat, NORM_L2) + dlt))
201
{
202
if (err_count++ < errMsgNum)
203
{
204
if (err_count == errMsgNum)
205
ts->printf( cvtest::TS::LOG, "%d) ...\n", r);
206
else
207
{
208
ts->printf( cvtest::TS::LOG, "%d) Bad accuracy in returned rvecs (rotation matrs). Index = %d\n", r, i);
209
ts->printf( cvtest::TS::LOG, "%d) norm(rot_mat_est - rot_mat_exp) = %f, norm(rot_mat_exp) = %f \n", r,
210
cvtest::norm(rmat_est, rmat, NORM_L2), cvtest::norm(rmat, NORM_L2));
211
212
}
213
}
214
ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
215
}
216
}
217
}
218
219
double reprojectErrorWithoutIntrinsics(const vector<Point3f>& cb3d, const vector<Mat>& _rvecs_exp, const vector<Mat>& _tvecs_exp,
220
const vector<Mat>& rvecs_est, const vector<Mat>& tvecs_est)
221
{
222
const static Mat eye33 = Mat::eye(3, 3, CV_64F);
223
const static Mat zero15 = Mat::zeros(1, 5, CV_64F);
224
Mat _chessboard3D(cb3d);
225
vector<Point2f> uv_exp, uv_est;
226
double res = 0;
227
228
for(size_t i = 0; i < rvecs_exp.size(); ++i)
229
{
230
projectPoints(_chessboard3D, _rvecs_exp[i], _tvecs_exp[i], eye33, zero15, uv_exp);
231
projectPoints(_chessboard3D, rvecs_est[i], tvecs_est[i], eye33, zero15, uv_est);
232
for(size_t j = 0; j < cb3d.size(); ++j)
233
res += cv::norm(uv_exp[i] - uv_est[i]); // TODO cvtest
234
}
235
return res;
236
}
237
238
Size2f sqSile;
239
240
vector<Point3f> chessboard3D;
241
vector<Mat> boards, rvecs_exp, tvecs_exp, rvecs_spnp, tvecs_spnp;
242
vector< vector<Point3f> > objectPoints;
243
vector< vector<Point2f> > imagePoints_art;
244
vector< vector<Point2f> > imagePoints_findCb;
245
246
247
void prepareForTest(const Mat& bg, const Mat& camMat, const Mat& distCoeffs, size_t brdsNum, const ChessBoardGenerator& cbg)
248
{
249
sqSile = Size2f(1.f, 1.f);
250
Size cornersSize = cbg.cornersSize();
251
252
chessboard3D.clear();
253
for(int j = 0; j < cornersSize.height; ++j)
254
for(int i = 0; i < cornersSize.width; ++i)
255
chessboard3D.push_back(Point3f(sqSile.width * i, sqSile.height * j, 0));
256
257
boards.resize(brdsNum);
258
rvecs_exp.resize(brdsNum);
259
tvecs_exp.resize(brdsNum);
260
objectPoints.clear();
261
objectPoints.resize(brdsNum, chessboard3D);
262
imagePoints_art.clear();
263
imagePoints_findCb.clear();
264
265
vector<Point2f> corners_art, corners_fcb;
266
for(size_t i = 0; i < brdsNum; ++i)
267
{
268
for(;;)
269
{
270
boards[i] = cbg(bg, camMat, distCoeffs, sqSile, corners_art);
271
if(findChessboardCorners(boards[i], cornersSize, corners_fcb))
272
break;
273
}
274
275
//cv::namedWindow("CB"); imshow("CB", boards[i]); cv::waitKey();
276
277
imagePoints_art.push_back(corners_art);
278
imagePoints_findCb.push_back(corners_fcb);
279
280
tvecs_exp[i].create(1, 3, CV_64F);
281
*tvecs_exp[i].ptr<Point3d>() = cbg.corners3d[0];
282
rvecs_exp[i] = calcRvec(cbg.corners3d, cbg.cornersSize());
283
}
284
285
}
286
287
void runTest(const Size& imgSize, const Mat_<double>& camMat, const Mat_<double>& distCoeffs, size_t brdsNum, const Size& cornersSize, int flag = 0)
288
{
289
const TermCriteria tc(TermCriteria::EPS|TermCriteria::MAX_ITER, 30, 0.1);
290
291
vector< vector<Point2f> > imagePoints;
292
293
switch(flag)
294
{
295
case JUST_FIND_CORNERS: imagePoints = imagePoints_findCb; break;
296
case ARTIFICIAL_CORNERS: imagePoints = imagePoints_art; break;
297
298
case USE_CORNERS_SUBPIX:
299
for(size_t i = 0; i < brdsNum; ++i)
300
{
301
Mat gray;
302
cvtColor(boards[i], gray, COLOR_BGR2GRAY);
303
vector<Point2f> tmp = imagePoints_findCb[i];
304
cornerSubPix(gray, tmp, Size(5, 5), Size(-1,-1), tc);
305
imagePoints.push_back(tmp);
306
}
307
break;
308
case USE_4QUAD_CORNERS:
309
for(size_t i = 0; i < brdsNum; ++i)
310
{
311
Mat gray;
312
cvtColor(boards[i], gray, COLOR_BGR2GRAY);
313
vector<Point2f> tmp = imagePoints_findCb[i];
314
find4QuadCornerSubpix(gray, tmp, Size(5, 5));
315
imagePoints.push_back(tmp);
316
}
317
break;
318
default:
319
throw std::exception();
320
}
321
322
Mat camMat_est = Mat::eye(3, 3, CV_64F), distCoeffs_est = Mat::zeros(1, 5, CV_64F);
323
vector<Mat> rvecs_est, tvecs_est;
324
325
int flags = /*CALIB_FIX_K3|*/CALIB_FIX_K4|CALIB_FIX_K5|CALIB_FIX_K6; //CALIB_FIX_K3; //CALIB_FIX_ASPECT_RATIO | | CALIB_ZERO_TANGENT_DIST;
326
TermCriteria criteria = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 100, DBL_EPSILON);
327
double rep_error = calibrateCamera(objectPoints, imagePoints, imgSize, camMat_est, distCoeffs_est, rvecs_est, tvecs_est, flags, criteria);
328
rep_error /= brdsNum * cornersSize.area();
329
330
const double thres = 1;
331
if (rep_error > thres)
332
{
333
ts->printf( cvtest::TS::LOG, "%d) Too big reproject error = %f\n", r, rep_error);
334
ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
335
}
336
337
compareCameraMatrs(camMat, camMat_est);
338
compareDistCoeffs(distCoeffs, distCoeffs_est);
339
compareShiftVecs(tvecs_exp, tvecs_est);
340
compareRotationVecs(rvecs_exp, rvecs_est);
341
342
double rep_errorWOI = reprojectErrorWithoutIntrinsics(chessboard3D, rvecs_exp, tvecs_exp, rvecs_est, tvecs_est);
343
rep_errorWOI /= brdsNum * cornersSize.area();
344
345
const double thres2 = 0.01;
346
if (rep_errorWOI > thres2)
347
{
348
ts->printf( cvtest::TS::LOG, "%d) Too big reproject error without intrinsics = %f\n", r, rep_errorWOI);
349
ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
350
}
351
352
ts->printf( cvtest::TS::LOG, "%d) Testing solvePnP...\n", r);
353
rvecs_spnp.resize(brdsNum);
354
tvecs_spnp.resize(brdsNum);
355
for(size_t i = 0; i < brdsNum; ++i)
356
solvePnP(Mat(objectPoints[i]), Mat(imagePoints[i]), camMat, distCoeffs, rvecs_spnp[i], tvecs_spnp[i]);
357
358
compareShiftVecs(tvecs_exp, tvecs_spnp);
359
compareRotationVecs(rvecs_exp, rvecs_spnp);
360
}
361
362
void run(int)
363
{
364
365
ts->set_failed_test_info(cvtest::TS::OK);
366
RNG& rng = theRNG();
367
368
int progress = 0;
369
int repeat_num = 3;
370
for(r = 0; r < repeat_num; ++r)
371
{
372
const int brds_num = 20;
373
374
Mat bg(Size(640, 480), CV_8UC3);
375
randu(bg, Scalar::all(32), Scalar::all(255));
376
GaussianBlur(bg, bg, Size(5, 5), 2);
377
378
double fx = 300 + (20 * (double)rng - 10);
379
double fy = 300 + (20 * (double)rng - 10);
380
381
double cx = bg.cols/2 + (40 * (double)rng - 20);
382
double cy = bg.rows/2 + (40 * (double)rng - 20);
383
384
Mat_<double> camMat(3, 3);
385
camMat << fx, 0., cx, 0, fy, cy, 0., 0., 1.;
386
387
double k1 = 0.5 + (double)rng/5;
388
double k2 = (double)rng/5;
389
double k3 = (double)rng/5;
390
391
double p1 = 0.001 + (double)rng/10;
392
double p2 = 0.001 + (double)rng/10;
393
394
Mat_<double> distCoeffs(1, 5, 0.0);
395
distCoeffs << k1, k2, p1, p2, k3;
396
397
ChessBoardGenerator cbg(Size(9, 8));
398
cbg.min_cos = 0.9;
399
cbg.cov = 0.8;
400
401
progress = update_progress(progress, r, repeat_num, 0);
402
ts->printf( cvtest::TS::LOG, "\n");
403
prepareForTest(bg, camMat, distCoeffs, brds_num, cbg);
404
405
ts->printf( cvtest::TS::LOG, "artificial corners\n");
406
runTest(bg.size(), camMat, distCoeffs, brds_num, cbg.cornersSize(), ARTIFICIAL_CORNERS);
407
progress = update_progress(progress, r, repeat_num, 0);
408
409
ts->printf( cvtest::TS::LOG, "findChessboard corners\n");
410
runTest(bg.size(), camMat, distCoeffs, brds_num, cbg.cornersSize(), JUST_FIND_CORNERS);
411
progress = update_progress(progress, r, repeat_num, 0);
412
413
ts->printf( cvtest::TS::LOG, "cornersSubPix corners\n");
414
runTest(bg.size(), camMat, distCoeffs, brds_num, cbg.cornersSize(), USE_CORNERS_SUBPIX);
415
progress = update_progress(progress, r, repeat_num, 0);
416
417
ts->printf( cvtest::TS::LOG, "4quad corners\n");
418
runTest(bg.size(), camMat, distCoeffs, brds_num, cbg.cornersSize(), USE_4QUAD_CORNERS);
419
progress = update_progress(progress, r, repeat_num, 0);
420
}
421
}
422
};
423
424
TEST(Calib3d_CalibrateCamera_CPP, DISABLED_accuracy_on_artificial_data) { CV_CalibrateCameraArtificialTest test; test.safe_run(); }
425
426
}} // namespace
427
428