Skip to content

Fix rectangle rounding in cvGetOptimalNewCameraMatrix. #24927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions modules/calib3d/src/calibration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2808,9 +2808,6 @@ void cvGetOptimalNewCameraMatrix( const CvMat* cameraMatrix, const CvMat* distCo
(double)((inner.y - cy0)*s + cy),
(double)(inner.width*s),
(double)(inner.height*s));
cv::Rect r(cvCeil(inner.x), cvCeil(inner.y), cvFloor(inner.width), cvFloor(inner.height));
r &= cv::Rect(0, 0, newImgSize.width, newImgSize.height);
*validPixROI = cvRect(r);
}
}
else
Expand Down Expand Up @@ -2840,11 +2837,39 @@ void cvGetOptimalNewCameraMatrix( const CvMat* cameraMatrix, const CvMat* distCo
if( validPixROI )
{
icvGetRectangles( cameraMatrix, distCoeffs, 0, &matM, imgSize, inner, outer );
cv::Rect r = inner;
r &= cv::Rect(0, 0, newImgSize.width, newImgSize.height);
*validPixROI = cvRect(r);
}
}
if( validPixROI )
{
// This tolerance defines how incomplete a pixel can be to still be considered complete.
// It has been chosen to get Calib3d_GetOptimalNewCameraMatrixNoDistortion.accuracy to pass.
constexpr double kTolerance = 1e-12;
Point p1, p2;
if( std::abs(inner.x-cvRound(inner.x))<kTolerance ) {
p1.x = cvRound(inner.x);
} else {
p1.x = cvCeil(inner.x);
}
if( std::abs(inner.y-cvRound(inner.y))<kTolerance ) {
p1.y = cvRound(inner.y);
} else {
p1.y = cvCeil(inner.y);
}
if( std::abs(inner.x+inner.width-cvRound(inner.x+inner.width))<kTolerance ) {
p2.x = cvRound(inner.x+inner.width);
} else {
p2.x = cvFloor(inner.x+inner.width);
}
if( std::abs(inner.y+inner.height-cvRound(inner.y+inner.height))<kTolerance ) {
p2.y = cvRound(inner.y+inner.height);
} else {
p2.y = cvCeil(inner.y+inner.height);
}
// +1 to make sure p2 is included.
cv::Rect r(p1, p2+cv::Point(1,1));
r &= cv::Rect(0, 0, newImgSize.width, newImgSize.height);
*validPixROI = cvRect(r);
}

cvConvert(&matM, newCameraMatrix);
}
Expand Down
11 changes: 10 additions & 1 deletion modules/calib3d/test/test_undistort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ class CV_GetOptimalNewCameraMatrixNoDistortionTest : public cvtest::ArrayTest
cv::Mat camera_mat;
cv::Mat distortion_coeffs;
cv::Mat new_camera_mat;
cv::Rect validPixROI;

cv::Size img_size;
double alpha;
Expand All @@ -187,6 +188,8 @@ CV_GetOptimalNewCameraMatrixNoDistortionTest::CV_GetOptimalNewCameraMatrixNoDist
test_array[INPUT].push_back(NULL); // camera_mat
test_array[INPUT].push_back(NULL); // distortion_coeffs
test_array[OUTPUT].push_back(NULL); // new_camera_mat
test_array[OUTPUT].push_back(NULL); // validPixROI
test_array[REF_OUTPUT].push_back(NULL);
test_array[REF_OUTPUT].push_back(NULL);

alpha = 0.0;
Expand All @@ -199,7 +202,9 @@ void CV_GetOptimalNewCameraMatrixNoDistortionTest::get_test_array_types_and_size
cvtest::ArrayTest::get_test_array_types_and_sizes(test_case_idx, sizes, types);
RNG& rng = ts->get_rng();
matrix_type = types[INPUT][0] = types[INPUT][1] = types[OUTPUT][0] = types[REF_OUTPUT][0] = cvtest::randInt(rng)%2 ? CV_64F : CV_32F;
types[OUTPUT][1] = types[REF_OUTPUT][1] = CV_32S;
sizes[INPUT][0] = sizes[OUTPUT][0] = sizes[REF_OUTPUT][0] = cvSize(3,3);
sizes[OUTPUT][1] = sizes[REF_OUTPUT][1] = Size(4,1);
sizes[INPUT][1] = cvSize(1,4);
}

Expand Down Expand Up @@ -240,17 +245,21 @@ int CV_GetOptimalNewCameraMatrixNoDistortionTest::prepare_test_case(int test_cas

void CV_GetOptimalNewCameraMatrixNoDistortionTest::run_func()
{
new_camera_mat = cv::getOptimalNewCameraMatrix(camera_mat, distortion_coeffs, img_size, alpha, img_size, NULL, center_principal_point);
new_camera_mat = cv::getOptimalNewCameraMatrix(camera_mat, distortion_coeffs, img_size, alpha, img_size, &validPixROI, center_principal_point);
}

void CV_GetOptimalNewCameraMatrixNoDistortionTest::prepare_to_validation(int /*test_case_idx*/)
{
const Mat& src = test_mat[INPUT][0];
Mat& dst = test_mat[REF_OUTPUT][0];
cvtest::copy(src, dst);
Mat& ref_validPixROI = test_mat[REF_OUTPUT][1];
cvtest::copy(cv::Mat(cv::Vec4i(0, 0, img_size.width, img_size.height)), ref_validPixROI);

Mat& output = test_mat[OUTPUT][0];
cvtest::convert(new_camera_mat, output, output.type());
Mat& output_validPixROI = test_mat[OUTPUT][1];
cvtest::copy(cv::Mat(cv::Vec4i(validPixROI.x, validPixROI.y, validPixROI.width, validPixROI.height)), output_validPixROI);
}

//---------
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy