OpenCV: unable to set video capture resolution

I am using OpenCV 2.4.5 on Ubuntu 12.04 64-bit. I would like to be able to set the input resolution from my Logitech C310 webcam. The camera supports up to 1280x960 at a speed of 30 frames per second, and I can view video in this resolution in guvcview. But OpenCV always receives video only at 640x480.

Attempting to change the resolution with cap.set (CV_CAP_PROP_FRAME_WIDTH, 1280) and cap.set (CV_CAP_PROP_FRAME_HEIGHT, 960) immediately after creating the VideoCapture cap does not affect; trying to set them immediately before getting each frame, the program will immediately work. I also can not reduce the resolution with this method. I also get the error message "HIGHGUI ERROR: V4L / V4L2: VIDIOC_S_CROP". I think this may be related because it appears once when VideoCapture is created, and once when I try to set the width and height (but, which is strange if I do not try to set only one of them).

I know that I am not the first to have this problem, but I still have to find a solution after a lot of googling and cleaning SO and other places on the Internet (among many things that I have already tried to no avail, the answer to this question StackOverflow: Increasing camera capture resolution in OpenCV ). Is this a bug in OpenCV? If so, it's pretty egregious.

Here is an example code that shows the problem (only a modified version of the OpenCV video display code):

#include <cv.h> #include <highgui.h> using namespace cv; int main(int argc, char** argv) { VideoCapture cap(0); // open the default camera if(!cap.isOpened()) // check if we 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 } 

Be that as it may, it gives me two "HIGHGUI ERROR", as described above, and I get an output of 640x480. I know that 160x120 is the resolution supported by my camera from running v4l2-ctl --list-formats-ext . If I uncomment the two commented lines in a while loop, the program will immediately work.

They can be 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 /

+11
opencv v4l2 v4l logitech
May 6 '13 at 1:19
source share
4 answers

This is a bug in version v4l "(build) of OpenCV 2.4 (including 2.4.12), but a bug is not in version libv4l . For OpenCV 3.1.0, neither version v4l nor version libv4l have an error.

(Your error message HIGHGUI ERROR: V4L/V4L2: VIDIOC_S_CROP indicates that you have version v4l, the message is in cap_v4l.cpp, see code , but not in cap_libv4l.cpp.)

Workaround for OpenCV 2.4 version v4l to work with a fixed resolution other than 640x480 by changing the values ​​for DEFAULT_V4L_WIDTH and DEFAULT_V4L_HEIGHT to the / highgui / src / cap _v4l.cpp modules and OpenCV recovery, answer .

If you want to build a version of libv4l, all you probably need to do is install libv4l-dev and rebuild OpenCV; WITH_LIBV4L was turned on by default for me. If not, your cmake command should contain

 -D WITH_LIBV4L=ON 

The cmake output (or version_string.tmp) for building libv4l contains something like

  Video I/O: ... V4L/V4L2: Using libv4l1 (ver 0.8.6) / libv4l2 (ver 0.8.6) 

(To build v4l, it's just V4L/V4L2: NO/YES .)

+3
Apr 20 '16 at 22:37
source share

You can use v4l2-ctl to set the frame size of the captured video, as shown below.

 v4l2-ctl --set-fmt-video=width=1920,height=1080,pixelformat=1 

Further information can be found on this page.

0
Aug 19 '15 at 21:03
source share

I just wanted to add CMAKE parameters for building from Java to Raspberry Pi 3 based on the comprehensive Ulrich answer for OpenCV 3.2.0. Create a / build a folder in the same folder as OpenCV CMakeList.txt, and run this script for the new / build folder:

sudo cmake -D CMAKE_BUILD_TYPE = RELEASE -D WITH_OPENCL = OFF -D BUILD_PERF_TESTS = OFF -D BUILD_SHARED_LIBS = OFF -D JAVA_INCLUDE_PATH = $ JAVA_HOME / include -D JAVA_AWT_LIBRARY = $ JAVA_HOME / jre / lib / arm / libawt.so - D JAVA_JVM_LIBRARY = $ JAVA_HOME / jre / lib / arm / server / libjvm.so -D CMAKE_INSTALL_PREFIX = / usr / local -D BUILD_TESTS = OFF -D WITH_MATLAB = OFF -D WITH_CUFFT = OFF -D WITH_CUDA = OFF -D WITH_CUBLAS = OFF -D WITH_GTK = OFF -D WITH_WEBP = OFF -D BUILD_opencv_apps = OFF -D BUILD_PACKAGE = OFF -D WITH_LIBV4L = ON ..

0
Apr 10 '17 at 3:21 on
source share

Maybe you can try this, but I'm not sure if this is what you want:

 #include <X11/Xlib.h> Display* disp = XOpenDisplay(NULL); Screen* scrn = DefaultScreenOfDisplay(disp); int height = scrn->height; int width = scrn->width; //Create window for the ip cam video cv::namedWindow("Front", CV_WINDOW_NORMAL); cvSetWindowProperty( "Front", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN ); //Position of the screen where the video is shows cvMoveWindow("Front", 0, 0); cvResizeWindow( "Front", width, height ); 

This way you get a full screen for any screen.

-2
Sep 04 '14 at 14:52
source share



All Articles