Skip to content

Speed up adaptive threshold in findChessboardCorners#25177

Merged
asmorkalov merged 3 commits intoopencv:4.xfrom
MaximSmolskiy:speed-up-adaptive-image-binary-threshold-in-findChessboardCorners
Mar 27, 2024
Merged

Speed up adaptive threshold in findChessboardCorners#25177
asmorkalov merged 3 commits intoopencv:4.xfrom
MaximSmolskiy:speed-up-adaptive-image-binary-threshold-in-findChessboardCorners

Conversation

@MaximSmolskiy
Copy link
Copy Markdown
Contributor

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

  • I agree to contribute to the project under Apache 2 License.
  • 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
  • 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

@AleksandrPanov AleksandrPanov force-pushed the speed-up-adaptive-image-binary-threshold-in-findChessboardCorners branch from 6ba9592 to 41e71d4 Compare March 27, 2024 08:38
Copy link
Copy Markdown
Contributor

@AleksandrPanov AleksandrPanov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@MaximSmolskiy
Copy link
Copy Markdown
Contributor Author

@AleksandrPanov Moving int prev_block_size = -1; outside from loop on variable k should not be equivalent change. Variable k is used as argument in adaptiveThreshold function, so we shouldn't reuse result from different k - only for same k. This change decreased detected chessboards number

cell_img_size = 100 (default)

                                 category  detected chessboard  total detected chessboard  total chessboard  average detected error chessboard
                                      all             0.469306                       6758             14400                           4.178116

----------------------------------------------------------------------------------------------------------------------------------------------

cell_img_size = 10

                                 category  detected chessboard  total detected chessboard  total chessboard  average detected error chessboard
                                      all             0.895833                      12900             14400                           0.600768

@AleksandrPanov AleksandrPanov force-pushed the speed-up-adaptive-image-binary-threshold-in-findChessboardCorners branch from 41e71d4 to ba3fc90 Compare March 27, 2024 09:56
@AleksandrPanov
Copy link
Copy Markdown
Contributor

@AleksandrPanov Moving int prev_block_size = -1; outside from loop on variable k should not be equivalent change. Variable k is used as argument in adaptiveThreshold function, so we shouldn't reuse result from different k - only for same k. This change decreased detected chessboards number

cell_img_size = 100 (default)

                                 category  detected chessboard  total detected chessboard  total chessboard  average detected error chessboard
                                      all             0.469306                       6758             14400                           4.178116

----------------------------------------------------------------------------------------------------------------------------------------------

cell_img_size = 10

                                 category  detected chessboard  total detected chessboard  total chessboard  average detected error chessboard
                                      all             0.895833                      12900             14400                           0.600768

@MaximSmolskiy, thx, fixed

@AleksandrPanov
Copy link
Copy Markdown
Contributor

Now the results are correct:
--configuration=generate_run --board_x=7 --path=res_chessboard --synthetic_object=chessboard
image

--configuration=generate_run --board_x=7 --path=res_chessboard --synthetic_object=chessboard --cell_img_size=10
image

@asmorkalov asmorkalov merged commit ff9aeac into opencv:4.x Mar 27, 2024
@MaximSmolskiy MaximSmolskiy deleted the speed-up-adaptive-image-binary-threshold-in-findChessboardCorners branch March 27, 2024 12:55
@asmorkalov asmorkalov mentioned this pull request Apr 1, 2024
klatism pushed a commit to klatism/opencv that referenced this pull request May 17, 2024
…image-binary-threshold-in-findChessboardCorners

Speed up adaptive threshold in findChessboardCorners opencv#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
Tzila-m pushed a commit to Tzila-m/opencv that referenced this pull request Jan 29, 2026
…sboardCorners

This change fixes a regression introduced after PR opencv#25177, where certain adaptive
threshold and dilation combinations were unintentionally skipped during chessboard
detection.

The updated logic restores the missing block_size and dilation combinations that
were evaluated in OpenCV 4.8, without fully reverting the changes introduced later.

A regression test reproducing issue opencv#28028 is included.

Fixes opencv#28028
Related to opencv#25177
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants