Skip to content

Commit e2ca50f

Browse files
authored
Merge pull request opencv#19539 from asmorkalov:as/calib_fix_focal_length
* Added CALIB_FIX_FOCAL_LENGTH to fisheye calibration opencv#13450 Sometimes you want to calibrate just the principal point of a camera, or just the distortion coefficients. In this case, you can pass the CALIB_FIX_FOCAL_LENGTH flag to keep Fx and Fy * Added test for CALIB_FIX_FOCAL_LENGTH option in fisheye callinration.
1 parent be24659 commit e2ca50f

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

modules/calib3d/include/opencv2/calib3d.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3773,7 +3773,8 @@ namespace fisheye
37733773
CALIB_FIX_K4 = 1 << 7,
37743774
CALIB_FIX_INTRINSIC = 1 << 8,
37753775
CALIB_FIX_PRINCIPAL_POINT = 1 << 9,
3776-
CALIB_ZERO_DISPARITY = 1 << 10
3776+
CALIB_ZERO_DISPARITY = 1 << 10,
3777+
CALIB_FIX_FOCAL_LENGTH = 1 << 11
37773778
};
37783779

37793780
/** @brief Projects points using fisheye model
@@ -3927,6 +3928,8 @@ namespace fisheye
39273928
are set to zeros and stay zero.
39283929
- @ref fisheye::CALIB_FIX_PRINCIPAL_POINT The principal point is not changed during the global
39293930
optimization. It stays at the center or at a different location specified when @ref fisheye::CALIB_USE_INTRINSIC_GUESS is set too.
3931+
- @ref fisheye::CALIB_FIX_FOCAL_LENGTH The focal length is not changed during the global
3932+
optimization. It is the \f$max(width,height)/\pi\f$ or the provided \f$f_x\f$, \f$f_y\f$ when @ref fisheye::CALIB_USE_INTRINSIC_GUESS is set too.
39303933
@param criteria Termination criteria for the iterative optimization algorithm.
39313934
*/
39323935
CV_EXPORTS_W double calibrate(InputArrayOfArrays objectPoints, InputArrayOfArrays imagePoints, const Size& image_size,

modules/calib3d/src/fisheye.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -754,8 +754,8 @@ double cv::fisheye::calibrate(InputArrayOfArrays objectPoints, InputArrayOfArray
754754
IntrinsicParams currentParam;
755755
IntrinsicParams errors;
756756

757-
finalParam.isEstimate[0] = 1;
758-
finalParam.isEstimate[1] = 1;
757+
finalParam.isEstimate[0] = flags & CALIB_FIX_FOCAL_LENGTH ? 0 : 1;
758+
finalParam.isEstimate[1] = flags & CALIB_FIX_FOCAL_LENGTH ? 0 : 1;
759759
finalParam.isEstimate[2] = flags & CALIB_FIX_PRINCIPAL_POINT ? 0 : 1;
760760
finalParam.isEstimate[3] = flags & CALIB_FIX_PRINCIPAL_POINT ? 0 : 1;
761761
finalParam.isEstimate[4] = flags & CALIB_FIX_SKEW ? 0 : 1;

modules/calib3d/test/test_fisheye.cpp

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ TEST_F(fisheyeTest, Calibration)
345345
std::vector<std::vector<cv::Point2d> > imagePoints(n_images);
346346
std::vector<std::vector<cv::Point3d> > objectPoints(n_images);
347347

348-
const std::string folder =combine(datasets_repository_path, "calib-3_stereo_from_JY");
348+
const std::string folder = combine(datasets_repository_path, "calib-3_stereo_from_JY");
349349
cv::FileStorage fs_left(combine(folder, "left.xml"), cv::FileStorage::READ);
350350
CV_Assert(fs_left.isOpened());
351351
for(int i = 0; i < n_images; ++i)
@@ -373,14 +373,61 @@ TEST_F(fisheyeTest, Calibration)
373373
EXPECT_MAT_NEAR(theD, this->D, 1e-10);
374374
}
375375

376+
TEST_F(fisheyeTest, CalibrationWithFixedFocalLength)
377+
{
378+
const int n_images = 34;
379+
380+
std::vector<std::vector<cv::Point2d> > imagePoints(n_images);
381+
std::vector<std::vector<cv::Point3d> > objectPoints(n_images);
382+
383+
const std::string folder =combine(datasets_repository_path, "calib-3_stereo_from_JY");
384+
cv::FileStorage fs_left(combine(folder, "left.xml"), cv::FileStorage::READ);
385+
CV_Assert(fs_left.isOpened());
386+
for(int i = 0; i < n_images; ++i)
387+
fs_left[cv::format("image_%d", i )] >> imagePoints[i];
388+
fs_left.release();
389+
390+
cv::FileStorage fs_object(combine(folder, "object.xml"), cv::FileStorage::READ);
391+
CV_Assert(fs_object.isOpened());
392+
for(int i = 0; i < n_images; ++i)
393+
fs_object[cv::format("image_%d", i )] >> objectPoints[i];
394+
fs_object.release();
395+
396+
int flag = 0;
397+
flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;
398+
flag |= cv::fisheye::CALIB_CHECK_COND;
399+
flag |= cv::fisheye::CALIB_FIX_SKEW;
400+
flag |= cv::fisheye::CALIB_FIX_FOCAL_LENGTH;
401+
flag |= cv::fisheye::CALIB_USE_INTRINSIC_GUESS;
402+
403+
cv::Matx33d theK = this->K;
404+
const cv::Matx33d newK(
405+
558.478088, 0.000000, 620.458461,
406+
0.000000, 560.506767, 381.939362,
407+
0.000000, 0.000000, 1.000000);
408+
409+
cv::Vec4d theD;
410+
const cv::Vec4d newD(-0.001461, -0.003298, 0.006057, -0.003742);
411+
412+
cv::fisheye::calibrate(objectPoints, imagePoints, imageSize, theK, theD,
413+
cv::noArray(), cv::noArray(), flag, cv::TermCriteria(3, 20, 1e-6));
414+
415+
// ensure that CALIB_FIX_FOCAL_LENGTH works and focal lenght has not changed
416+
EXPECT_EQ(theK(0,0), K(0,0));
417+
EXPECT_EQ(theK(1,1), K(1,1));
418+
419+
EXPECT_MAT_NEAR(theK, newK, 1e-6);
420+
EXPECT_MAT_NEAR(theD, newD, 1e-6);
421+
}
422+
376423
TEST_F(fisheyeTest, Homography)
377424
{
378425
const int n_images = 1;
379426

380427
std::vector<std::vector<cv::Point2d> > imagePoints(n_images);
381428
std::vector<std::vector<cv::Point3d> > objectPoints(n_images);
382429

383-
const std::string folder =combine(datasets_repository_path, "calib-3_stereo_from_JY");
430+
const std::string folder = combine(datasets_repository_path, "calib-3_stereo_from_JY");
384431
cv::FileStorage fs_left(combine(folder, "left.xml"), cv::FileStorage::READ);
385432
CV_Assert(fs_left.isOpened());
386433
for(int i = 0; i < n_images; ++i)
@@ -498,7 +545,7 @@ TEST_F(fisheyeTest, stereoRectify)
498545
"For the purpose of continuity the following should be true: cv::CALIB_ZERO_DISPARITY == cv::fisheye::CALIB_ZERO_DISPARITY"
499546
);
500547

501-
const std::string folder =combine(datasets_repository_path, "calib-3_stereo_from_JY");
548+
const std::string folder = combine(datasets_repository_path, "calib-3_stereo_from_JY");
502549

503550
cv::Size calibration_size = this->imageSize, requested_size = calibration_size;
504551
cv::Matx33d K1 = this->K, K2 = K1;
@@ -599,7 +646,7 @@ TEST_F(fisheyeTest, stereoCalibrate)
599646
{
600647
const int n_images = 34;
601648

602-
const std::string folder =combine(datasets_repository_path, "calib-3_stereo_from_JY");
649+
const std::string folder = combine(datasets_repository_path, "calib-3_stereo_from_JY");
603650

604651
std::vector<std::vector<cv::Point2d> > leftPoints(n_images);
605652
std::vector<std::vector<cv::Point2d> > rightPoints(n_images);
@@ -666,7 +713,7 @@ TEST_F(fisheyeTest, stereoCalibrateFixIntrinsic)
666713
{
667714
const int n_images = 34;
668715

669-
const std::string folder =combine(datasets_repository_path, "calib-3_stereo_from_JY");
716+
const std::string folder = combine(datasets_repository_path, "calib-3_stereo_from_JY");
670717

671718
std::vector<std::vector<cv::Point2d> > leftPoints(n_images);
672719
std::vector<std::vector<cv::Point2d> > rightPoints(n_images);
@@ -822,6 +869,7 @@ const cv::Matx33d fisheyeTest::K(558.478087865323, 0, 620.45851536
822869

823870
const cv::Vec4d fisheyeTest::D(-0.0014613319981768, -0.00329861110580401, 0.00605760088590183, -0.00374209380722371);
824871

872+
825873
const cv::Matx33d fisheyeTest::R ( 9.9756700084424932e-01, 6.9698277640183867e-02, 1.4929569991321144e-03,
826874
-6.9711825162322980e-02, 9.9748249845531767e-01, 1.2997180766418455e-02,
827875
-5.8331736398316541e-04,-1.3069635393884985e-02, 9.9991441852366736e-01);

0 commit comments

Comments
 (0)