Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is OpenCv already threaded?

I want to use OpenCV in order to record a video and send it as a stream. I'm a beginner and I need to know if OpenCV blocks the main thread or is it threaded itself ?

I read OpenCV documentation (2.4.9) and I couldn't find any answer.

Thanks for reading.

like image 358
Napsters Desmars Avatar asked Oct 10 '14 19:10

Napsters Desmars


People also ask

Does OpenCV use threads?

If you use OpenCV library beware that it spawns multiple threads for image processing internally. For example, cv. VideoCapture() spawns multiple threads internally. General rule of thumb is, your app should be spawning that many threads as the cores it has available for processing.

Is OpenCV thread safe?

OpenCV is Thread Safe.

Is OpenCV parallel?

I known that OpenCV has parallelism capabilities in the form of multithreading.

What is OpenCV TBB?

Threading Building Blocks (TBB) is a C++ template library developed by Intel for writing software programs that take advantage of multi-core processors. OpenCV has a provided a simple API to take advantage of TBB.


2 Answers

OpenCV can spawn threads when you call a function. However, all the work is performed before control is returned to the calling thread. For a number of reasons, asynchronous processing would add a substantial extra bit of complexity. (Consider, for instance: How would your program know when the computation was done?) It would also introduce some undesired overhead if the program didn't need to be asynchronous.

You can do asynchronous processing yourself with a minimal amount of effort, though, with C++11's threading API.

like image 95
George Hilliard Avatar answered Sep 19 '22 13:09

George Hilliard


OpenCV can be built with OpenMP support to make the compute functions use all the available cores on your machine. It can be built also with OpenCL and CUDA. In addition it has sorts of SIMD optimization flags.

If you don't build it with such support it will run single threaded.

In either versions, calling an OpenCV function blocks the launcher thread until it computes all the operations. That is true even when offloading the computation to a GPU.

like image 26
VAndrei Avatar answered Sep 19 '22 13:09

VAndrei