Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Assuming a simple parallel scheme as in your question, in my opinion you should not observe such a framerate drop.

I see multiple flaws in your code:

  • usage of deprecated C API as pointed by berak
  • a mutex is missing even if in this simple code it should not matter
  • you should not time the grabbing but rather the whole acquisition as the camera acquisition should be done in background and each new frame should be stored in a buffer (if I am not wrong)

With the following code, I observe the same framerate with or whitout a parallel thread launched:

#include <iostream>
#include <thread>
#include <mutex>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

mutex quit_mutex;
bool quit = false;

void thread_callback() {
  while (true) {
    lock_guard<mutex> lock(quit_mutex);
    if (quit)
      break;
  }
}


int main(int argc, char *argv[]) {
  VideoCapture capture(0);
  capture.set(CAP_PROP_FRAME_WIDTH, 1280);
  capture.set(CAP_PROP_FRAME_HEIGHT, 720);

  if (!capture.isOpened()) {
    cerr << "Cannot open camera!" << endl;
    return 0;
  }

  thread th;
  bool launch_thread = argc > 1 && atoi(argv[1]);
  if (launch_thread) {
    th = thread(thread_callback);
  }

  Mat frame;
  double t = (double) getTickCount();
  int iterations = 0;
  while (true) {
    capture >> frame;
    imshow("Camera", frame);

    int c = waitKey(30) & 0xFF; // & 0xFF should be useless with OpenCV >= 3.2
    iterations++;
    if (c == 27)
      break;
  }
  t = ((double) getTickCount() - t) / getTickFrequency();
  cout << "Mean capture time: " << iterations / t << " FPS on " << iterations << " iterations" << endl;

  if (launch_thread) {
    //Set quit to true
    unique_lock<mutex> lock(quit_mutex);
    quit = true;
    lock.unlock();
    th.join();
  }

  return 0;
}

To enable the thread launching, you have to pass as argument 1 to the executable.

Some additional links related to the subject:

Assuming a simple parallel scheme as in your question, in my opinion you should not observe such a framerate drop.drop on a modern CPU.

I see multiple flaws in your code:

  • usage of deprecated C API as pointed by berak
  • a mutex is missing even if in this simple code it should not matter
  • you should not time the grabbing but rather the whole acquisition as the camera acquisition should be done in background and each new frame should be stored in a buffer (if I am not wrong)

With the following code, I observe the same framerate with or whitout a parallel thread launched:

#include <iostream>
#include <thread>
#include <mutex>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

mutex quit_mutex;
bool quit = false;

void thread_callback() {
  while (true) {
    lock_guard<mutex> lock(quit_mutex);
    if (quit)
      break;
  }
}


int main(int argc, char *argv[]) {
  VideoCapture capture(0);
  capture.set(CAP_PROP_FRAME_WIDTH, 1280);
  capture.set(CAP_PROP_FRAME_HEIGHT, 720);

  if (!capture.isOpened()) {
    cerr << "Cannot open camera!" << endl;
    return 0;
  }

  thread th;
  bool launch_thread = argc > 1 && atoi(argv[1]);
  if (launch_thread) {
    th = thread(thread_callback);
  }

  Mat frame;
  double t = (double) getTickCount();
  int iterations = 0;
  while (true) {
    capture >> frame;
    imshow("Camera", frame);

    int c = waitKey(30) & 0xFF; // & 0xFF should be useless with OpenCV >= 3.2
    iterations++;
    if (c == 27)
      break;
  }
  t = ((double) getTickCount() - t) / getTickFrequency();
  cout << "Mean capture time: " << iterations / t << " FPS on " << iterations << " iterations" << endl;

  if (launch_thread) {
    //Set quit to true
    unique_lock<mutex> lock(quit_mutex);
    quit = true;
    lock.unlock();
    th.join();
  }

  return 0;
}

To enable the thread launching, you have to pass as argument 1 to the executable.

Some additional links related to the subject: