Important: Please read the Qt Code of Conduct -

How to pre-process QCamera frames using OpenCV before displaying on QCameraViewfinder

  • Hi All,

    I have been using QT5 for developing cross-platform applications for a short while although I am pretty good at C++ and stitching things together :)

    I have developed a small application to connect to webcam and display videos inspired by the below example:

    I have bean trying to find a way to preprocess each frame before it is displayed by QCameraViewfinder (using opencv eg. apply edge detection filters and camera calibration ).

    So far I looked into pretty much many examples like below which turned out to be VERY slow:

    and also I have come across some other examples which mainly use qml which are NOT helpful cuz I am supposed to do everything in standard QT C++ as below:

    in which they use QAbstractVideoFilter QVideoFilterRunnable but I have no idea if they could be used by QCamer/QCameraViewfinder.

    But as I mentioned I am NOT supposed to use qml.

    I am just looking for a example describing how I can accomplish such task

    Much appreciate any clue and comment that could help me.

    Thank you all


  • Lifetime Qt Champion


    How slow are they ?
    Did you consider moving the OpenCV processing part in its own thread and send a QImage to the GUI thread for displaying ?

  • I use timer with interval 100 but I get to see one 1fps. Really weird, I have to admit I might have not threading correctly.

    void MainWindow::on_startButton_clicked()
    cout << "on_pushButton_clicked" << endl;
    // start camera
    ui->graphicsView->setScene(new QGraphicsScene(this));
    ui->graphicsView->fitInView(&pixmap, Qt::KeepAspectRatio);;

    thread = new QThread(this);
    timer = new QTimer();
    //Creating instance of Engine worker
    worker = new CaptureProcessVideoThread();
    worker->setVideoPixma(&video, &pixmap);
    //Connecting Engine worker' foo slot to timer
    connect(timer, &QTimer::timeout, worker, &CaptureProcessVideoThread::run);
    connect(thread, &QThread::started, timer, static_cast<void (QTimer::*)(void)>(&QTimer::start));


    alt text

    Even if you can comment on the multi-threading that would help a lot.

    Thank you

  • Lifetime Qt Champion

    Can you show the CaptureProcessVideoThread class content ?

  • @SGaist said in How to pre-process QCamera frames using OpenCV before displaying on QCameraViewfinder:


    Sure ....

    #include <QThread>
    #include <opencv/cv.hpp>
    #include <QGraphicsPixmapItem>
    #include <QListWidget>
    #include <QGraphicsView>
    using namespace cv;
    class CaptureProcessVideoThread : public QObject
    public slots:
        void run();
        void captureSnapshot();
        //CaptureProcessVideoThread(QGraphicsPixmapItem *pixmap, VideoCapture *video);
        void setVideoPixma(VideoCapture *video, QGraphicsPixmapItem *pixmap);
        void setSnapshotFolder(QString snapshotFolder);
        void setCaptureListWidget(QListWidget*);
        void setGraphicsView(QGraphicsView*);
        QGraphicsView *m_graphicsView;
        QGraphicsPixmapItem *m_pixmap;
        VideoCapture *m_video;
        QString m_snapshotFolder;
        bool m_takeSnapshot = false;
        QListWidget *m_captureListWideget;
    #include "captureprocessvideothread.h"
    #include <utils/settings.h>
    #include <utils/utils.h>
    using namespace cv;
    using namespace std;
    void CaptureProcessVideoThread::setVideoPixma(VideoCapture *video, QGraphicsPixmapItem *pixmap){
        m_video = video;
        m_pixmap = pixmap;
    void CaptureProcessVideoThread::setSnapshotFolder(QString snapshotFolder){
        m_snapshotFolder = snapshotFolder;
    void CaptureProcessVideoThread::setCaptureListWidget(QListWidget *captureListWideget){
        m_captureListWideget = captureListWideget;
    void CaptureProcessVideoThread::run(){
        Mat frame, frameToSave;
        QTextStream cout(stdout);
        // index for snapshots
        static int lastSnapIndex = 0;
        if (m_video->isOpened()){
            *m_video >> frame;
            *m_video >> frameToSave;
            // try to find chessboard corners
            // if found draw them
            vector<Point2f> pointBuf;
            Settings s;
            //        s.boardSize = Size(19, 11);
            s = getCalibrationSettings();
            bool found = findChessboardCorners( frame, s.boardSize, pointBuf, chessBoardFlags);
            if (found){
                drawChessboardCorners( frame, s.boardSize, Mat(pointBuf), found );
            // create QImage from frame
            QImage qimg(, frame.cols, frame.rows, frame.step, QImage::Format_RGB888);        
            QImage qimgToSave(, frameToSave.cols, frameToSave.rows, frameToSave.step, QImage::Format_RGB888);
            // update the Graphvis the view with the new frame
            m_pixmap->setPixmap( QPixmap::fromImage(qimg.rgbSwapped()) );
            m_graphicsView->fitInView(m_pixmap, Qt::KeepAspectRatio);
            // take snapshot if asked for
                QString fileName = m_snapshotFolder + QString("/Capture_%1d.png").arg(++lastSnapIndex, 5, 10, QChar('0'));
                QFileInfo fileInfo(fileName);            
                // add the snapshot to list of snapshots
                m_captureListWideget->addItem(new QListWidgetItem( QIcon(fileName), fileInfo.fileName()));
                m_takeSnapshot = false;
    void CaptureProcessVideoThread::setGraphicsView(QGraphicsView *graphicsView){
        m_graphicsView = graphicsView;
    void CaptureProcessVideoThread::captureSnapshot(){
        m_takeSnapshot = true;

  • Lifetime Qt Champion

    You should avoid re-creating the pixmap, once you have it with the correct size, just memcpy the content of the processed frame into place.

    You should also avoid calling fitInView from that external thread. GUI operation should only happen in the GUI thread. You're lucky it doesn't crash.

    Same goes for the snapshot handling.

    Did you try to get some performance number from your run method ?

  • so .... you mean sth like: QPixmap::loadFromData

    I tried this but no luck so far....

            QByteArray arr;
            QBuffer buffer(&arr);

    Looks like I have done correctly before :

    No errors ... but I don't see anything...

  • The source code for setPixmap is like below:

    void QGraphicsPixmapItem::setPixmap(const QPixmap &pixmap)
        d->pixmap = pixmap;
        d->hasShape = false;

    from here:

    The only way seems to keep access and update d->pixmap directly using a memcopy sth like that just to avoid changing the location of the image to be viewed in the memory.

    am I correct?

    QPixmap QGraphicsPixmapItem::pixmap() const
        Q_D(const QGraphicsPixmapItem);
        return d->pixmap;

  • Lifetime Qt Champion

    It's the idea. Avoid re-creating objects if possible. By the way, can't you get OpenCV to directly give you image data in RGB rather than BGR ? That would avoid a copy operation.

  • @mikeitexpert thanks mike, can you share please the: #include <utils/settings.h>
    #include <utils/utils.h> ?

Log in to reply