title: 50% faulty pictures from webcam using VideoCapture
body: <p>I 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 (<a href="http://s1.directupload.net/images/121119/nl7ggood.jpg" rel="nofollow">see image</a>).</p>
  
  <p>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 searching. For me it looks as if the image from the webcam is read 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".</p>
  
  <p>So my question is, is there a way to use this mode with openCV too? I would like to avoid using v4l2 directly.</p>
  
  <pre><code>VideoCapture cap(0);
  if(!cap.isOpened())
  cout &lt;&lt; "Opening video device failed" &lt;&lt; endl;
  cap.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
  cap.set(CV_CAP_PROP_FRAME_HEIGHT, 1024);
  Mat frame;
  for(int i = 0; i&lt;50; i++)
  
  {
      stringstream str;
      str &lt;&lt; "/home/ubuntu/image" &lt;&lt; i&lt;&lt; ".jpg";
      cap &gt;&gt; frame;
      imwrite(str.str(), frame);
      cout &lt;&lt; i &lt;&lt; endl;
  }
  
  return 0;
  </code></pre>