Results 1 to 5 of 5

Thread: Multithreaded opencv video processing Qt/C++

  1. #1

    Default Multithreaded opencv video processing Qt/C++

    Hope your are doing good ! I'm strugguling from yesterday on how to implement a multithreaded video processing program with opencv.

    I understand how threads actually work, how to use a simple mutex etc...But when it comes to implementation, i'm completely lost.

    My goal is to create an interface with Qt and display two labels, one showing original video feed and the other showing the processed one, each one handeled by a thread.

    For now i'm just trying to get on thread to display images, but i really struggle with it.

    Here is what i've done so far :

    CaptureThread a class inheriting from QThread : It is supposed to handle the start of cam capture etc...

    Qt Code:
    1. #include <QThread>
    2.  
    3. #include <opencv2/opencv.hpp>
    4. #include <opencv2/highgui/highgui.hpp>
    5. #include <opencv2/core/core.hpp>
    6. #include <opencv2/imgproc/imgproc.hpp>
    7. #include "opencv2/videoio.hpp"
    8.  
    9. #include <QTimer>
    10.  
    11. class CaptureThread : public QThread
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit CaptureThread(QObject *parent);
    17.  
    18.  
    19. protected:
    20. void run();
    21.  
    22. signals:
    23. void frameCaptured(cv::Mat);
    24.  
    25. public slots:
    26. void captureFrame();
    27.  
    28. private:
    29. QTimer* tmrTimer;
    30. cv::VideoCapture capWebam;
    31. cv::Mat capturedFrame;
    32.  
    33. };
    34.  
    35. #endif // CAPTURETHREAD_H
    To copy to clipboard, switch view to plain text mode 


    And this is its implementation :

    Qt Code:
    1. #include "capturethread.h"
    2. #include <QDebug>
    3.  
    4. CaptureThread::CaptureThread(QObject* parent):QThread(parent)
    5. {
    6. capWebam.open(0);
    7. }
    8.  
    9. void CaptureThread::run()
    10. {
    11. tmrTimer = new QTimer(this);
    12. QObject::connect(tmrTimer, SIGNAL(timeout()), this, SLOT(captureFrame()));
    13.  
    14. tmrTimer->start(10);
    15. exec();
    16. }
    17.  
    18. void CaptureThread::captureFrame()
    19. {
    20. if(capWebam.isOpened()){
    21. capWebam.read(capturedFrame);
    22.  
    23. emit frameCaptured(capturedFrame);
    24. }
    25.  
    26. }
    To copy to clipboard, switch view to plain text mode 

    MainWindow use to display the camera feed etc...

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <opencv2/opencv.hpp>
    6. #include <opencv2/highgui/highgui.hpp>
    7. #include <opencv2/core/core.hpp>
    8. #include <opencv2/imgproc/imgproc.hpp>
    9. #include "opencv2/videoio.hpp"
    10. #include <capturethread.h>
    11.  
    12. #include <QTimer>
    13.  
    14. namespace Ui {
    15. class MainWindow;
    16. }
    17.  
    18. class MainWindow : public QMainWindow
    19. {
    20. Q_OBJECT
    21.  
    22. public:
    23. explicit MainWindow(QWidget *parent = 0);
    24. ~MainWindow();
    25.  
    26. public slots:
    27. void pricessFrameAndUpdateGUI(cv::Mat matOriginal);
    28.  
    29. private slots:
    30. void on_button_clicked();
    31.  
    32. private:
    33. Ui::MainWindow *ui;
    34. QImage toGrayscale(QImage image);
    35.  
    36. cv::VideoCapture capWebcam;
    37. cv::Mat matOriginal;
    38. cv::Mat matProcessed;
    39.  
    40. QImage qimgOriginal;
    41. QImage qimgProcessed;
    42.  
    43. QTimer* tmrTimer;
    44.  
    45. CaptureThread* cpThread;
    46. };
    47.  
    48. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    its implementaion

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QtCore>
    4. #include <cv.h>
    5. #include <QColor>
    6. #include <opencv/highgui.h>
    7.  
    8. MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11.  
    12. cpThread = new CaptureThread(this);
    13. QObject::connect(cpThread, SIGNAL(frameCaptured(cv::Mat)),this, SLOT(pricessFrameAndUpdateGUI(cv::Mat)));
    14. cpThread->start();
    15.  
    16. }
    17.  
    18. MainWindow::~MainWindow()
    19. {
    20. delete ui;
    21. }
    22.  
    23. void MainWindow::pricessFrameAndUpdateGUI(cv::Mat matOriginal)
    24. {
    25.  
    26.  
    27. cv::Canny(matOriginal, matProcessed, 100, 300);
    28.  
    29. cv::cvtColor(matOriginal, matOriginal, CV_BGR2RGB);
    30. QImage qimgOriginal((uchar*)matOriginal.data, matOriginal.cols, matOriginal.rows, matOriginal.step, QImage::Format_RGB888);
    31. QImage qimgProcessed((uchar*)matProcessed.data, matProcessed.cols, matProcessed.rows, matProcessed.step,QImage::Format_Indexed8);
    32.  
    33. ui->original->setPixmap(QPixmap::fromImage(qimgOriginal));
    34. ui->modified->setPixmap(QPixmap::fromImage(qimgProcessed));
    35.  
    36. }
    To copy to clipboard, switch view to plain text mode 

    After compiling and executing the program i get this error :


    Qt Code:
    1. Starting /media/wassim/BLAZER/Workspace/CPP/build-firstCV-Desktop_Qt_5_3_GCC_64bit-Debug/firstCV...
    2.  
    3. VIDEOIO ERROR: V4L/V4L2: VIDIOC_S_CROP
    4.  
    5. QObject: Cannot create children for a parent that is in a different thread.
    6.  
    7. (Parent is CaptureThread(0xc02be0), parent's thread is QThread(0xae82c0), current thread is CaptureThread(0xc02be0)
    8.  
    9. The program has unexpectedly finished.
    10.  
    11. /media/wassim/BLAZER/Workspace/CPP/build-firstCV-Desktop_Qt_5_3_GCC_64bit-Debug/firstCV crashed
    To copy to clipboard, switch view to plain text mode 

    Thanks you guys for your help !

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Multithreaded opencv video processing Qt/C++

    The CaptureThread object is created in the main program thread. The run() function is executed in a different thread and tries to create a new QTimer parented to the CaptureThread causing the error you see. You might want to use the worker object arrangement you see first in the QThread docs. It is more fully explained here.

  3. #3

    Default Re: Multithreaded opencv video processing Qt/C++

    Thank for your answer !
    I will try that and keep you in touch !

  4. #4

    Default Re: Multithreaded opencv video processing Qt/C++

    Thank your it worked very well !

  5. #5
    Join Date
    Oct 2015
    Posts
    2
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Multithreaded opencv video processing Qt/C++

    where did you initialize your timer please

Similar Threads

  1. Using concurrency for video processing
    By ccbaci in forum Qt Programming
    Replies: 0
    Last Post: 4th September 2013, 07:51
  2. Replies: 3
    Last Post: 4th January 2013, 06:40
  3. Qt Gui with OpenCV video and buttons
    By marcusbarnet in forum Qt Programming
    Replies: 3
    Last Post: 20th May 2011, 08:48
  4. Replies: 0
    Last Post: 17th December 2010, 15:55
  5. Video processing library integration Qt4
    By tpieciak in forum Qt Programming
    Replies: 0
    Last Post: 5th December 2009, 17:37

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.