Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to properly show&process Multiple OpenCV stream on the UI ?
QtWS25 Last Chance

How to properly show&process Multiple OpenCV stream on the UI ?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RahibeMeryem
    wrote on last edited by
    #1

    I am trying to build a multi threaded application which will be (hopefully) capturing multiply camera stream and make some preprocess than show them on the UI .

    I successfully done for one stream. but when start the second stream the first thread stopping and second stream playing in its area. Than I start (by clicking button) again first one its start working but second one stopping ?

    How can I use multiply opencv stream in thread to show on UI ?

    Clearly I am missing something here...

    I attached camera_03 files. other camera files are identical.
    thanks

    I couldt figure it out:

    camera_03.:

    #ifndef CAMERA_03_H
    #define CAMERA_03_H
    
    #include <QObject>
    
    #include <opencv2/highgui.hpp>
    #include <opencv2/imgproc.hpp>
    #include <opencv2/core/core.hpp>
    
    using namespace std;
    using namespace cv;
    
    class Camera_03: public QObject
    {
        Q_OBJECT
    public:
    explicit Camera_03(QObject *parent = 0);
    
        cv::VideoCapture cap_03;
        cv::Mat frame;
    
    
    public slots:
    
        void start_cam_03();
    
     signals:
        void cam03_frameChanged(const QImage& qimg);
    
    
    };
    
    #endif // CAMERA_03_H
    
    

    camera_03.cpp

    #include "camera_03.h"
    #include "cvmatconvert.h"
    #include <QDebug>
    Camera_03::Camera_03(QObject *parent) : QObject(parent)
    {
    
    }
    
    void Camera_03::start_cam_03()
    {
        cap_03.open(0);
        if (!cap_03.isOpened()){
            qDebug() << " cam-03 açılamadı" << endl;
        }
    
        while(true){
            cap_03 >> frame;
            Mat tmp ;
            if(!cap_03.read(tmp) )
            {
                qDebug() << " Frame not available "<< endl ;
                cap_03.release();
                cap_03.open(0);
                qDebug() << "problem..." << endl;
    
                if(cap_03.isOpened()){
                    qDebug() << "sorry" << endl;
                }
            }
    
            emit cam03_frameChanged(ASM::cvMatToQImage(frame));
            qDebug() << " cam-03 sended frame";
    
        }
    
    
    }
    
    

    widget.h:

    .....
     void on_kamera03_pushButton_clicked();
     void cam03_displayFrame(QImage qimg);
    ......
    
    
    private:
    //Cam_03
        QThread *cam_03_thread;
        Camera_03 *cam_03;
    
    

    widget.cpp

        cam_03_thread = new QThread;
        cam_03 = new Camera_03;
        cam_03->moveToThread(cam_03_thread);
        cam_03_thread->start();
    ....
    ....
    void Widget::on_kamera03_pushButton_clicked()
    {
        cam_03->start_cam_03();
    }
    
    ...
    
    
    ..
    void Widget::cam03_displayFrame(QImage qimg)
    {
        ui->videol_camera_3->setPixmap(QPixmap::fromImage(qimg));
        qApp->processEvents();
        qDebug() << qimg.sizeInBytes() << endl;
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You're always opening the same capture device with OpenCV. You should improve the handling of the multiple devices you can connect to.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • R Offline
        R Offline
        RahibeMeryem
        wrote on last edited by
        #3

        @RahibeMeryem said in How to properly show&process Multiple OpenCV stream on the UI ?:

        cv::VideoCapture cap_03;

        Hi;

        Each camrea using respective device like :

        cam01.cpp
        cv::VideoCapture cap_01;  
        cam02.cpp
        cv::VideoCapture cap_02;
        cam03.cpp
        cv::VideoCapture cap_02;
        

        So there is already different devices for the job.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          RahibeMeryem
          wrote on last edited by
          #4

          I Found the answer .

          I should start the processing job inside the thread not from the main loop.

          V 1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Indeed, I missed that. You where likely also strangulating the event loop with that since you started an infinite loop.

            Note that you have another issue. You can't cleanly stop your capturing loop which is not nice when stopping your application.

            In anywise, since you have it working now, please mark the thread as solved using the "Topic Tools" button so that other forum members may know a solution has been found :)

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • R RahibeMeryem

              I Found the answer .

              I should start the processing job inside the thread not from the main loop.

              V Offline
              V Offline
              Vijaykarthikeyan
              wrote on last edited by
              #6

              @RahibeMeryem can you share that how using threading..becuase i have did that in conventional way but it is too lagging

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved