Multithreading for OpenCV Functions with Qt Signal Slot

C++ multithreading with QT and event-based computer vision architecture

Imran Bangash
1 min readJan 1, 2021
image source [author]

Developing OpenCV based vision applications with QT open up possibilities of different powerful features such as QT Signals & Slots [1] and Qthread [2].

In the given example [ link ], you will see

  • Qthread for worker class. The main steps are listed here
1-Create your QObjects,
2-connect your signals,
3-create your QThread,
4-move your QObjects to the QThread and
5-start the thread.
  • Signal & Slot for cv::Mat structure. The signal/slot mechanisms will ensure that thread boundary is crossed properly and safely. Important to note! Register the type cv::Mat via Q_DECLARE_METATYP at the beginning of the constructor. Exact syntax is qRegisterMetaType<cv::Mat>(“cv::Mat”);

The console output and opencv imshow in my case is

Main thread id = 0x298
pipeline is opened
camera stream available!
Worker thread id = 0x3ad8

image source [author]

References:

  1. https://stackoverflow.com/questions/25289082/signal-slot-opencv-mat-over-different-threads-in-qt
  2. https://doc.qt.io/qt-5/thread-basics.html

--

--

Imran Bangash

Imran is a computer vision and AI enthusiast with a PhD in computer vision. Imran loves to share his experience with self-improvement and technology.