Results 1 to 6 of 6

Thread: read webcam with opencv

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

    Default read webcam with opencv

    Hello!

    I've trying to set just a really simple program to make sure my OpenCV installation is working properly. The idea is simple get the frames from my webcam and show them.

    I made a code that is able to load an image from a directory and i also have a code that uses the opencv to get the images from the webcam and show them with the opencv highgui. I already figured out that i need to convert the IplImage to the QImage so i also managed to find a function that does the conversion between formats. What i did was this:

    Qt Code:
    1. QImage* IplImage2QImage(IplImage *iplImg)
    2. {
    3. int h = iplImg->height;
    4. int w = iplImg->width;
    5. int channels = iplImg->nChannels;
    6. QImage *qimg = new QImage(w, h, QImage::Format_ARGB32);
    7. char *data = iplImg->imageData;
    8.  
    9. for (int y = 0; y < h; y++, data += iplImg->widthStep) {
    10. for (int x = 0; x < w; x++) {
    11. char r, g, b, a = 0;
    12.  
    13. if (channels == 1) {
    14. r = data[x * channels];
    15. g = data[x * channels];
    16. b = data[x * channels]; }
    17. else if (channels == 3 || channels == 4) {
    18. r = data[x * channels + 2];
    19. g = data[x * channels + 1];
    20. b = data[x * channels]; }
    21.  
    22. if (channels == 4) {
    23. a = data[x * channels + 3];
    24. qimg->setPixel(x, y, qRgba(r, g, b, a)); }
    25. else {
    26. qimg->setPixel(x, y, qRgb(r, g, b)); }
    27. }
    28. }
    29. return qimg;
    30. }
    31.  
    32. int main(int argc, char *argv[])
    33. {
    34. IplImage* frame;
    35. CvCapture* capture;
    36. QImage* myImage;
    37. QLabel myLabel;
    38.  
    39. QApplication a(argc, argv);
    40.  
    41. capture = cvCreateCameraCapture(1);
    42.  
    43. while(1) {
    44. frame = cvQueryFrame(capture);
    45. myImage = IplImage2QImage(frame);
    46.  
    47. myLabel.setPixmap(QPixmap::fromImage(*myImage));
    48. myLabel.show(); }
    49.  
    50. cvReleaseCapture(&capture);
    51. return a.exec();
    52. }
    To copy to clipboard, switch view to plain text mode 

    it builds without any error but when i run it i only get this:
    result.jpg

    i also checked THIS post and from it i assumed that only using openCV i should also be able to get the output of the webcam (i rewritten the code only for one web)

    Qt Code:
    1. int main( int argc, char *argv[] )
    2. {
    3. CvCapture *capture = 0;
    4. IplImage *frame = 0;
    5. int key = 0;
    6.  
    7. /* initialize camera */
    8. capture = cvCaptureFromCAM( 0 );
    9.  
    10. /* always check */
    11. if ( !capture ) {
    12. fprintf( stderr, "Cannot open initialize webcam!\n" );
    13. return 1; }
    14.  
    15. cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 640);
    16. cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 480);
    17.  
    18. cvSetCaptureProperty(capture, CV_CAP_PROP_FPS, 8);
    19.  
    20. /* create a window for the video */
    21. cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );
    22.  
    23. while( key != 'q' ) {
    24. /* get a frame */
    25. frame = cvQueryFrame( capture );
    26.  
    27. /* always check */
    28. if( !frame ) break;
    29.  
    30. /* display current frame */
    31. cvShowImage("Left", frame);
    32.  
    33. /* exit if user press 'q' */
    34. key = cvWaitKey( 1 ); }
    35.  
    36. /* free memory */
    37. cvDestroyWindow( "result" );
    38. cvReleaseCapture( &capture );
    39. return 0;
    40. }
    To copy to clipboard, switch view to plain text mode 

    and i get exactly the same results. no errors when built and the a similar cmd window as the previous picture...

    can someone give me a hint on what is wrong in either of the codes and why i'm not getting the images from the webcam?
    many thanks
    Last edited by rodolfo.marques; 22nd January 2012 at 02:05.

  2. #2
    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".

  3. #3
    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()" ?

  4. #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: 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.

  5. #5
    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)));
    }

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

  6. #6
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.