Repository: opencv
Issue: Charuco detection has slowed down considerably since 4.6.0
Labels: bug,category: objdetect

### System Information

OpenCV Python version: 4.9.0.80
OS / Platform: MacOS on Apple Silicon (M2 Max) 
Python version: 3.11.6

I have seen the same behaviour in C++ going from 4.5.5 to 4.9.0.

### Detailed description

Charuco board detection in 4.9.0 and 4.8.0 is running 10-20 times slower versus 4.5.5 and 4.6.0. The big change here is that it moved out of opencv_contrib and into the main opencv repository, which included a slight change to the API.

My app was in C++ and I initially figured it was an issue with my compilation of OpenCV - but I was able to reproduce the problem in Python using OpenCV versions from PyPI.

I dived into the code and aside from a few structural changes in the migration from contrib, the code doing the bulk of the work is almost identical. In the newer version there is the addition of the `checkBoard()` function [here](https://github.com/opencv/opencv/blob/dad8af6b17f8e60d7b95a1203a1b4d22f56574cf/modules/objdetect/src/aruco/charuco_detector.cpp#L374) but I patched opencv 4.9.0 to remove it and it did not have a major affect on performance. As far as I can tell, all of the parameter default values remain the same (e.g. subpixel window size).

I also did some benchmarking of individual functions - for example the `filterCornersWithoutMinMarkers()` function is the last thing run and discards markers that don't meet the `minMarkers` parameter threshold. In 4.9.0 that was running 2-3 times slower than the entire charuco detection function in 4.5.5 - yet the code between the two versions is almost identical:

4.9.0: https://github.com/opencv/opencv/blob/dad8af6b17f8e60d7b95a1203a1b4d22f56574cf/modules/objdetect/src/aruco/charuco_detector.cpp#L272
4.5.5: https://github.com/opencv/opencv_contrib/blob/49e8f123ca08e76891856a1ecce491b62d08ba20/modules/aruco/src/charuco.cpp#L233

### Steps to reproduce

I have created a public repo that has Python code to benchmark charuco detection across multiple versions: https://github.com/jan-jachnik-oxa/opencv_charuco_version_benchmark

The output on my M2 Max Macbook:
```
Running tests on 4.5.5
Testing old api
Number of Aruco markers detected: 204
Number of Charuco corners detected: 368
Time to detect markers: 0.065684925
Time to detect charuco: 0.0057080125

Running tests on 4.6.0
Testing old api
Number of Aruco markers detected: 204
Number of Charuco corners detected: 368
Time to detect markers: 0.05975144583333334
Time to detect charuco: 0.0058611083333333334

Running tests on 4.8.1
Testing new api
Number of Aruco markers detected: 204
Number of Charuco corners detected: 368
Time to detect markers: 0.05999894569999999
Time to detect charuco: 0.1107731625

Running tests on 4.9.0
Testing new api
Number of Aruco markers detected: 204
Number of Charuco corners detected: 368
Time to detect markers: 0.061556029199999994
Time to detect charuco: 0.1175777584
```
I was unable to test 4.7.0 because it fails an assertion (which might be a bug).

### Issue submission checklist

- [X] I report the issue, it's not a question
- [X] I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
- [X] I updated to the latest OpenCV version and the issue is still there
- [X] There is reproducer code and related data files (videos, images, onnx, etc)