Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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;
    }
    

  • Lifetime Qt Champion

    Hi,

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



  • @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.



  • I Found the answer .

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


  • Lifetime Qt Champion

    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 :)


Log in to reply