From ff9aeaceb0a8abede5fc5189f2551712037db9ef Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Wed, 27 Mar 2024 15:34:54 +0300 Subject: [PATCH] Merge pull request #25177 from MaximSmolskiy:speed-up-adaptive-image-binary-threshold-in-findChessboardCorners Speed up adaptive threshold in findChessboardCorners #25177 ### Pull Request Readiness Checklist If `block_size` hasn't been changed between iterations for same `k`, then all `adaptiveThreshold` arguments will be same and we can reuse result from previous iteration. I tested this PR with benchmark ``` python3 objdetect_benchmark.py --configuration=generate_run --board_x=7 --path=res_chessboard --synthetic_object=chessboard ``` PR speed up chessboards detection by `7.5/17%` without any changes in detected chessboards number: ``` cell_img_size = 100 (default) before category detected chessboard total detected chessboard total chessboard average detected error chessboard all 0.904167 13020 14400 0.600512 Total detected time: 107.27875600000003 sec after category detected chessboard total detected chessboard total chessboard average detected error chessboard all 0.904167 13020 14400 0.600512 Total detected time: 99.0223499999999 sec ---------------------------------------------------------------------------------------------------------------------------------------------- cell_img_size = 10 before category detected chessboard total detected chessboard total chessboard average detected error chessboard all 0.539792 7773 14400 4.209964 Total detected time: 2.989205999999999 sec after category detected chessboard total detected chessboard total chessboard average detected error chessboard all 0.539792 7773 14400 4.209964 Total detected time: 2.4802350000000013 sec ``` 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 - [ ] 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 | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/modules/calib3d/src/calibinit.cpp b/modules/calib3d/src/calibinit.cpp index 7e9b87ba5d..37c982a4f9 100644 --- a/modules/calib3d/src/calibinit.cpp +++ b/modules/calib3d/src/calibinit.cpp @@ -583,8 +583,10 @@ bool findChessboardCorners(InputArray image_, Size pattern_size, } //if flag CALIB_CB_ADAPTIVE_THRESH is not set it doesn't make sense to iterate over k int max_k = useAdaptive ? 6 : 1; + Mat prev_thresh_img; for (int k = 0; k < max_k && !found; k++) { + int prev_block_size = -1; for (int dilations = min_dilations; dilations <= max_dilations; dilations++) { // convert the input grayscale image to binary (black-n-white) @@ -595,9 +597,18 @@ bool findChessboardCorners(InputArray image_, Size pattern_size, : prev_sqr_size * 2); block_size = block_size | 1; // convert to binary - adaptiveThreshold( img, thresh_img, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, block_size, (k/2)*5 ); - dilate( thresh_img, thresh_img, Mat(), Point(-1, -1), dilations ); - + if (block_size != prev_block_size) + { + adaptiveThreshold( img, thresh_img, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, block_size, (k/2)*5 ); + dilate( thresh_img, thresh_img, Mat(), Point(-1, -1), dilations ); + thresh_img.copyTo(prev_thresh_img); + } + else if (dilations > 0) + { + dilate( prev_thresh_img, prev_thresh_img, Mat(), Point(-1, -1), 1 ); + prev_thresh_img.copyTo(thresh_img); + } + prev_block_size = block_size; } else {