View Full Version : Corrupted video with OpenCV reading

4th July 2013, 00:43
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 :

startwebcamAct = new QAction(tr("&Start Webcam"), this);
connect(startwebcamAct, SIGNAL(triggered()), this, SLOT(start_webcam()));

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

void ImageViewer::start_webcam()
webcam_on = 1;
invertWebcam = 0;
// Init capture
capture = cvCaptureFromCAM(0);
first_image = cvQueryFrame(capture);
// Init current qimage
current_qimage = QImage(QSize(first_image->width,first_image->height),QImage::Format_RGB32);
// start QTimer
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(query_fr ame()));

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

void ImageViewer::query_frame()
webcam_off = 0;
IplImage* frame = cvQueryFrame(capture);
int w = frame->width;
int h = frame->height;

if (matrixMode)
current_image = cvCreateImage(cvGetSize(frame),8,3);
int cvIndex = 0;
int cvLineStart = 0;
unsigned char red,green,blue;

for(int j = 0; j < h; j++)
cvIndex = cvLineStart;
for(int i = 0; i < w; i++)
red = 0;
green = current_image->imageData[cvIndex+1];
blue = 0;

current_qimage.setPixel(i,j,qRgb(red, green, blue));
cvIndex += 3;
cvLineStart += current_image->widthStep;
if (invertWebcam)

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

void ImageViewer::paintEvent(QPaintEvent*)

QPainter painter(this);
painter.drawImage(QRect(0,0,current_qimage.width() ,current_qimage.height()),current_qimage);

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 ?

12th July 2013, 01:43
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 !