Results 1 to 8 of 8

Thread: To Display Video Frames from webcam on QML

  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:


  3. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: To Display Video Frames from webcam on QML

    How exactly didn't you "succeed"?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. The following user says thank you to wysota for this useful post:


  5. #3
    Join Date
    May 2013
    Posts
    11
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Platforms
    Unix/X11

    Default Re: To Display Video Frames from webcam on QML

    wysota thank you for the reply.
    I am using qdeclarative image provider to display image on qml but i am getting an error message "QML Image: Failed to get image from provider: image://colors/test.png"
    Here i am attaching the source code

    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "qmlapplicationviewer.h"
    3. #include <QDeclarativeImageProvider>
    4. #include <QDeclarativeContext>
    5. #include <QDeclarativeEngine>
    6.  
    7.  
    8. class ColorImageProvider : public QDeclarativeImageProvider
    9. {
    10. public:
    11. ColorImageProvider()
    12. : QDeclarativeImageProvider(QDeclarativeImageProvider::Pixmap)
    13. {
    14. }
    15.  
    16. QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
    17. {
    18. int width = 100;
    19. int height = 50;
    20.  
    21. if (size)
    22. *size = QSize(width, height);
    23. QPixmap pixmap(requestedSize.width() > 0 ? requestedSize.width() : width,
    24. requestedSize.height() > 0 ? requestedSize.height() : height);
    25. pixmap.fill(QColor(id).rgba());
    26.  
    27. return pixmap;
    28. }
    29. };
    30.  
    31. Q_DECL_EXPORT int main(int argc, char *argv[])
    32. {
    33. QScopedPointer<QApplication> app(createApplication(argc, argv));
    34.  
    35. QmlApplicationViewer viewer;
    36. viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    37. viewer.setMainQmlFile(QLatin1String("qml/declarative_image_provider/main.qml"));
    38. viewer.showExpanded();
    39.  
    40. QDeclarativeView* view = new QDeclarativeView();
    41. view->engine()->addImageProvider(QLatin1String("color"), new ColorImageProvider);
    42. return app->exec();
    43. }
    To copy to clipboard, switch view to plain text mode 

    main.qml

    Qt Code:
    1. import QtQuick 1.1
    2.  
    3.  
    4. Column {
    5. Image { source: "image://colors/test.png" }
    6. Image { source: "image://colors/test.png" }
    7. }
    To copy to clipboard, switch view to plain text mode 

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


  7. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: To Display Video Frames from webcam on QML

    Seems that you have added your image provider with the name "color" but are trying to access "colors".

  8. The following user says thank you to ChrisW67 for this useful post:


  9. #5
    Join Date
    May 2013
    Posts
    11
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Platforms
    Unix/X11

    Default Re: To Display Video Frames from webcam on QML

    ChrisW67 I have changed the "color" to "colors". Even though I am getting the same error. I am not sure with the syntax. Is it correct to mention the image name in qml or I should load the image with qpixmap.

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


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

    Default Re: To Display Video Frames from webcam on QML

    with no other option I tried the example program from nokia developer site here is the link

    http://harmattan-dev.nokia.com/docs/...eprovider.html

    when I tried the above example I got an error message "QML Image:Failed to get image from provider: image://colors/yellow"

    Here is the code with few changes from example

    main.cpp

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "qmlapplicationviewer.h"
    3. #include <QDeclarativeEngine>
    4. #include <QDeclarativeImageProvider>
    5.  
    6.  
    7. class ColorImageProvider : public QDeclarativeImageProvider
    8. {
    9. public:
    10. ColorImageProvider()
    11. : QDeclarativeImageProvider(QDeclarativeImageProvider::Pixmap)
    12. {
    13. }
    14.  
    15. QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
    16. {
    17. int width = 100;
    18. int height = 50;
    19.  
    20. if (size)
    21. *size = QSize(width, height);
    22. QPixmap pixmap(requestedSize.width() > 0 ? requestedSize.width() : width,
    23. requestedSize.height() > 0 ? requestedSize.height() : height);
    24. pixmap.fill(QColor(id).rgba());
    25.  
    26. return pixmap;
    27. }
    28. };
    29.  
    30. int main(int argc, char *argv[])
    31. {
    32. QScopedPointer<QApplication> app(createApplication(argc, argv));
    33.  
    34. QmlApplicationViewer viewer;
    35. viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    36. viewer.setMainQmlFile(QLatin1String("qml/yellow_red_imageprovider/main.qml"));
    37. viewer.showExpanded();
    38.  
    39. QDeclarativeEngine engine;
    40. engine.addImageProvider(QLatin1String("colors"), new ColorImageProvider);
    41.  
    42.  
    43. return app->exec();
    44. }
    To copy to clipboard, switch view to plain text mode 

    main.qml
    Qt Code:
    1. import QtQuick 1.1
    2.  
    3. Column {
    4. Image { source: "image://colors/red" }
    5. Image { source: "image://colors/yellow" }
    6. }
    To copy to clipboard, switch view to plain text mode 

    I don't know how to solve this problem

  12. #7
    Join Date
    May 2013
    Posts
    11
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Platforms
    Unix/X11

    Default Re: To Display Video Frames from webcam on QML

    I made some changes in the main file. getting the same error . what is the solution

    main.cpp
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QScopedPointer<QApplication> app(createApplication(argc, argv));
    4.  
    5.  
    6. QmlApplicationViewer viewer;
    7. viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    8. viewer.setMainQmlFile(QLatin1String("qml/yellow_red_imageprovider/main.qml"));
    9. viewer.showExpanded();
    10. //QDeclarativeEngine engine;
    11. //engine.addImageProvider(QLatin1String("colors"), new ColorImageProvider);
    12. viewer.engine()->addImageProvider("colors", new ColorImageProvider);
    13.  
    14. return app->exec();
    15. }
    To copy to clipboard, switch view to plain text mode 

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


  14. #8
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: To Display Video Frames from webcam on QML

    May be because you are returning QPixmap instead of QImage ! ??

    Sorry,,, Dint see the class QDeclarativeImageProvider properly You can use requestPixmap() too

Similar Threads

  1. Need access to individual video frames
    By themagician in forum Newbie
    Replies: 2
    Last Post: 26th November 2012, 14:40
  2. Webcam Video Capture
    By bajoelkid12 in forum Qt Programming
    Replies: 28
    Last Post: 25th December 2011, 04:28
  3. How to drop some frames while playing video?
    By cic in forum Qt Programming
    Replies: 0
    Last Post: 11th November 2011, 15:49
  4. Replies: 8
    Last Post: 18th March 2011, 12:27
  5. Help displaying frames from a webcam to the screen
    By stealth86 in forum Qt Programming
    Replies: 2
    Last Post: 3rd July 2007, 19: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.