Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. To Display Video Frame on QML
QtWS25 Last Chance

To Display Video Frame on QML

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 Posters 3.4k 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.
  • B Offline
    B Offline
    bts-007
    wrote on last edited by
    #1

    I am using opencv to display video frames from web cam on Qt gui. Now I have planed to display it on QML window(without Qt gui). I was trying to use qdeclarative view but I didn't succeed.
    I am attaching the source code for displaying the video on label. In the code I used label (labelInputVideo) and push button to start the frames. Is it possible to display qimage directly on qml.
    Thanks in advance

    mainwindow.h
    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QFileDialog>
    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    #include <iostream>
    #include <QTimer>
    //#include <dialog.h>
    #include<QLabel>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    QImage qimg;

    private slots:
    void on_pushButton_clicked();

    public slots:
    virtual void Frame() ;

    private:

    cv::Mat image; // the image variable
    Ui::MainWindow *ui;
    CvCapture *capture;          // OpenCV Video Capture Variable
    IplImage *frame;            // Variable to capture a frame of the input video
    cv::Mat source_image;     // Variable pointing to the same input frame
    QTimer *imageTimer;
    

    };

    #endif // MAINWINDOW_H
    @

    main.cpp
    @
    #include <QtGui/QApplication>
    #include "mainwindow.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
    }
    @

    mainwindow.cpp
    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <iostream>
    #include <QDeclarativeView>
    #include <QDeclarativeComponent>
    #include <QDeclarativeItem>
    #include<QGridLayout>
    #include<QDesktopWidget>

    using namespace cv;
    Mat cameraFrame;

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    const int imagePeriod = 1000/25;   // ms
    
    imageTimer = new QTimer(this);
    
    imageTimer->setInterval(imagePeriod);
    
    connect(imageTimer, SIGNAL(timeout()), this, SLOT(Frame()));
    
    // Use the default camera
    capture = cvCaptureFromCAM( CV_CAP_ANY );//cvCreateCameraCapture(0);
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    cvReleaseImage(&frame);
    cvReleaseCapture(&capture);
    }

    void MainWindow::on_pushButton_clicked()
    {
    if( imageTimer->isActive() )
    {
    imageTimer->stop();
    }
    else
    {
    imageTimer->start();
    }

    }

    void MainWindow::Frame(){
    // Capture a frame
    frame = cvQueryFrame(capture);

    // Point to the same frame
    source_image = frame;
    
    // Resize Image
    cv::resize(source_image, source_image, cv::Size(1080,960) , 0, 0);
    
    // Change to RGB format
    cv::cvtColor(source_image,source_image,CV_BGR2RGB);
    
    
    // Convert to QImage
    QImage qimg = QImage((const unsigned char*) source_image.data, source_image.cols, source_image.rows, QImage::Format_RGB888); // convert to QImage
    
    ui->labelInputVideo->setPixmap(QPixmap::fromImage(qimg));
    
    // Resize the label to fit the image
    ui->labelInputVideo->resize(ui->labelInputVideo->pixmap()->size());
    

    }
    @

    1 Reply Last reply
    0
    • R Offline
      R Offline
      RafaelTSCS
      wrote on last edited by
      #2

      So. Have you managed to do that? I am trying it too with no success. :(

      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