Results 1 to 6 of 6

Thread: read webcam with opencv

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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: read webcam with opencv

    First program:
    The fundamental problem from a Qt viewpoint is that your code never reaches the Qt event loop, i.e. the a.exec() call, and therefore Qt is not functioning. QWidget::show() schedules the widget to be displayed when the event loop is functioning: it does not show it immediately.

    Aside from that, you code leaks memory, line 6, such that it would probably not run for long before crashing out of memory or at least thrashing the machine's virtual memory.

    Second program:
    You create a named window "result" to display the frames, and then cvShowImage targeting a window called "Left".

  2. #2
    Join Date
    Oct 2011
    Location
    Porto, Portugal
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: read webcam with opencv

    thanks a lot!

    well i changed the window name in the second program but it still does the same...

    and regarding the first program, what do you advise then? removing the "while(1)" loop or extending it until the "return a.exec()" ?

  3. #3
    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: read webcam with opencv

    The problem in the second program is entirely between you and OpenCV, nothing to do with Qt. I cannot offer much help.

    First program: Neither. The looping behaviour needs to be replaced with event driven behaviour. It doesn't belong in the main() function. Try plugging your code into something like the framework below. Also, have your conversion function return the QImage by value rather than using the heap.
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class MainWindow: public QMainWindow {
    5. Q_OBJECT
    6. public:
    7. MainWindow(QWidget *p = 0): QMainWindow(p) {
    8. QLabel *label = new QLabel(this);
    9. setCentralWidget(label);
    10.  
    11. // setup OpenCv
    12.  
    13. connect(&timer, SIGNAL(timeout()), SLOT(capture()));
    14. timer.setInterval(200); // about 5 times per second
    15. timer.start();
    16. }
    17. ~MainWindow() {
    18. // tear down OpenCv
    19. }
    20. public slots:
    21. void capture() {
    22. // capture, convert and display image on label
    23. qDebug() << "Capturing";
    24. }
    25.  
    26. private:
    27. QLabel *label;
    28. QTimer timer;
    29. };
    30.  
    31. int main(int argc, char *argv[])
    32. {
    33. QApplication app(argc, argv);
    34.  
    35. MainWindow m;
    36. m.show();
    37. return app.exec();
    38. }
    39. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    You can adjust the "sampling" rate or use a single shot timer to run as-fast-as-possible, but most video sources will produce a frame every 30th or 25th of a second.

  4. #4
    Join Date
    Jan 2008
    Posts
    72
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: read webcam with opencv

    Hi ,
    I have made an application using opencv for webcam
    I put here some of mine code , i hope it will help you
    /***********************************/
    //first you make a dummy image
    QImage dummy(100,100,QImage::Format_RGB32);
    image = dummy;
    for (int x = 0; x < 100; x ++) {
    for (int y =0; y < 100; y++) {
    image.setPixel(x,y,qRgb(x, y, y));
    }
    }

    //start a timer to capture frame of your webcam

    pCamraTimer = new QTimer(this);
    connect(pCamraTimer,SIGNAL(timeout()),this,SLOT(st artCapture()));
    //In slot start capture

    void WebCamViewer::startCapture()
    {
    if(NULL != camera)
    {
    IplImage *image=cvQueryFrame(camera);
    putImage(image);
    }
    }
    void WebCamViewer:utImage(IplImage *cvimage)
    {
    int cvIndex, cvLineStart;
    // switch between bit depths
    switch (cvimage->depth) {
    case IPL_DEPTH_8U:
    switch (cvimage->nChannels) {
    case 3:
    if ( (cvimage->width != image.width()) || (cvimage->height != image.height()) ) {
    QImage temp(cvimage->width, cvimage->height, QImage::Format_RGB32);
    image = temp;
    }
    cvIndex = 0; cvLineStart = 0;
    for (int y = 0; y < cvimage->height; y++) {
    unsigned char red,green,blue;
    cvIndex = cvLineStart;
    for (int x = 0; x < cvimage->width; x++) {
    // DO it
    red = cvimage->imageData[cvIndex+2];
    green = cvimage->imageData[cvIndex+1];
    blue = cvimage->imageData[cvIndex+0];

    image.setPixel(x,y,qRgb(red, green, blue));
    cvIndex += 3;
    }
    cvLineStart += cvimage->widthStep;
    }
    break;
    default:
    printf("This number of channels is not supported\n");
    break;
    }
    break;
    default:
    printf("This type of IplImage is not implemented in QOpenCVWidget\n");
    break;
    }

    //set this image to your label to show
    ui->imgLbl->setPixmap(QPixmap::fromImage(image.scaled(ui->imgLbl->size(),Qt::KeepAspectRatio,Qt::SmoothTransformati on)));
    }

    /***********************************/

  5. #5
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    1

    Default Re: read webcam with opencv

    Thanks you all for some good guides around here!

    I've tried to implement with these and found some problems:
    + the framework of ChrisW67 is good but in OpenCV, the SLOT capture() needs to hold an object Videocapture (an OpenCV class) to avoid repeating creating this class inside SLOT capture() -> by now, I still don't know how to work around this

    Besides, I work with both Qt and OpenCV new versions Qt 4 and OpenCV 2.3.1 respectively and I had to do some editings to recode your ideas

    ** Solution:
    I've just found it:
    + create a class variable VideoCapture cap
    + open it in class constructor with cap.Open(0) // default device
    + then, it can be used in SLOT without having to pass this 'cap' variable around

    => DONE already !! thanks all!
    Last edited by study24816; 1st July 2012 at 08:41. Reason: add solutions

Similar Threads

  1. Replies: 7
    Last Post: 31st January 2012, 15:19
  2. OpenCV Record Webcam And Audio ???
    By Thành Viên Mới in forum Qt Programming
    Replies: 0
    Last Post: 25th December 2011, 04:18
  3. Replies: 8
    Last Post: 18th March 2011, 11:27
  4. Replies: 2
    Last Post: 15th November 2010, 17:44
  5. use Webcam
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 21st August 2007, 07:52

Tags for this Thread

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.