Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Get 50% faulty pictures from webcam using VideoCapture

Hello, I just want to take single frames from a Logitech C300 webcam using openCV. When I use the code below. Images are saved but, in about 50% of the cases they contain errors (see image image description). I now have been searching for hours, but as I don't know what is causing it, I'm not sure which keyword may help. For me it looks that the image from the webcam is saved while the camera is still updating the frame (but's just a guess). I tested uvccapture which produces the same problems (but less often) with standard options, but works without problems when using the -m option. According to the man-page it's "Toggles capture mode to YUYV capture".

So my question is, is there a way to use this mode with openCV too? I would like to avoid using v4l2 directly.

VideoCapture cap(0);
if(!cap.isOpened())
    cout << "Opening video device failed" << endl;
cap.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 1024);
Mat frame;
for(int i = 0; i<50; i++)
{
    stringstream str;
    str << "/home/ubuntu/image" << i<< ".jpg";
    cap >> frame;
    imwrite(str.str(), frame);
    cout << i << endl;
}

return 0;