Repository: opencv
Issue: ArucoDetector detectMarkers Memory leak and severe slowdown in recent versions
Labels: bug,category: objdetect

### System Information

OpenCV 4.10.0.84
Windows 11 Home 24H2
Python 3.11.9

pip freeze:
numpy==1.26.4
opencv-python==4.10.0.84
psutil==6.1.1

### Detailed description

Using detectMarkers repeatedly on the same noisy large image results in memory leaks and severe slow downs of the detection especially in the most recent version of OpenCV.

Here's the output using OpenCV 4.10.0.84:
```
iteration 0 took 2.370551586151123 seconds. Total process memory: 123,441,152
iteration 1 took 3.1970834732055664 seconds. Total process memory: 131,985,408
iteration 2 took 3.3649799823760986 seconds. Total process memory: 136,482,816
iteration 3 took 3.5040178298950195 seconds. Total process memory: 1,671,176,192
iteration 4 took 4.196935415267944 seconds. Total process memory: 145,444,864
iteration 5 took 4.476127862930298 seconds. Total process memory: 2,117,165,056
iteration 6 took 5.6765806674957275 seconds. Total process memory: 2,338,840,576
iteration 7 took 7.461204767227173 seconds. Total process memory: 158,998,528
iteration 8 took 7.712129831314087 seconds. Total process memory: 2,753,626,112
iteration 9 took 8.872538089752197 seconds. Total process memory: 2,983,907,328
iteration 10 took 9.44568419456482 seconds. Total process memory: 168,304,640
```

Here's the same output using OpenCV 4.9.0.80.
There is still a memory leak but it's not nearly as bad
```
iteration 0 took 0.5849573612213135 seconds. Total process memory: 106,168,320
iteration 1 took 0.5812480449676514 seconds. Total process memory: 107,143,168
iteration 2 took 0.5951583385467529 seconds. Total process memory: 107,868,160
iteration 3 took 0.601895809173584 seconds. Total process memory: 108,466,176
iteration 4 took 0.5014839172363281 seconds. Total process memory: 228,802,560
iteration 5 took 0.5855703353881836 seconds. Total process memory: 109,469,696
iteration 6 took 0.5641584396362305 seconds. Total process memory: 257,597,440
iteration 7 took 0.5695486068725586 seconds. Total process memory: 263,557,120
iteration 8 took 0.592726469039917 seconds. Total process memory: 110,755,840
iteration 9 took 0.5882177352905273 seconds. Total process memory: 298,942,464
iteration 10 took 0.47766566276550293 seconds. Total process memory: 314,290,176
iteration 11 took 0.5535173416137695 seconds. Total process memory: 304,709,632
iteration 12 took 0.5782535076141357 seconds. Total process memory: 112,029,696
iteration 13 took 0.5441710948944092 seconds. Total process memory: 111,517,696
iteration 14 took 0.5778942108154297 seconds. Total process memory: 322,514,944
iteration 15 took 0.5481007099151611 seconds. Total process memory: 111,931,392
iteration 16 took 0.581120491027832 seconds. Total process memory: 332,570,624
iteration 17 took 0.5674891471862793 seconds. Total process memory: 331,341,824
iteration 18 took 0.5971369743347168 seconds. Total process memory: 112,955,392
iteration 19 took 0.585500955581665 seconds. Total process memory: 336,048,128
iteration 20 took 0.5637915134429932 seconds. Total process memory: 339,034,112
iteration 21 took 0.5227835178375244 seconds. Total process memory: 345,055,232
iteration 22 took 0.5924174785614014 seconds. Total process memory: 113,094,656
iteration 23 took 0.5959928035736084 seconds. Total process memory: 347,766,784
```

### Steps to reproduce

Run the following code to reproduce:
```
import cv2
import psutil
import time

params = cv2.aruco.DetectorParameters()
aruco_dict = cv2.aruco.getPredefinedDictionary(0)
detector = cv2.aruco.ArucoDetector(aruco_dict, params)

process = psutil.Process()
img = cv2.imread('noisy.jpg')
for i in range(100):
    b = time.time()
    detector.detectMarkers(img)
    print(f'iteration {i} took {time.time()-b} seconds. Total process memory: {process.memory_info().rss:,}')  # in bytes
    time.sleep(0.01)

```
![noisy](https://github.com/user-attachments/assets/2c2ebabf-3312-4f48-b2a0-5bb2bf3e6597)


### 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)