OpenCV (via python) on Linux: set frame width / height?

I am using openCV via python on linux (ubuntu 12.04) and I have a logitech c920 from which I would like to capture images. Cheese is capable of capturing frames to really high resolution, but whenever I try to use openCV, I only get 640x480 images. I tried:

import cv cam = cv.CaptureFromCAM(-1) cv.SetCaptureProperty(cam,cv.CV_CAP_PROP_FRAME_WIDTH,1920) cv.SetCaptureProperty(cam,cv.CV_CAP_PROP_FRAME_WIDTH,1080) 

but this gives the result "0" after each of the last two lines, and when I subsequently capture the frame through:

 image = cv.QueryFrame(cam) 

The resulting image remains 640x480.

I tried to install what looks like related tools through (outside of python):

 sudo apt-get install libv4l-dev v4l-utils qv4l2 v4l2ucp 

and I can really manipulate the camera settings (again, outside of python) with:

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

and note that:

 v4l2-ctl -V 

really assumes something has changed:

 Format Video Capture: Width/Height : 1920/1080 Pixel Format : 'H264' Field : None Bytes per Line : 3840 Size Image : 4147200 Colorspace : sRGB 

But when I enter the python shell, the above code behaves exactly the same as before (print zeros when trying to set properties and get a 640x480 image).

The ability to raise capture resolution is pretty important to me, so I would really appreciate any pointers anyone can provide.

+12
python linux opencv webcam v4l2
Jul 17 2018-12-17T00:
source share
6 answers

From the docs ,

The cvSetCaptureProperty function sets the specified video capture property. Currently, the function only supports video files: CV_CAP_PROP_POS_MSEC, CV_CAP_PROP_POS_FRAMES, CV_CAP_PROP_POS_AVI_RATIO.

NB This function currently does nothing when using the latest CVS download on Linux with FFMPEG (the contents of the function are hidden if 0 is used and returns).

+3
Jul 26 2018-12-12T00:
source share

I had the same problem as you. The transition to the OpenCV source code and changing the default parameters in modules/highgui/src/cap_v4l.cpp , lines 245-246 and rebuilding of the project have modules/highgui/src/cap_v4l.cpp .

 #define DEFAULT_V4L_WIDTH 1920 #define DEFAULT_V4L_HEIGHT 1080 

This is for OpenCV 2.4.8

+3
Mar 10 '14 at 16:04
source share

It seems to have been changed by a cammer.

AFIK, Logitech cameras have particularly poor linux support (although this has gotten better). Most of their problems are related to advanced features such as focus control. I would advise sticking to the main cameras (IE manual focus Logitech cameras) to play it safely.

My built-in laptop camera has no problems and is displayed at normal resolution.
My external logitech user has activation issues.

However, I can overcome the resolution issue with these two lines.

Yes, they are the same as you.

 cv.SetCaptureProperty(self.capture,cv.CV_CAP_PROP_FRAME_WIDTH, 1280) cv.SetCaptureProperty(self.capture,cv.CV_CAP_PROP_FRAME_HEIGHT, 720) 

My Logitech still causes errors, but the resolution is ok.

Please make sure that the resolution you set is supported by your camera, or v4l will yell at you. If I install an unsupported native permission, I have zero success.

+2
Nov 24 '12 at 2:19
source share

Not sure if it works, but you can try to force the parameters to their values ​​after creating the camera object:

 import cv cam = cv.CaptureFromCAM(-1) os.system("v4l2-ctl --set-fmt-video=width=1920,height=1080,pixelformat=1") os.system("v4l2-ctl --set-parm=30") image = cv.QueryFrame(cam) 

This is a bit hacky, so expect a crash.

+1
Jul 17 2018-12-12T00:
source share
  ## Sets up the camera to capture video cap = cv2.VideoCapture(device) 

width = 1280, height = 720

 #set the width and height cap.set(3,width) cap.set(4,height) 
0
Dec 19 '13 at 13:32
source share

OpenCV automatically selects the first available capture backend (see here) . It may happen that he does not use V4L2 automatically.

Also set -D WITH_V4L=ON and -D WITH_LIBV4L=ON during assembly.

To set the pixel format used, set the capture property CAP_PROP_FOURCC :

  capture = cv2.VideoCapture(self.cam_id, cv2.CAP_V4L2) scapture.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')) width = 1920 height = 1080 capture.set(cv2.CAP_PROP_FRAME_WIDTH, width) capture.set(cv2.CAP_PROP_FRAME_HEIGHT, height) 
0
Jul 14 '19 at 12:19
source share



All Articles