Ask Your Question

OpenCV C++ Multithreading

asked 2016-06-27 11:22:51 -0500

SumeetB gravatar image

I made a opencv c++ program that does a lot of image processing on frames from a video feed and as such decided to implement multithreading to speed things up. However, because I am new to opencv and multithreading, my implementation has caused my image processing functions to produce erroneous results. My guess is that the thread in which image processing occurs cannot keep up with the thread that reads in each frame. Here is my code:

void CaptureFrames() {
  VideoCapture capture(0);
  if (!capture.isOpened()) {
    cout << "cannot open camera" << endl;

  while (true) {
    //CAMERAFRAME is a global Mat defined at the top of my program;

void ProcessFrames() {

  while (true) {

  Mat hsvFrame;
  Mat binMat;
  Mat grayFrame;
  Mat grayBinMat;

  if (!CAMERAFRAME.empty()) {
    //do image processing functions (these worked before implementing threads and are not causing errors)

    imshow("gray binary", grayBinMat);
    imshow("multithread test", CAMERAFRAME);

if (waitKey(1) >= 0) { break; }

int main(int argc, char** argv[]) {
  thread t1(CaptureFrames);
  thread t2(ProcessFrames);

  while(true) {
    if(waitKey(1) >= 0) {break;}

  return 0;

Any ideas on what I am doing wrong?

edit retag flag offensive close merge delete



first , your processing essentially has to wait for the camera to deliver a new frame.

trying to mutithread something sequential does not make any sense.

then, your code does not have any locks, also having gui calls in external threads is a NO-NO-NO.

please just don't.

berak gravatar imageberak ( 2016-06-27 23:19:48 -0500 )edit

@berak I was trying to implement something like this:

Except in c++ which I don't know how to do since I am new to opencv and multithreading. Also, if you would like to be helpful and show me how I can fix my mistakes, that would be much appreciated.

SumeetB gravatar imageSumeetB ( 2016-06-27 23:32:12 -0500 )edit

again, better forget the whole idea asap.

berak gravatar imageberak ( 2016-06-27 23:41:36 -0500 )edit

You can make a local copy of your image matrix and pass it to the ProcessFrames() function. That way the image matrix, which is global in this case will not be changed while ProcessFrames() is processing it. But then, fundamentally, multithreading sequential processes is a bad approach.

vishwanathkr gravatar imagevishwanathkr ( 2016-10-18 18:24:46 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-04-08 05:35:41 -0500

sagiz gravatar image

You can use the TBB parallel pipeline with OpenCV to do just that.

I have a post about using it with OpenCV here -

TBB has advanced multi threading patterns and tools - some of them used internally by OpenCV (if built with it), and some can be used externally, regardless if OpenCV was built with TBB support.

edit flag offensive delete link more


for learning purpose this link is not very good, if you have a simple example it would be nice, I don't understand what are you doing in that link:(

saeid_masumi gravatar imagesaeid_masumi ( 2018-01-21 09:17:28 -0500 )edit

I looked at pipeline but found in the documentation that if the consumer/processing filter isn't keeping up with the producing/grabbing it will actually cause the producer to wait for the processing to keep up. This depends on how you setup the number of "in flight" units in the pipeline. In my case I want to process the latest available frame to minimize latency and if the processing can't keep up I want it to drop frames. I created a TBB concurrent_queue and I have a producer thread which pushes images onto it and consumer thread that calls try_pop in a while loop to consume all the images until it gets to the "current" one.

Rusty gravatar imageRusty ( 2018-10-16 11:56:40 -0500 )edit

Question Tools



Asked: 2016-06-27 11:22:51 -0500

Seen: 16,398 times

Last updated: Jun 27 '16