简体   繁体   中英

Setting Frame Rate on Logitech C210 webcam in C++ on Raspberry pi using v4l2

I've got some webcams connected to my raspberry pi and am capturing images using OpenCV. However, even thought the cameras aren't moving, the images look kind of shaky. When I only run one camera this doesn't seem to happen so I'm thinking that it is a USB bandwidth issue thing. To reduce the bandwidth I'm starting out by trying to reduce the frame rate. Logitech C210 should be able to support frame rates of 5,10,15,20,25, and 30 fps.

So I've been looking through the documentation on v4l2 since it appears that OpenCV uses this library anyway (although not very well). I've been using v4l2_ioctl() without problem to set camera controls. For some reason I have not been able to set the frame rate in a similar manner. Here is what I am doing following along the documentation here :.

int descriptor = v4l2_open("/dev/video0", O_RDWR);
v4l2_captureparm s;
s.capability = V4L2_CAP_TIMEPERFRAME;
s.timeperframe.numerator = 1;
s.timeperframe.denominator = 5;
if( v4l2_ioctl(descriptor, VIDIOC_S_PARM, &s) !=0 ) 
{
     cout<< "Failed to set frame rate "<<endl;
}

I have no problems compiling, but I do get the couted error message indicating that the value has not successfully been set.

Does anyone have an idea what I am doing wrong?

Thanks in advance!

You are using wrong structure. Y0u should use

struct v4l2_streamparm

instead of

struct v4l2_captureparm

First check if your camera does support those frame rates for desired resoulution and pixel format:

v4l2-ctl --list-formats-ext

Then, you need to set resolution and pixel format. For example:

struct v4l2_format fmt;
meset(&fmt, 0, sizeof(fmt));
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.width       = 1280;
fmt.fmt.pix.height      = 720;
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
fmt.fmt.pix.field       = V4L2_FIELD_NONE;

if (v4l2_ioctl(m_fd, VIDIOC_S_FMT, &fmt) != 0)
{
   // Error
}

Ideally nominator and denominator should be selected from one enumerated from device. Also you should get v4l2_streamparm structure:

struct v4l2_streamparm streamparm;
memset(&streamparm, 0, sizeof(streamparm));
streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (v4l2_ioctl(m_fd, VIDIOC_G_PARM, &streamparm) != 0)
{
   // Error
}

streamparm.parm.capture.capturemode |= V4L2_CAP_TIMEPERFRAME;
streamparm.timeperframe.numerator = X;
streamparm.timeperframe.denominator = y;
if(v4l2_ioctl(descriptor,VIDEO_S_PARM, &s) !=0) 
{
     cout<< "Failed to set frame rate "<<endl;
}

Also be sure to check you arent having problems powering those webcams. The pi cant provide much power from its USB ports and anything like a wifi dongle(god, i hate that word) may be competing for power and i have seen bad pictures as a result before...Maybe a powered hub that is RPi compatible, ie one that doesnt have a 5v connection to the Pi. Buy a rpi recommended powered hub.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM