CV::Resize Performance degrading in multithreaded application

Hey everyone,

I’m currently writing a program which continuously grabs images from 4+ cameras, processes and then stores them. To speed the program up, I splitted the processing for each camera into it’s own thread. This works fine, until I begin to resize the images with cv::resize. From this point on, performance degrades to almost the point as if I would be running everything in a single thread again.

I wrote a minimal sample to check if it truly is a issue with OpenCV.

I load 4 images from disk
Then I resize all images with

cv::resize(picture1, pictureRes1, cv::Size(5000, 5000));

This takes 1296ms

Then I create 4 worker threads,with this code (4x):

Worker* worker1 = new Worker(picture1);
std::thread workerThread1(&Worker::RunWorker, worker1);

The code in the worker is:

void Worker::RunWorker()
{
	cv::resize(pic, pictureResized, cv::Size(5000, 5000));
}

This takes ~1800ms

So in this case, the multithreaded code is actually slower than running everything on a single thread. I also replaced the cv::resize with a sleep_for instruction, just as a sanity check.
I let the program sleep for 200ms in the single thread and 50ms in each thread in the multithreaded part.

The results are:
211 ms for Single Threaded load
64 ms for Multi Threaded load

So I guess it’s not a problem of my threading itself.

The full code of the sample can be found here:

Does anybody know how I could handle this task better in OpenCV, or maybe has a hint what might go wrong here?

Thank you very much in advance! :slight_smile:

resize() is multithreaded already. it uses all cores.

you can’t multithread your work to infinity. the same amount of work still needs to be done by the same number of cpu cores. threading doesn’t make the work go away. it redistributes the work from time into space (cores).

if you’re saying resizing four images one after the other goes faster than starting all resizes at the same time… then yes, now you’ve got all those threads fighting over the cores, wasting time.

you could see about switching off or reducing the multithreading used by resize() calls.

try cv::setNumThreads(0);

https://docs.opencv.org/4.x/db/de0/group__core__utils.html#gae78625c3c2aa9e0b83ed31b73c6549c0