Results 1 to 8 of 8

Thread: To Display Video Frames from webcam on QML

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2013
    Posts
    11
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Platforms
    Unix/X11

    Default To Display Video Frames from webcam on QML

    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
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QFileDialog>
    6. #include <opencv2/core/core.hpp>
    7. #include <opencv2/highgui/highgui.hpp>
    8. #include <opencv2/imgproc/imgproc.hpp>
    9. #include <iostream>
    10. #include <QTimer>
    11. #include<QLabel>
    12.  
    13.  
    14.  
    15. namespace Ui {
    16. class MainWindow;
    17. }
    18.  
    19. class MainWindow : public QMainWindow
    20. {
    21. Q_OBJECT
    22.  
    23. public:
    24. explicit MainWindow(QWidget *parent = 0);
    25. ~MainWindow();
    26. QImage qimg;
    27.  
    28. private slots:
    29. void on_pushButton_clicked();
    30.  
    31.  
    32. public slots:
    33. virtual void Frame() ;
    34.  
    35. private:
    36.  
    37. cv::Mat image; // the image variable
    38. Ui::MainWindow *ui;
    39. CvCapture *capture; // OpenCV Video Capture Variable
    40. IplImage *frame; // Variable to capture a frame of the input video
    41. cv::Mat source_image; // Variable pointing to the same input frame
    42. QTimer *imageTimer;
    43.  
    44. };
    45.  
    46. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <iostream>
    4. #include <QDeclarativeView>
    5. #include <QDeclarativeComponent>
    6. #include <QDeclarativeItem>
    7. #include<QGridLayout>
    8. #include<QDesktopWidget>
    9.  
    10. using namespace cv;
    11. Mat cameraFrame;
    12.  
    13. MainWindow::MainWindow(QWidget *parent) :
    14. QMainWindow(parent),
    15. ui(new Ui::MainWindow)
    16. {
    17. ui->setupUi(this);
    18.  
    19. const int imagePeriod = 1000/25; // ms
    20.  
    21. imageTimer = new QTimer(this);
    22.  
    23. imageTimer->setInterval(imagePeriod);
    24.  
    25. connect(imageTimer, SIGNAL(timeout()), this, SLOT(Frame()));
    26.  
    27. // Use the default camera
    28. capture = cvCaptureFromCAM( CV_CAP_ANY );//cvCreateCameraCapture(0);
    29. }
    30.  
    31.  
    32. MainWindow::~MainWindow()
    33. {
    34. delete ui;
    35. cvReleaseImage(&frame);
    36. cvReleaseCapture(&capture);
    37. }
    38.  
    39.  
    40.  
    41. void MainWindow::on_pushButton_clicked()
    42. {
    43. if( imageTimer->isActive() )
    44. {
    45. imageTimer->stop();
    46. }
    47. else
    48. {
    49. imageTimer->start();
    50. }
    51.  
    52. }
    53.  
    54. void MainWindow::Frame(){
    55. // Capture a frame
    56. frame = cvQueryFrame(capture);
    57.  
    58. // Point to the same frame
    59. source_image = frame;
    60.  
    61. // Resize Image
    62. cv::resize(source_image, source_image, cv::Size(1080,960) , 0, 0);
    63.  
    64. // Change to RGB format
    65. cv::cvtColor(source_image,source_image,CV_BGR2RGB);
    66.  
    67.  
    68. // Convert to QImage
    69. QImage qimg = QImage((const unsigned char*) source_image.data, source_image.cols, source_image.rows, QImage::Format_RGB888); // convert to QImage
    70.  
    71. ui->labelInputVideo->setPixmap(QPixmap::fromImage(qimg));
    72.  
    73. // Resize the label to fit the image
    74. ui->labelInputVideo->resize(ui->labelInputVideo->pixmap()->size());
    75.  
    76.  
    77. }
    To copy to clipboard, switch view to plain text mode 

  2. The following user says thank you to bts-007 for this useful post:


Similar Threads

  1. Need access to individual video frames
    By themagician in forum Newbie
    Replies: 2
    Last Post: 26th November 2012, 13:40
  2. Webcam Video Capture
    By bajoelkid12 in forum Qt Programming
    Replies: 28
    Last Post: 25th December 2011, 03:28
  3. How to drop some frames while playing video?
    By cic in forum Qt Programming
    Replies: 0
    Last Post: 11th November 2011, 14:49
  4. Replies: 8
    Last Post: 18th March 2011, 11:27
  5. Help displaying frames from a webcam to the screen
    By stealth86 in forum Qt Programming
    Replies: 2
    Last Post: 3rd July 2007, 18:48

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.