From 853f7ff904b77f234aeac1a0edc48dc6756738a0 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Mon, 26 Feb 2024 12:12:53 +0300 Subject: [PATCH] Merge pull request #25090 from MaximSmolskiy:compensate-edge-length-in-ChessBoardDetector-generateQuads-attempt-2 Compensate edge length in ChessBoardDetector::generateQuads (attempt 2) #25090 ### Pull Request Readiness Checklist New attempt for #24833, which was reverted as #25036. Locally I fixed `Calib3d_StereoCalibrate_CPP.regression` test by corners refinement using `cornerSubPix` function See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake --- modules/calib3d/src/calibinit.cpp | 11 +++++++---- modules/calib3d/test/test_cameracalibration.cpp | 6 ++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/modules/calib3d/src/calibinit.cpp b/modules/calib3d/src/calibinit.cpp index 4b57aad1fe..ea5e5023b9 100644 --- a/modules/calib3d/src/calibinit.cpp +++ b/modules/calib3d/src/calibinit.cpp @@ -234,7 +234,7 @@ public: all_quads_count = 0; } - void generateQuads(const cv::Mat& image_, int flags); + void generateQuads(const cv::Mat& image_, int flags, int dilations); bool processQuads(std::vector& out_corners, int &prev_sqr_size); @@ -547,7 +547,7 @@ bool findChessboardCorners(InputArray image_, Size pattern_size, rectangle( thresh_img_new, Point(0,0), Point(thresh_img_new.cols-1, thresh_img_new.rows-1), Scalar(255,255,255), 3, LINE_8); detector.reset(); - detector.generateQuads(thresh_img_new, flags); + detector.generateQuads(thresh_img_new, flags, dilations); DPRINTF("Quad count: %d/%d", detector.all_quads_count, (pattern_size.width/2+1)*(pattern_size.height/2+1)); SHOW_QUADS("New quads", thresh_img_new, &detector.all_quads[0], detector.all_quads_count); if (detector.processQuads(out_corners, prev_sqr_size)) @@ -612,7 +612,7 @@ bool findChessboardCorners(InputArray image_, Size pattern_size, rectangle( thresh_img, Point(0,0), Point(thresh_img.cols-1, thresh_img.rows-1), Scalar(255,255,255), 3, LINE_8); detector.reset(); - detector.generateQuads(thresh_img, flags); + detector.generateQuads(thresh_img, flags, dilations); DPRINTF("Quad count: %d/%d", detector.all_quads_count, (pattern_size.width/2+1)*(pattern_size.height/2+1)); SHOW_QUADS("Old quads", thresh_img, &detector.all_quads[0], detector.all_quads_count); if (detector.processQuads(out_corners, prev_sqr_size)) @@ -1755,7 +1755,7 @@ void ChessBoardDetector::findQuadNeighbors() // returns corners in clockwise order // corners don't necessarily start at same position on quad (e.g., // top left corner) -void ChessBoardDetector::generateQuads(const cv::Mat& image_, int flags) +void ChessBoardDetector::generateQuads(const cv::Mat& image_, int flags, int dilations) { binarized_image = image_; // save for debug purposes @@ -1880,6 +1880,9 @@ void ChessBoardDetector::generateQuads(const cv::Mat& image_, int flags) float d = normL2Sqr(q.corners[i]->pt - q.corners[(i+1)&3]->pt); q.edge_len = std::min(q.edge_len, d); } + + const int edge_len_compensation = 2 * dilations; + q.edge_len += 2 * sqrt(q.edge_len) * edge_len_compensation + edge_len_compensation * edge_len_compensation; } all_quads_count = quad_count; diff --git a/modules/calib3d/test/test_cameracalibration.cpp b/modules/calib3d/test/test_cameracalibration.cpp index f5e80bbd28..7668d74c48 100644 --- a/modules/calib3d/test/test_cameracalibration.cpp +++ b/modules/calib3d/test/test_cameracalibration.cpp @@ -1388,8 +1388,8 @@ void CV_StereoCalibrationTest::run( int ) for( int i = 0; i < nframes; i++ ) { - Mat left = imread(imglist[i*2]); - Mat right = imread(imglist[i*2+1]); + Mat left = imread(imglist[i*2], IMREAD_GRAYSCALE); + Mat right = imread(imglist[i*2+1], IMREAD_GRAYSCALE); if(left.empty() || right.empty()) { ts->printf( cvtest::TS::LOG, "Can not load images %s and %s, testcase %d\n", @@ -1400,6 +1400,8 @@ void CV_StereoCalibrationTest::run( int ) imgsize = left.size(); bool found1 = findChessboardCorners(left, patternSize, imgpt1[i]); bool found2 = findChessboardCorners(right, patternSize, imgpt2[i]); + cornerSubPix(left, imgpt1[i], Size(5, 5), Size(-1, -1), TermCriteria(TermCriteria::EPS | TermCriteria::MAX_ITER, 30, 0.1)); + cornerSubPix(right, imgpt2[i], Size(5, 5), Size(-1, -1), TermCriteria(TermCriteria::EPS | TermCriteria::MAX_ITER, 30, 0.1)); if(!found1 || !found2) { ts->printf( cvtest::TS::LOG, "The function could not detect boards (%d x %d) on the images %s and %s, testcase %d\n",