Repository: opencv
Issue: opencv-python cv2.cvtColor memory leak when executed within a threading.Thread
Labels: category: python bindings,category: imgproc,needs investigation

<!--
If you have a question rather than reporting a bug please go to https://forum.opencv.org where you get much faster responses.
If you need further assistance please read [How To Contribute](https://github.com/opencv/opencv/wiki/How_to_contribute).

This is a template helping you to create an issue which can be processed as quickly as possible. This is the bug reporting section for the OpenCV library.
-->

##### System information (version)
- opencv-python => 4.5.5.64
- Operating System / Platform => macOS 12.1

##### Detailed description

I use cv2.cvtColor(img_bgr, cv2.BGR2RGB) to transform image from BGR to RGB. When I call this for multiple times (say >10000 times) within a single thread, the memory consumption is fine. But then when I wrap this into a function called by a python threading.Thread, there are memory leak. I guess there is a bug somewhere in the implementation of cv2.cvtColor, because if I instead use np.flip(img_bgr, 2) to convert BGR2RGB in the same python threading.Thread scenario, there memory footprint looks much better. See below.

##### Steps to reproduce
```
def read_and_convert_npflip(image_file):
    img_bgr = cv2.imread(image_file)
    img_rgb = np.flip(img_bgr, 2)

def read_and_convert_cvtColor(image_file):
    img_bgr = cv2.imread(image_file)
    img_rgb = cv2.cvtColor(img_bgr, cv2.BGR2RGB)

for i in range(100000):
    image_file = "path_to_image.jpg"
    t = threading.Thread(target=read_and_convert_cvtColor, kwargs={'image_file': image_file})
    t.start()
    t.join()
    if i % 1000 == 0:
        rss_print(i)
```

and the console output is like:
rss=59.43359375 MB, iteration=0
rss=60.61328125 MB, iteration=1000
rss=60.9921875 MB, iteration=2000
rss=61.24609375 MB, iteration=3000
rss=61.74609375 MB, iteration=4000
rss=61.96484375 MB, iteration=5000
rss=62.2578125 MB, iteration=6000
rss=62.53515625 MB, iteration=7000
rss=62.74609375 MB, iteration=8000
rss=63.06640625 MB, iteration=9000
rss=63.29296875 MB, iteration=10000
rss=63.5078125 MB, iteration=11000
rss=63.72265625 MB, iteration=12000
rss=63.93359375 MB, iteration=13000
rss=64.1484375 MB, iteration=14000
rss=64.359375 MB, iteration=15000
rss=64.57421875 MB, iteration=16000
rss=64.9296875 MB, iteration=17000
rss=65.1484375 MB, iteration=18000
rss=65.37890625 MB, iteration=19000
rss=65.61328125 MB, iteration=20000

But if I change the threading target to read_and_convert_npflip:
rss=57.640625 MB, iteration=0
rss=58.0 MB, iteration=1000
rss=58.0078125 MB, iteration=2000
rss=58.109375 MB, iteration=3000
rss=58.109375 MB, iteration=4000
rss=58.22265625 MB, iteration=5000
rss=58.22265625 MB, iteration=6000
rss=58.31640625 MB, iteration=7000
rss=58.31640625 MB, iteration=8000
rss=58.31640625 MB, iteration=9000
rss=58.31640625 MB, iteration=10000
rss=58.390625 MB, iteration=11000
rss=58.41015625 MB, iteration=12000
rss=58.4140625 MB, iteration=13000
rss=58.48828125 MB, iteration=14000
rss=58.5 MB, iteration=15000
rss=58.5 MB, iteration=16000
rss=58.5 MB, iteration=17000
rss=58.5 MB, iteration=18000
rss=58.5 MB, iteration=19000
rss=58.5 MB, iteration=20000
rss=58.5 MB, iteration=21000
rss=58.5 MB, iteration=22000
rss=58.5 MB, iteration=23000
rss=58.5 MB, iteration=24000
rss=58.5 MB, iteration=25000
rss=58.5 MB, iteration=26000