From 15815fb54d162bb682834f106ca0d23221ca2cfc Mon Sep 17 00:00:00 2001 From: Vincent Rabaud Date: Thu, 20 Jul 2023 17:54:42 +0200 Subject: [PATCH] Fix stereoRectify image boundaries. This should hav ebeen fixed with https://github.com/opencv/opencv/issues/23304 --- modules/calib3d/src/calibration.cpp | 8 ++-- .../calib3d/test/test_cameracalibration.cpp | 48 +++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/modules/calib3d/src/calibration.cpp b/modules/calib3d/src/calibration.cpp index 9e8c252cee..002394b917 100644 --- a/modules/calib3d/src/calibration.cpp +++ b/modules/calib3d/src/calibration.cpp @@ -2699,11 +2699,11 @@ void cvStereoRectify( const CvMat* _cameraMatrix1, const CvMat* _cameraMatrix2, if( alpha >= 0 ) { double s0 = std::max(std::max(std::max((double)cx1/(cx1_0 - inner1.x), (double)cy1/(cy1_0 - inner1.y)), - (double)(newImgSize.width - cx1)/(inner1.x + inner1.width - cx1_0)), - (double)(newImgSize.height - cy1)/(inner1.y + inner1.height - cy1_0)); + (double)(newImgSize.width - 1 - cx1)/(inner1.x + inner1.width - cx1_0)), + (double)(newImgSize.height - 1 - cy1)/(inner1.y + inner1.height - cy1_0)); s0 = std::max(std::max(std::max(std::max((double)cx2/(cx2_0 - inner2.x), (double)cy2/(cy2_0 - inner2.y)), - (double)(newImgSize.width - cx2)/(inner2.x + inner2.width - cx2_0)), - (double)(newImgSize.height - cy2)/(inner2.y + inner2.height - cy2_0)), + (double)(newImgSize.width - 1 - cx2)/(inner2.x + inner2.width - cx2_0)), + (double)(newImgSize.height - 1 - cy2)/(inner2.y + inner2.height - cy2_0)), s0); double s1 = std::min(std::min(std::min((double)cx1/(cx1_0 - outer1.x), (double)cy1/(cy1_0 - outer1.y)), diff --git a/modules/calib3d/test/test_cameracalibration.cpp b/modules/calib3d/test/test_cameracalibration.cpp index d2ab906fee..f5e80bbd28 100644 --- a/modules/calib3d/test/test_cameracalibration.cpp +++ b/modules/calib3d/test/test_cameracalibration.cpp @@ -2150,6 +2150,54 @@ TEST(Calib3d_StereoCalibrate, regression_11131) EXPECT_GE(roi2.area(), 400*300) << roi2; } +TEST(Calib3d_StereoCalibrate, regression_23305) +{ + const Matx33d M1( + 850, 0, 640, + 0, 850, 640, + 0, 0, 1 + ); + + const Matx34d P1_gold( + 850, 0, 640, 0, + 0, 850, 640, 0, + 0, 0, 1, 0 + ); + + const Matx33d M2( + 850, 0, 640, + 0, 850, 640, + 0, 0, 1 + ); + + const Matx34d P2_gold( + 850, 0, 640, -2*850, // correcponds to T(-2., 0., 0.) + 0, 850, 640, 0, + 0, 0, 1, 0 + ); + + const Matx D1(0, 0, 0, 0, 0); + const Matx D2(0, 0, 0, 0, 0); + + const Matx33d R( + 1., 0., 0., + 0., 1., 0., + 0., 0., 1. + ); + const Matx31d T(-2., 0., 0.); + + const Size imageSize(1280, 1280); + + Mat R1, R2, P1, P2, Q; + Rect roi1, roi2; + stereoRectify(M1, D1, M2, D2, imageSize, R, T, + R1, R2, P1, P2, Q, + CALIB_ZERO_DISPARITY, 0, imageSize, &roi1, &roi2); + + EXPECT_EQ(cv::norm(P1, P1_gold), 0.); + EXPECT_EQ(cv::norm(P2, P2_gold), 0.); +} + TEST(Calib3d_Triangulate, accuracy) { // the testcase from http://code.opencv.org/issues/4334