jeudi 28 février 2019

Opencv Multithreaded Image Streams

I have several cameras that I'm capturing images for in real-time with a thread for each camera.

I have a pub sub format right now so the saving isn't in real time but the frame capturing is at the rated frame rate for the camera. I decided to separate the image display from the image capture so that the frame capturing is not (potentially) locked by the io. I'm wondering whether opencv's imshow can only be accessed by a single thread at any given time - can I show multiple image streams? At present, I'm getting a seg fault at:

void h::viewImage(std::shared_ptr<CamHandle> cam, //this is a thread
                  bool &cancellation_token,
                  std::shared_ptr<queue<cv::Mat> > q,
                  std::mutex &cancellation_token_mutex,
                  std::shared_ptr<interThread_signalling<bool> > saveSignal) {

  std::string name = "Camera@" + cam->getIPAsString();
  cv::namedWindow(name); //I'm getting a seg fault here
  cv::Size windowSize(cam->width, cam->height);
  cv::Mat im;
  while (!cancellation_token) {
    q->peek(im); //There is a queue of these images from the frame capture thread

    if (cam->show_images == "true") {
      if (!im.empty()) {
        cv::imshow(name, im);
      } 
  }
}

This is the interThread_signalling class:

#include <queue>
#include <mutex>
#include <condition_variable>

template<typename T>
class interThread_signalling {
 protected:
  T signal;
  mutable std::mutex the_mutex;
  std::condition_variable the_condition_variable;

 public:
  interThread_signalling() = default;

  ~interThread_signalling() = default;

  void modify(const T &data) {
    std::unique_lock<std::mutex> lock(the_mutex);
    this->signal = data;
    lock.unlock();
    the_condition_variable.notify_one();
  }

  void peek(T &peeked_value) {
    std::unique_lock<std::mutex> lock(the_mutex);
    if (this->signal == NULL) {
      return;
    } //this will always lag behind by one
    peeked_value = this->signal;
  }

};

Should I be calling the namedWindow command in the main thread - once my program determines how many cameras there are, should I create n namedWindows? How should I be doing this?

Aucun commentaire:

Enregistrer un commentaire