Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/calib3d/test/test_fisheye.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-2011, 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 <opencv2/ts/cuda_test.hpp> // EXPECT_MAT_NEAR
45
#include "../src/fisheye.hpp"
46
#include "opencv2/videoio.hpp"
47
48
namespace opencv_test { namespace {
49
50
class fisheyeTest : public ::testing::Test {
51
52
protected:
53
const static cv::Size imageSize;
54
const static cv::Matx33d K;
55
const static cv::Vec4d D;
56
const static cv::Matx33d R;
57
const static cv::Vec3d T;
58
std::string datasets_repository_path;
59
60
virtual void SetUp() {
61
datasets_repository_path = combine(cvtest::TS::ptr()->get_data_path(), "cv/cameracalibration/fisheye");
62
}
63
64
protected:
65
std::string combine(const std::string& _item1, const std::string& _item2);
66
static void merge4(const cv::Mat& tl, const cv::Mat& tr, const cv::Mat& bl, const cv::Mat& br, cv::Mat& merged);
67
};
68
69
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
70
/// TESTS::
71
72
TEST_F(fisheyeTest, projectPoints)
73
{
74
double cols = this->imageSize.width,
75
rows = this->imageSize.height;
76
77
const int N = 20;
78
cv::Mat distorted0(1, N*N, CV_64FC2), undist1, undist2, distorted1, distorted2;
79
undist2.create(distorted0.size(), CV_MAKETYPE(distorted0.depth(), 3));
80
cv::Vec2d* pts = distorted0.ptr<cv::Vec2d>();
81
82
cv::Vec2d c(this->K(0, 2), this->K(1, 2));
83
for(int y = 0, k = 0; y < N; ++y)
84
for(int x = 0; x < N; ++x)
85
{
86
cv::Vec2d point(x*cols/(N-1.f), y*rows/(N-1.f));
87
pts[k++] = (point - c) * 0.85 + c;
88
}
89
90
cv::fisheye::undistortPoints(distorted0, undist1, this->K, this->D);
91
92
cv::Vec2d* u1 = undist1.ptr<cv::Vec2d>();
93
cv::Vec3d* u2 = undist2.ptr<cv::Vec3d>();
94
for(int i = 0; i < (int)distorted0.total(); ++i)
95
u2[i] = cv::Vec3d(u1[i][0], u1[i][1], 1.0);
96
97
cv::fisheye::distortPoints(undist1, distorted1, this->K, this->D);
98
cv::fisheye::projectPoints(undist2, distorted2, cv::Vec3d::all(0), cv::Vec3d::all(0), this->K, this->D);
99
100
EXPECT_MAT_NEAR(distorted0, distorted1, 1e-10);
101
EXPECT_MAT_NEAR(distorted0, distorted2, 1e-10);
102
}
103
104
TEST_F(fisheyeTest, DISABLED_undistortImage)
105
{
106
cv::Matx33d theK = this->K;
107
cv::Mat theD = cv::Mat(this->D);
108
std::string file = combine(datasets_repository_path, "/calib-3_stereo_from_JY/left/stereo_pair_014.jpg");
109
cv::Matx33d newK = theK;
110
cv::Mat distorted = cv::imread(file), undistorted;
111
{
112
newK(0, 0) = 100;
113
newK(1, 1) = 100;
114
cv::fisheye::undistortImage(distorted, undistorted, theK, theD, newK);
115
cv::Mat correct = cv::imread(combine(datasets_repository_path, "new_f_100.png"));
116
if (correct.empty())
117
CV_Assert(cv::imwrite(combine(datasets_repository_path, "new_f_100.png"), undistorted));
118
else
119
EXPECT_MAT_NEAR(correct, undistorted, 1e-10);
120
}
121
{
122
double balance = 1.0;
123
cv::fisheye::estimateNewCameraMatrixForUndistortRectify(theK, theD, distorted.size(), cv::noArray(), newK, balance);
124
cv::fisheye::undistortImage(distorted, undistorted, theK, theD, newK);
125
cv::Mat correct = cv::imread(combine(datasets_repository_path, "balance_1.0.png"));
126
if (correct.empty())
127
CV_Assert(cv::imwrite(combine(datasets_repository_path, "balance_1.0.png"), undistorted));
128
else
129
EXPECT_MAT_NEAR(correct, undistorted, 1e-10);
130
}
131
132
{
133
double balance = 0.0;
134
cv::fisheye::estimateNewCameraMatrixForUndistortRectify(theK, theD, distorted.size(), cv::noArray(), newK, balance);
135
cv::fisheye::undistortImage(distorted, undistorted, theK, theD, newK);
136
cv::Mat correct = cv::imread(combine(datasets_repository_path, "balance_0.0.png"));
137
if (correct.empty())
138
CV_Assert(cv::imwrite(combine(datasets_repository_path, "balance_0.0.png"), undistorted));
139
else
140
EXPECT_MAT_NEAR(correct, undistorted, 1e-10);
141
}
142
}
143
144
TEST_F(fisheyeTest, jacobians)
145
{
146
int n = 10;
147
cv::Mat X(1, n, CV_64FC3);
148
cv::Mat om(3, 1, CV_64F), theT(3, 1, CV_64F);
149
cv::Mat f(2, 1, CV_64F), c(2, 1, CV_64F);
150
cv::Mat k(4, 1, CV_64F);
151
double alpha;
152
153
cv::RNG r;
154
155
r.fill(X, cv::RNG::NORMAL, 2, 1);
156
X = cv::abs(X) * 10;
157
158
r.fill(om, cv::RNG::NORMAL, 0, 1);
159
om = cv::abs(om);
160
161
r.fill(theT, cv::RNG::NORMAL, 0, 1);
162
theT = cv::abs(theT); theT.at<double>(2) = 4; theT *= 10;
163
164
r.fill(f, cv::RNG::NORMAL, 0, 1);
165
f = cv::abs(f) * 1000;
166
167
r.fill(c, cv::RNG::NORMAL, 0, 1);
168
c = cv::abs(c) * 1000;
169
170
r.fill(k, cv::RNG::NORMAL, 0, 1);
171
k*= 0.5;
172
173
alpha = 0.01*r.gaussian(1);
174
175
cv::Mat x1, x2, xpred;
176
cv::Matx33d theK(f.at<double>(0), alpha * f.at<double>(0), c.at<double>(0),
177
0, f.at<double>(1), c.at<double>(1),
178
0, 0, 1);
179
180
cv::Mat jacobians;
181
cv::fisheye::projectPoints(X, x1, om, theT, theK, k, alpha, jacobians);
182
183
//test on T:
184
cv::Mat dT(3, 1, CV_64FC1);
185
r.fill(dT, cv::RNG::NORMAL, 0, 1);
186
dT *= 1e-9*cv::norm(theT);
187
cv::Mat T2 = theT + dT;
188
cv::fisheye::projectPoints(X, x2, om, T2, theK, k, alpha, cv::noArray());
189
xpred = x1 + cv::Mat(jacobians.colRange(11,14) * dT).reshape(2, 1);
190
CV_Assert (cv::norm(x2 - xpred) < 1e-10);
191
192
//test on om:
193
cv::Mat dom(3, 1, CV_64FC1);
194
r.fill(dom, cv::RNG::NORMAL, 0, 1);
195
dom *= 1e-9*cv::norm(om);
196
cv::Mat om2 = om + dom;
197
cv::fisheye::projectPoints(X, x2, om2, theT, theK, k, alpha, cv::noArray());
198
xpred = x1 + cv::Mat(jacobians.colRange(8,11) * dom).reshape(2, 1);
199
CV_Assert (cv::norm(x2 - xpred) < 1e-10);
200
201
//test on f:
202
cv::Mat df(2, 1, CV_64FC1);
203
r.fill(df, cv::RNG::NORMAL, 0, 1);
204
df *= 1e-9*cv::norm(f);
205
cv::Matx33d K2 = theK + cv::Matx33d(df.at<double>(0), df.at<double>(0) * alpha, 0, 0, df.at<double>(1), 0, 0, 0, 0);
206
cv::fisheye::projectPoints(X, x2, om, theT, K2, k, alpha, cv::noArray());
207
xpred = x1 + cv::Mat(jacobians.colRange(0,2) * df).reshape(2, 1);
208
CV_Assert (cv::norm(x2 - xpred) < 1e-10);
209
210
//test on c:
211
cv::Mat dc(2, 1, CV_64FC1);
212
r.fill(dc, cv::RNG::NORMAL, 0, 1);
213
dc *= 1e-9*cv::norm(c);
214
K2 = theK + cv::Matx33d(0, 0, dc.at<double>(0), 0, 0, dc.at<double>(1), 0, 0, 0);
215
cv::fisheye::projectPoints(X, x2, om, theT, K2, k, alpha, cv::noArray());
216
xpred = x1 + cv::Mat(jacobians.colRange(2,4) * dc).reshape(2, 1);
217
CV_Assert (cv::norm(x2 - xpred) < 1e-10);
218
219
//test on k:
220
cv::Mat dk(4, 1, CV_64FC1);
221
r.fill(dk, cv::RNG::NORMAL, 0, 1);
222
dk *= 1e-9*cv::norm(k);
223
cv::Mat k2 = k + dk;
224
cv::fisheye::projectPoints(X, x2, om, theT, theK, k2, alpha, cv::noArray());
225
xpred = x1 + cv::Mat(jacobians.colRange(4,8) * dk).reshape(2, 1);
226
CV_Assert (cv::norm(x2 - xpred) < 1e-10);
227
228
//test on alpha:
229
cv::Mat dalpha(1, 1, CV_64FC1);
230
r.fill(dalpha, cv::RNG::NORMAL, 0, 1);
231
dalpha *= 1e-9*cv::norm(f);
232
double alpha2 = alpha + dalpha.at<double>(0);
233
K2 = theK + cv::Matx33d(0, f.at<double>(0) * dalpha.at<double>(0), 0, 0, 0, 0, 0, 0, 0);
234
cv::fisheye::projectPoints(X, x2, om, theT, theK, k, alpha2, cv::noArray());
235
xpred = x1 + cv::Mat(jacobians.col(14) * dalpha).reshape(2, 1);
236
CV_Assert (cv::norm(x2 - xpred) < 1e-10);
237
}
238
239
TEST_F(fisheyeTest, Calibration)
240
{
241
const int n_images = 34;
242
243
std::vector<std::vector<cv::Point2d> > imagePoints(n_images);
244
std::vector<std::vector<cv::Point3d> > objectPoints(n_images);
245
246
const std::string folder =combine(datasets_repository_path, "calib-3_stereo_from_JY");
247
cv::FileStorage fs_left(combine(folder, "left.xml"), cv::FileStorage::READ);
248
CV_Assert(fs_left.isOpened());
249
for(int i = 0; i < n_images; ++i)
250
fs_left[cv::format("image_%d", i )] >> imagePoints[i];
251
fs_left.release();
252
253
cv::FileStorage fs_object(combine(folder, "object.xml"), cv::FileStorage::READ);
254
CV_Assert(fs_object.isOpened());
255
for(int i = 0; i < n_images; ++i)
256
fs_object[cv::format("image_%d", i )] >> objectPoints[i];
257
fs_object.release();
258
259
int flag = 0;
260
flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;
261
flag |= cv::fisheye::CALIB_CHECK_COND;
262
flag |= cv::fisheye::CALIB_FIX_SKEW;
263
264
cv::Matx33d theK;
265
cv::Vec4d theD;
266
267
cv::fisheye::calibrate(objectPoints, imagePoints, imageSize, theK, theD,
268
cv::noArray(), cv::noArray(), flag, cv::TermCriteria(3, 20, 1e-6));
269
270
EXPECT_MAT_NEAR(theK, this->K, 1e-10);
271
EXPECT_MAT_NEAR(theD, this->D, 1e-10);
272
}
273
274
TEST_F(fisheyeTest, Homography)
275
{
276
const int n_images = 1;
277
278
std::vector<std::vector<cv::Point2d> > imagePoints(n_images);
279
std::vector<std::vector<cv::Point3d> > objectPoints(n_images);
280
281
const std::string folder =combine(datasets_repository_path, "calib-3_stereo_from_JY");
282
cv::FileStorage fs_left(combine(folder, "left.xml"), cv::FileStorage::READ);
283
CV_Assert(fs_left.isOpened());
284
for(int i = 0; i < n_images; ++i)
285
fs_left[cv::format("image_%d", i )] >> imagePoints[i];
286
fs_left.release();
287
288
cv::FileStorage fs_object(combine(folder, "object.xml"), cv::FileStorage::READ);
289
CV_Assert(fs_object.isOpened());
290
for(int i = 0; i < n_images; ++i)
291
fs_object[cv::format("image_%d", i )] >> objectPoints[i];
292
fs_object.release();
293
294
cv::internal::IntrinsicParams param;
295
param.Init(cv::Vec2d(cv::max(imageSize.width, imageSize.height) / CV_PI, cv::max(imageSize.width, imageSize.height) / CV_PI),
296
cv::Vec2d(imageSize.width / 2.0 - 0.5, imageSize.height / 2.0 - 0.5));
297
298
cv::Mat _imagePoints (imagePoints[0]);
299
cv::Mat _objectPoints(objectPoints[0]);
300
301
cv::Mat imagePointsNormalized = NormalizePixels(_imagePoints, param).reshape(1).t();
302
_objectPoints = _objectPoints.reshape(1).t();
303
cv::Mat objectPointsMean, covObjectPoints;
304
305
int Np = imagePointsNormalized.cols;
306
cv::calcCovarMatrix(_objectPoints, covObjectPoints, objectPointsMean, cv::COVAR_NORMAL | cv::COVAR_COLS);
307
cv::SVD svd(covObjectPoints);
308
cv::Mat theR(svd.vt);
309
310
if (cv::norm(theR(cv::Rect(2, 0, 1, 2))) < 1e-6)
311
theR = cv::Mat::eye(3,3, CV_64FC1);
312
if (cv::determinant(theR) < 0)
313
theR = -theR;
314
315
cv::Mat theT = -theR * objectPointsMean;
316
cv::Mat X_new = theR * _objectPoints + theT * cv::Mat::ones(1, Np, CV_64FC1);
317
cv::Mat H = cv::internal::ComputeHomography(imagePointsNormalized, X_new.rowRange(0, 2));
318
319
cv::Mat M = cv::Mat::ones(3, X_new.cols, CV_64FC1);
320
X_new.rowRange(0, 2).copyTo(M.rowRange(0, 2));
321
cv::Mat mrep = H * M;
322
323
cv::divide(mrep, cv::Mat::ones(3,1, CV_64FC1) * mrep.row(2).clone(), mrep);
324
325
cv::Mat merr = (mrep.rowRange(0, 2) - imagePointsNormalized).t();
326
327
cv::Vec2d std_err;
328
cv::meanStdDev(merr.reshape(2), cv::noArray(), std_err);
329
std_err *= sqrt((double)merr.reshape(2).total() / (merr.reshape(2).total() - 1));
330
331
cv::Vec2d correct_std_err(0.00516740156010384, 0.00644205331553901);
332
EXPECT_MAT_NEAR(std_err, correct_std_err, 1e-12);
333
}
334
335
TEST_F(fisheyeTest, EstimateUncertainties)
336
{
337
const int n_images = 34;
338
339
std::vector<std::vector<cv::Point2d> > imagePoints(n_images);
340
std::vector<std::vector<cv::Point3d> > objectPoints(n_images);
341
342
const std::string folder =combine(datasets_repository_path, "calib-3_stereo_from_JY");
343
cv::FileStorage fs_left(combine(folder, "left.xml"), cv::FileStorage::READ);
344
CV_Assert(fs_left.isOpened());
345
for(int i = 0; i < n_images; ++i)
346
fs_left[cv::format("image_%d", i )] >> imagePoints[i];
347
fs_left.release();
348
349
cv::FileStorage fs_object(combine(folder, "object.xml"), cv::FileStorage::READ);
350
CV_Assert(fs_object.isOpened());
351
for(int i = 0; i < n_images; ++i)
352
fs_object[cv::format("image_%d", i )] >> objectPoints[i];
353
fs_object.release();
354
355
int flag = 0;
356
flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;
357
flag |= cv::fisheye::CALIB_CHECK_COND;
358
flag |= cv::fisheye::CALIB_FIX_SKEW;
359
360
cv::Matx33d theK;
361
cv::Vec4d theD;
362
std::vector<cv::Vec3d> rvec;
363
std::vector<cv::Vec3d> tvec;
364
365
cv::fisheye::calibrate(objectPoints, imagePoints, imageSize, theK, theD,
366
rvec, tvec, flag, cv::TermCriteria(3, 20, 1e-6));
367
368
cv::internal::IntrinsicParams param, errors;
369
cv::Vec2d err_std;
370
double thresh_cond = 1e6;
371
int check_cond = 1;
372
param.Init(cv::Vec2d(theK(0,0), theK(1,1)), cv::Vec2d(theK(0,2), theK(1, 2)), theD);
373
param.isEstimate = std::vector<uchar>(9, 1);
374
param.isEstimate[4] = 0;
375
376
errors.isEstimate = param.isEstimate;
377
378
double rms;
379
380
cv::internal::EstimateUncertainties(objectPoints, imagePoints, param, rvec, tvec,
381
errors, err_std, thresh_cond, check_cond, rms);
382
383
EXPECT_MAT_NEAR(errors.f, cv::Vec2d(1.29837104202046, 1.31565641071524), 1e-10);
384
EXPECT_MAT_NEAR(errors.c, cv::Vec2d(0.890439368129246, 0.816096854937896), 1e-10);
385
EXPECT_MAT_NEAR(errors.k, cv::Vec4d(0.00516248605191506, 0.0168181467500934, 0.0213118690274604, 0.00916010877545648), 1e-10);
386
EXPECT_MAT_NEAR(err_std, cv::Vec2d(0.187475975266883, 0.185678953263995), 1e-10);
387
CV_Assert(fabs(rms - 0.263782587133546) < 1e-10);
388
CV_Assert(errors.alpha == 0);
389
}
390
391
TEST_F(fisheyeTest, stereoRectify)
392
{
393
const std::string folder =combine(datasets_repository_path, "calib-3_stereo_from_JY");
394
395
cv::Size calibration_size = this->imageSize, requested_size = calibration_size;
396
cv::Matx33d K1 = this->K, K2 = K1;
397
cv::Mat D1 = cv::Mat(this->D), D2 = D1;
398
399
cv::Vec3d theT = this->T;
400
cv::Matx33d theR = this->R;
401
402
double balance = 0.0, fov_scale = 1.1;
403
cv::Mat R1, R2, P1, P2, Q;
404
cv::fisheye::stereoRectify(K1, D1, K2, D2, calibration_size, theR, theT, R1, R2, P1, P2, Q,
405
cv::CALIB_ZERO_DISPARITY, requested_size, balance, fov_scale);
406
407
// Collected with these CMake flags: -DWITH_IPP=OFF -DCV_ENABLE_INTRINSICS=OFF -DCV_DISABLE_OPTIMIZATION=ON -DCMAKE_BUILD_TYPE=Debug
408
cv::Matx33d R1_ref(
409
0.9992853269091279, 0.03779164101000276, -0.0007920188690205426,
410
-0.03778569762983931, 0.9992646472015868, 0.006511981857667881,
411
0.001037534936357442, -0.006477400933964018, 0.9999784831677112
412
);
413
cv::Matx33d R2_ref(
414
0.9994868963898833, -0.03197579751378937, -0.001868774538573449,
415
0.03196298186616116, 0.9994677442608699, -0.0065265589947392,
416
0.002076471801477729, 0.006463478587068991, 0.9999769555891836
417
);
418
cv::Matx34d P1_ref(
419
420.8551870450913, 0, 586.501617798451, 0,
420
0, 420.8551870450913, 374.7667511986098, 0,
421
0, 0, 1, 0
422
);
423
cv::Matx34d P2_ref(
424
420.8551870450913, 0, 586.501617798451, -41.77758076597302,
425
0, 420.8551870450913, 374.7667511986098, 0,
426
0, 0, 1, 0
427
);
428
cv::Matx44d Q_ref(
429
1, 0, 0, -586.501617798451,
430
0, 1, 0, -374.7667511986098,
431
0, 0, 0, 420.8551870450913,
432
0, 0, 10.07370889670733, -0
433
);
434
435
const double eps = 1e-10;
436
EXPECT_MAT_NEAR(R1_ref, R1, eps);
437
EXPECT_MAT_NEAR(R2_ref, R2, eps);
438
EXPECT_MAT_NEAR(P1_ref, P1, eps);
439
EXPECT_MAT_NEAR(P2_ref, P2, eps);
440
EXPECT_MAT_NEAR(Q_ref, Q, eps);
441
442
if (::testing::Test::HasFailure())
443
{
444
std::cout << "Actual values are:" << std::endl
445
<< "R1 =" << std::endl << R1 << std::endl
446
<< "R2 =" << std::endl << R2 << std::endl
447
<< "P1 =" << std::endl << P1 << std::endl
448
<< "P2 =" << std::endl << P2 << std::endl
449
<< "Q =" << std::endl << Q << std::endl;
450
}
451
452
#if 1 // Debug code
453
cv::Mat lmapx, lmapy, rmapx, rmapy;
454
//rewrite for fisheye
455
cv::fisheye::initUndistortRectifyMap(K1, D1, R1, P1, requested_size, CV_32F, lmapx, lmapy);
456
cv::fisheye::initUndistortRectifyMap(K2, D2, R2, P2, requested_size, CV_32F, rmapx, rmapy);
457
458
cv::Mat l, r, lundist, rundist;
459
for (int i = 0; i < 34; ++i)
460
{
461
SCOPED_TRACE(cv::format("image %d", i));
462
l = imread(combine(folder, cv::format("left/stereo_pair_%03d.jpg", i)), cv::IMREAD_COLOR);
463
r = imread(combine(folder, cv::format("right/stereo_pair_%03d.jpg", i)), cv::IMREAD_COLOR);
464
ASSERT_FALSE(l.empty());
465
ASSERT_FALSE(r.empty());
466
467
int ndisp = 128;
468
cv::rectangle(l, cv::Rect(255, 0, 829, l.rows-1), cv::Scalar(0, 0, 255));
469
cv::rectangle(r, cv::Rect(255, 0, 829, l.rows-1), cv::Scalar(0, 0, 255));
470
cv::rectangle(r, cv::Rect(255-ndisp, 0, 829+ndisp ,l.rows-1), cv::Scalar(0, 0, 255));
471
cv::remap(l, lundist, lmapx, lmapy, cv::INTER_LINEAR);
472
cv::remap(r, rundist, rmapx, rmapy, cv::INTER_LINEAR);
473
474
for (int ii = 0; ii < lundist.rows; ii += 20)
475
{
476
cv::line(lundist, cv::Point(0, ii), cv::Point(lundist.cols, ii), cv::Scalar(0, 255, 0));
477
cv::line(rundist, cv::Point(0, ii), cv::Point(lundist.cols, ii), cv::Scalar(0, 255, 0));
478
}
479
480
cv::Mat rectification;
481
merge4(l, r, lundist, rundist, rectification);
482
483
cv::imwrite(cv::format("fisheye_rectification_AB_%03d.png", i), rectification);
484
}
485
#endif
486
}
487
488
TEST_F(fisheyeTest, stereoCalibrate)
489
{
490
const int n_images = 34;
491
492
const std::string folder =combine(datasets_repository_path, "calib-3_stereo_from_JY");
493
494
std::vector<std::vector<cv::Point2d> > leftPoints(n_images);
495
std::vector<std::vector<cv::Point2d> > rightPoints(n_images);
496
std::vector<std::vector<cv::Point3d> > objectPoints(n_images);
497
498
cv::FileStorage fs_left(combine(folder, "left.xml"), cv::FileStorage::READ);
499
CV_Assert(fs_left.isOpened());
500
for(int i = 0; i < n_images; ++i)
501
fs_left[cv::format("image_%d", i )] >> leftPoints[i];
502
fs_left.release();
503
504
cv::FileStorage fs_right(combine(folder, "right.xml"), cv::FileStorage::READ);
505
CV_Assert(fs_right.isOpened());
506
for(int i = 0; i < n_images; ++i)
507
fs_right[cv::format("image_%d", i )] >> rightPoints[i];
508
fs_right.release();
509
510
cv::FileStorage fs_object(combine(folder, "object.xml"), cv::FileStorage::READ);
511
CV_Assert(fs_object.isOpened());
512
for(int i = 0; i < n_images; ++i)
513
fs_object[cv::format("image_%d", i )] >> objectPoints[i];
514
fs_object.release();
515
516
cv::Matx33d K1, K2, theR;
517
cv::Vec3d theT;
518
cv::Vec4d D1, D2;
519
520
int flag = 0;
521
flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;
522
flag |= cv::fisheye::CALIB_CHECK_COND;
523
flag |= cv::fisheye::CALIB_FIX_SKEW;
524
525
cv::fisheye::stereoCalibrate(objectPoints, leftPoints, rightPoints,
526
K1, D1, K2, D2, imageSize, theR, theT, flag,
527
cv::TermCriteria(3, 12, 0));
528
529
cv::Matx33d R_correct( 0.9975587205950972, 0.06953016383322372, 0.006492709911733523,
530
-0.06956823121068059, 0.9975601387249519, 0.005833595226966235,
531
-0.006071257768382089, -0.006271040135405457, 0.9999619062167968);
532
cv::Vec3d T_correct(-0.099402724724121, 0.00270812139265413, 0.00129330292472699);
533
cv::Matx33d K1_correct (561.195925927249, 0, 621.282400272412,
534
0, 562.849402029712, 380.555455380889,
535
0, 0, 1);
536
537
cv::Matx33d K2_correct (560.395452535348, 0, 678.971652040359,
538
0, 561.90171021422, 380.401340535339,
539
0, 0, 1);
540
541
cv::Vec4d D1_correct (-7.44253716539556e-05, -0.00702662033932424, 0.00737569823650885, -0.00342230256441771);
542
cv::Vec4d D2_correct (-0.0130785435677431, 0.0284434505383497, -0.0360333869900506, 0.0144724062347222);
543
544
EXPECT_MAT_NEAR(theR, R_correct, 1e-10);
545
EXPECT_MAT_NEAR(theT, T_correct, 1e-10);
546
547
EXPECT_MAT_NEAR(K1, K1_correct, 1e-10);
548
EXPECT_MAT_NEAR(K2, K2_correct, 1e-10);
549
550
EXPECT_MAT_NEAR(D1, D1_correct, 1e-10);
551
EXPECT_MAT_NEAR(D2, D2_correct, 1e-10);
552
553
}
554
555
TEST_F(fisheyeTest, stereoCalibrateFixIntrinsic)
556
{
557
const int n_images = 34;
558
559
const std::string folder =combine(datasets_repository_path, "calib-3_stereo_from_JY");
560
561
std::vector<std::vector<cv::Point2d> > leftPoints(n_images);
562
std::vector<std::vector<cv::Point2d> > rightPoints(n_images);
563
std::vector<std::vector<cv::Point3d> > objectPoints(n_images);
564
565
cv::FileStorage fs_left(combine(folder, "left.xml"), cv::FileStorage::READ);
566
CV_Assert(fs_left.isOpened());
567
for(int i = 0; i < n_images; ++i)
568
fs_left[cv::format("image_%d", i )] >> leftPoints[i];
569
fs_left.release();
570
571
cv::FileStorage fs_right(combine(folder, "right.xml"), cv::FileStorage::READ);
572
CV_Assert(fs_right.isOpened());
573
for(int i = 0; i < n_images; ++i)
574
fs_right[cv::format("image_%d", i )] >> rightPoints[i];
575
fs_right.release();
576
577
cv::FileStorage fs_object(combine(folder, "object.xml"), cv::FileStorage::READ);
578
CV_Assert(fs_object.isOpened());
579
for(int i = 0; i < n_images; ++i)
580
fs_object[cv::format("image_%d", i )] >> objectPoints[i];
581
fs_object.release();
582
583
cv::Matx33d theR;
584
cv::Vec3d theT;
585
586
int flag = 0;
587
flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;
588
flag |= cv::fisheye::CALIB_CHECK_COND;
589
flag |= cv::fisheye::CALIB_FIX_SKEW;
590
flag |= cv::fisheye::CALIB_FIX_INTRINSIC;
591
592
cv::Matx33d K1 (561.195925927249, 0, 621.282400272412,
593
0, 562.849402029712, 380.555455380889,
594
0, 0, 1);
595
596
cv::Matx33d K2 (560.395452535348, 0, 678.971652040359,
597
0, 561.90171021422, 380.401340535339,
598
0, 0, 1);
599
600
cv::Vec4d D1 (-7.44253716539556e-05, -0.00702662033932424, 0.00737569823650885, -0.00342230256441771);
601
cv::Vec4d D2 (-0.0130785435677431, 0.0284434505383497, -0.0360333869900506, 0.0144724062347222);
602
603
cv::fisheye::stereoCalibrate(objectPoints, leftPoints, rightPoints,
604
K1, D1, K2, D2, imageSize, theR, theT, flag,
605
cv::TermCriteria(3, 12, 0));
606
607
cv::Matx33d R_correct( 0.9975587205950972, 0.06953016383322372, 0.006492709911733523,
608
-0.06956823121068059, 0.9975601387249519, 0.005833595226966235,
609
-0.006071257768382089, -0.006271040135405457, 0.9999619062167968);
610
cv::Vec3d T_correct(-0.099402724724121, 0.00270812139265413, 0.00129330292472699);
611
612
613
EXPECT_MAT_NEAR(theR, R_correct, 1e-10);
614
EXPECT_MAT_NEAR(theT, T_correct, 1e-10);
615
}
616
617
TEST_F(fisheyeTest, CalibrationWithDifferentPointsNumber)
618
{
619
const int n_images = 2;
620
621
std::vector<std::vector<cv::Point2d> > imagePoints(n_images);
622
std::vector<std::vector<cv::Point3d> > objectPoints(n_images);
623
624
std::vector<cv::Point2d> imgPoints1(10);
625
std::vector<cv::Point2d> imgPoints2(15);
626
627
std::vector<cv::Point3d> objectPoints1(imgPoints1.size());
628
std::vector<cv::Point3d> objectPoints2(imgPoints2.size());
629
630
for (size_t i = 0; i < imgPoints1.size(); i++)
631
{
632
imgPoints1[i] = cv::Point2d((double)i, (double)i);
633
objectPoints1[i] = cv::Point3d((double)i, (double)i, 10.0);
634
}
635
636
for (size_t i = 0; i < imgPoints2.size(); i++)
637
{
638
imgPoints2[i] = cv::Point2d(i + 0.5, i + 0.5);
639
objectPoints2[i] = cv::Point3d(i + 0.5, i + 0.5, 10.0);
640
}
641
642
imagePoints[0] = imgPoints1;
643
imagePoints[1] = imgPoints2;
644
objectPoints[0] = objectPoints1;
645
objectPoints[1] = objectPoints2;
646
647
cv::Matx33d theK = cv::Matx33d::eye();
648
cv::Vec4d theD;
649
650
int flag = 0;
651
flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;
652
flag |= cv::fisheye::CALIB_USE_INTRINSIC_GUESS;
653
flag |= cv::fisheye::CALIB_FIX_SKEW;
654
655
cv::fisheye::calibrate(objectPoints, imagePoints, cv::Size(100, 100), theK, theD,
656
cv::noArray(), cv::noArray(), flag, cv::TermCriteria(3, 20, 1e-6));
657
}
658
659
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
660
/// fisheyeTest::
661
662
const cv::Size fisheyeTest::imageSize(1280, 800);
663
664
const cv::Matx33d fisheyeTest::K(558.478087865323, 0, 620.458515360843,
665
0, 560.506767351568, 381.939424848348,
666
0, 0, 1);
667
668
const cv::Vec4d fisheyeTest::D(-0.0014613319981768, -0.00329861110580401, 0.00605760088590183, -0.00374209380722371);
669
670
const cv::Matx33d fisheyeTest::R ( 9.9756700084424932e-01, 6.9698277640183867e-02, 1.4929569991321144e-03,
671
-6.9711825162322980e-02, 9.9748249845531767e-01, 1.2997180766418455e-02,
672
-5.8331736398316541e-04,-1.3069635393884985e-02, 9.9991441852366736e-01);
673
674
const cv::Vec3d fisheyeTest::T(-9.9217369356044638e-02, 3.1741831972356663e-03, 1.8551007952921010e-04);
675
676
std::string fisheyeTest::combine(const std::string& _item1, const std::string& _item2)
677
{
678
std::string item1 = _item1, item2 = _item2;
679
std::replace(item1.begin(), item1.end(), '\\', '/');
680
std::replace(item2.begin(), item2.end(), '\\', '/');
681
682
if (item1.empty())
683
return item2;
684
685
if (item2.empty())
686
return item1;
687
688
char last = item1[item1.size()-1];
689
return item1 + (last != '/' ? "/" : "") + item2;
690
}
691
692
void fisheyeTest::merge4(const cv::Mat& tl, const cv::Mat& tr, const cv::Mat& bl, const cv::Mat& br, cv::Mat& merged)
693
{
694
int type = tl.type();
695
cv::Size sz = tl.size();
696
ASSERT_EQ(type, tr.type()); ASSERT_EQ(type, bl.type()); ASSERT_EQ(type, br.type());
697
ASSERT_EQ(sz.width, tr.cols); ASSERT_EQ(sz.width, bl.cols); ASSERT_EQ(sz.width, br.cols);
698
ASSERT_EQ(sz.height, tr.rows); ASSERT_EQ(sz.height, bl.rows); ASSERT_EQ(sz.height, br.rows);
699
700
merged.create(cv::Size(sz.width * 2, sz.height * 2), type);
701
tl.copyTo(merged(cv::Rect(0, 0, sz.width, sz.height)));
702
tr.copyTo(merged(cv::Rect(sz.width, 0, sz.width, sz.height)));
703
bl.copyTo(merged(cv::Rect(0, sz.height, sz.width, sz.height)));
704
br.copyTo(merged(cv::Rect(sz.width, sz.height, sz.width, sz.height)));
705
}
706
707
}} // namespace
708
709