1

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 (see image).

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".

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;
the_summer
  • 439
  • 3
  • 10

2 Answers2

2

Did you try without setting the frame width and height?

You can also rescale the images later if that is necessary, you'll get the maximum resolution from the VideoCapture by default.

Yeah, also try to wait before each frame:

cv::waitKey(30); // it waits only for 30ms, that could be your fps on the webcam.

EDIT:

have a look at Karl Philipp's answer.

Community
  • 1
  • 1
Barney Szabolcs
  • 11,846
  • 12
  • 66
  • 91
  • Indeed. When I comment these lines, it works. The images are now only 640x480. According to Logitech the Camera should have 1.3 MP, so 1MP would be missing if you are right and that's really the maximum resolution. o.O – the_summer Nov 19 '12 at 22:50
  • Your second suggestion with the waitKey-didn't change anything for the original problem. I think I will stick with the 640x480 for now and see if I find a better webcam. Thank you very much. – the_summer Nov 19 '12 at 22:58
  • This can be quite annoying. Np, it could be an opencv driver support problem. There is a webpage to check supported webcams and max. resolutions (I've seen it on google back in the day). Also might worth a shot experimenting with lower resolutions. – Barney Szabolcs Nov 19 '12 at 23:03
  • I edited the answer to point you in the right direction, hopefully. If my answer was also helpful, can you please +1 too? :) cheers – Barney Szabolcs Nov 19 '12 at 23:06
  • Sure. Sorry, I not too familiar with stackoverflow yet ;-) – the_summer Nov 19 '12 at 23:14
  • NP, it took me a while too until I have figured out how things work, I asked you only [following "howtoask" section in the faq](http://stackoverflow.com/faq#howtoask). I soon became a fan of this page, I hope you'll enjoy it a lot, too. ;) – Barney Szabolcs Nov 20 '12 at 05:19
1

I found another thread here which states that the implementation of how the frames are captured by openCV may be the culprit:

OpenCV calls on video-4-linux (v4l2) to request RGB frames from the camera. My camera was delivering JPEG compressed images which v4l2 was decompressing using a very slow software decoder.

I am using a BeagleBoard and when using the (convenient) openCV-functions I had the mentioned problems. I tried the program Martin Fox linked in the thread, which takes YUV-frames from the webcam directly with v4l2. Now it is possible to get pictures up to 1280x1024 pixels without problems what is the resolution of the webcam's sensor.

the_summer
  • 439
  • 3
  • 10
  • wow, congrats! :) Im really glad you've found it after all, it kept coming back into my thoughts why OpenCV could've kept the resolution so low. – Barney Szabolcs Nov 23 '12 at 22:21