How to capture still image from webcam on linux -


i trying write c++/qt program linux, take still image photo webcam, make transformations photo (cropping, resizing, etc.), , save jpeg file.

but have encountered problems. main problem standart uvc (usb video device class) linux driver not support direct still image capture: http://www.ideasonboard.org/uvc/ .

so, there 2 possible ways capture still image. can take 1 frame video stream camera, or can take separate photo, digital portable camera. second way not supported in linux uvc driver, first method way. problem is, if want take frame video stream, size of photo can't bigger size of video in video preview window. so, if want take 2 megapixel photo, must start videostream size 1600x1200, not comfortable (at least, in qt size of videostream depends on videopreview window size).

i know there video linux 2 api, may helpful in task, don't know how use it. learning gstreamer, can't figure out how need using these tools.

so, appreciate help. think not hard problem people know linux, gstreamer, v4l2 api, , other linux-specific things.

by way, program used web-camera logitech c270 hd.

please, me. don't know api or framework can me this. may know.

unfortunately c4v2 calls in opencv did not work still image capture camera have tried out of box using uvc driver.

to debug issue have been playing trying accomplish c code calling c4v2 directly.

i have been playing example code found here. uses method of pulling frames video stream.

you can compile with:

gcc -o2 -wall `pkg-config --cflags --libs libv4l2` filename.c -o filename 

i have experimented 3 logitech cameras. best of lot seems logitech c910. has significant issues.

here problems have encountered trying accomplish same task code.

it works pretty every time width , height set 1920x1080.

when query other possibilities directly command line using example:

v4l2-ctl --list-formats-ext 

and try of other "available" smaller sizes hangs in select waiting camera release buffer.

also when try set other sizes directly command line using example:

v4l2-ctl -v height=320 -v width=240 -v pixelformat=yuyv 

then check

v4l2-ctl -v 

i find returns correct pixel format quite not correct size.

apparently camera listed on uvc site being uvc , therefore v4l2 compatible not snuff. suspect bad other cameras. other 2 tried listed compatible on site had worse problems.

i did more testing on logitechc910 after posted this. thought post results in case helps else out.

i wrote script test v4l2 grabber code mentioned above on formats camera claims supports when queried v4l2 here results:

640x480 => hangs on clearing buffer 160x120 => works 176x144 => works 320x176 => works 320x240 => works 432x240 => works 352x288 => works 544x288 => works 640x360 =>  works 752x416 => hangs on clearing buffer 800x448 => hangs on clearing buffer 864x480 => works 960x544 => works 1024x576 => works 800x600 => works 1184x656 => works 960x720 => works 1280x720 => works 1392x768 => works 1504x832 => works 1600x896 => works 1280x960 => works 1712x960 => works 1792x1008 => works 1920x1080 => works 1600x1200 => works 2048x1536 => works 2592x1944 => hangs on clearing buffer. 

it turns out default setting of 640x480 doesnt work , trapped me , others have posted on message boards.

since grabbing video frame first frame grabs when starting may have incorrect exposure (often black or close it). believe because since being used video camera adjusts exposure goes , doesnt care first frames. believe trapped me , other saw first frame black or black , thought kind of error. later frames have correct exposure

it turns out opencv python wrappers works fine camera if avoid land mines listed above , ignore error messages. error messages due fact while camera accepts v4l2 commands doesnt respond correctly. if set width gets set correctly responds incorrect width.

to run under opencv python wrappers can following:

import cv2 import numpy  cap = cv2.videocapture(0)  #ignore errors cap.set(3, 960)        #set width important because default timeout                        #ignore error or false response cap.set(4, 544)        #set height ignore errors r, frame = cap.read() cv2.imwrite("test.jpg", frame) 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -