v4l2 - OpenCV: can't set resolution of video capture -


i using opencv 2.4.5 on ubuntu 12.04 64-bit. able set resolution of input logitech c310 webcam. camera supports 1280x960 @ 30fps, , able view video @ resolution in guvcview. opencv gets video @ 640x480.

trying change resolution cap.set(cv_cap_prop_frame_width, 1280) , cap.set(cv_cap_prop_frame_height, 960) after videocapture cap created has no effect; trying set them before getting every frame causes program crash immediately. cannot reduce resolution method either. getting error "highgui error: v4l/v4l2: vidioc_s_crop". think may related, because appears once when videocapture created, , once when try set width , height (but, oddly, not if try set 1 of them).

i know i'm not first have problem, have yet find solution after googling , scouring of , elsewhere on internet (among many things i've tried no avail answer stackoverflow question: increasing camera capture resolution in opencv). bug in opencv? if so, it's rather glaring one.

here's example of code exhibits problem (just modified version of opencv's video display code):

#include <cv.h> #include <highgui.h> using namespace cv;  int main(int argc, char** argv) {     videocapture cap(0); // open default camera     if(!cap.isopened())  // check if succeeded             return -1;      cap.set(cv_cap_prop_frame_width, 160);     cap.set(cv_cap_prop_frame_height, 120);      mat image;     namedwindow("video", cv_window_autosize);      while(1)     {             // cap.set(cv_cap_prop_frame_width, 160);             // cap.set(cv_cap_prop_frame_height, 120);             cap >> image;              imshow("video", image);              if(waitkey(10) == 99 ) break;     }     return  } 

as is, gets me 2 "highgui error"s described above , 640x480 output. know 160x120 resolution camera supports running v4l2-ctl --list-formats-ext. if uncomment 2 commented-out lines in while loop, program crashes immediately.

these might related or have possible solutions: http://answers.opencv.org/question/11427/decreasing-capture-resolution-of-webcam/, http://answers.opencv.org/question/30062/error-setting-resolution-of-video-capture-device/

this bug in v4l "version" (build) of opencv 2.4 (including 2.4.12), bug not in libv4l version. opencv 3.1.0, neither v4l nor libv4l version has bug.

(your error error message highgui error: v4l/v4l2: vidioc_s_crop indicates have v4l version; message in cap_v4l.cpp, see code, not in cap_libv4l.cpp.)

a workaround v4l version of opencv 2.4 work @ fixed resolution other 640x480 changing values default_v4l_width , default_v4l_height in modules/highgui/src/cap_v4l.cpp , re-building opencv, kudos answer.

if want build libv4l version instead, need install libv4l-dev , rebuild opencv; with_libv4l enabled default me. if not, cmake command should contain

-d with_libv4l=on 

the cmake output (or version_string.tmp) libv4l build contains like

  video i/o:     ...     v4l/v4l2:   using libv4l1 (ver 0.8.6) / libv4l2 (ver 0.8.6) 

(for v4l build, v4l/v4l2: no/yes.)


Comments