Results 1 to 2 of 2

Thread: Corrupted video with OpenCV reading

  1. #1
    Join Date
    Nov 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Corrupted video with OpenCV reading

    1 down vote favorite


    Recently, I have done a graphical user interface in C++/Qt/OpenCV on Debian 6.0. It achieves image processing (Canny Edge) on a webcam video streaming.

    At the execution, the video is corrupted. The most annoying is this bug is not reproducible, it appears randomly.

    Here's an example of corrupted video :



    This problem seems not to come from the webcam because a simple OpenCV display code shows a good video.

    Into my Qt code, I use a main class inherited from QMainWindow. The "start_webcam" button is linked to the "start_webcam" slot by :


    Qt Code:
    1. startwebcamAct = new QAction(tr("&Start Webcam"), this);
    2. connect(startwebcamAct, SIGNAL(triggered()), this, SLOT(start_webcam()));
    To copy to clipboard, switch view to plain text mode 

    Then, the "start_webcam" function uses a QTimer as follows :


    Qt Code:
    1. void ImageViewer::start_webcam()
    2. {
    3. webcam_on = 1;
    4. invertWebcam = 0;
    5. close_current();
    6. updateStatusWebcam(webcam_on);
    7. // Init capture
    8. capture = cvCaptureFromCAM(0);
    9. first_image = cvQueryFrame(capture);
    10. // Init current qimage
    11. current_qimage = QImage(QSize(first_image->width,first_image->height),QImage::Format_RGB32);
    12. resize(first_image->width,first_image->height);
    13. // start QTimer
    14. timer = new QTimer(this);
    15. connect(timer,SIGNAL(timeout()),this,SLOT(query_frame()));
    16. timer->start(0);
    17. }
    To copy to clipboard, switch view to plain text mode 

    with "query_frame" function (where I use repaint() ) :

    Qt Code:
    1. void ImageViewer::query_frame()
    2. {
    3. webcam_off = 0;
    4. IplImage* frame = cvQueryFrame(capture);
    5. int w = frame->width;
    6. int h = frame->height;
    7.  
    8. if (matrixMode)
    9. {
    10. current_image = cvCreateImage(cvGetSize(frame),8,3);
    11. cvCvtColor(frame,current_image,CV_BGR2RGB);
    12. int cvIndex = 0;
    13. int cvLineStart = 0;
    14. unsigned char red,green,blue;
    15.  
    16. for(int j = 0; j < h; j++)
    17. {
    18. cvIndex = cvLineStart;
    19. for(int i = 0; i < w; i++)
    20. {
    21. red = 0;
    22. green = current_image->imageData[cvIndex+1];
    23. blue = 0;
    24.  
    25. current_qimage.setPixel(i,j,qRgb(red, green, blue));
    26. cvIndex += 3;
    27. }
    28. cvLineStart += current_image->widthStep;
    29. }
    30. }
    31. gaussianfilter(webcam_off);
    32. border_detect(webcam_off);
    33. if (invertWebcam)
    34. invertvalues(webcam_off);
    35. cvReleaseImage(&current_image);
    36. repaint();
    37. }
    To copy to clipboard, switch view to plain text mode 

    The repaint() function calls (immediatly ?) paintEvent :


    Qt Code:
    1. void ImageViewer::paintEvent(QPaintEvent*)
    2. {
    3.  
    4. QPainter painter(this);
    5. painter.drawImage(QRect(0,0,current_qimage.width(),current_qimage.height()),current_qimage);
    6. }
    To copy to clipboard, switch view to plain text mode 

    At the execution, if I maximize the GUI window, video is shown correctly ; here's an example of this good display :




    This lag seems to be a conflict between the QTimer into "start_webcam()" and the "paintEvent" function but I am not sure.

    I tried to use update() instead of repaint() but the problem remains.

    Anyone could give me a clue on this kind of bug ?

  2. #2
    Join Date
    Nov 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Corrupted video with OpenCV reading

    With this lag, I really suspect a no-synchronization between the call of query_frame and the call of paintEvent into this.

    Could you help me to force the synchronization in order to, when I call paintEvent with repaint(), have the image displayed once and after, have
    the next call of query_frame by QTimer ?

    Thanks !

Similar Threads

  1. reading qt + opencv source code
    By nkint in forum Newbie
    Replies: 1
    Last Post: 3rd June 2013, 06:14
  2. Replies: 1
    Last Post: 3rd August 2012, 18:39
  3. How to capture Video with OpenCV
    By CCDamon in forum Qt Programming
    Replies: 5
    Last Post: 17th June 2011, 15:18
  4. Qt Gui with OpenCV video and buttons
    By marcusbarnet in forum Qt Programming
    Replies: 3
    Last Post: 20th May 2011, 09:48
  5. Reading audio AND video metadata from files (Phonon?)
    By garfunkel in forum Qt Programming
    Replies: 0
    Last Post: 1st March 2010, 08:32

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.