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 ?