OpenCV (via python) on Linux: Set frame width/height?
Asked Answered
S

6

13

I'm using openCV via python on linux (ubuntu 12.04), and I have a logitech c920 from which I'd like to grab images. Cheese is able to grab frames up to really high resolutions, but whenever I try to use openCV, I only get 640x480 images. I have 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 yields output of "0" after each of the last two lines, and when I subsequently grab a frame via:

image = cv.QueryFrame(cam)

The resulting image is still 640x480.

I've tried installing what seemed to be related tools via (outside of python):

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

and I can indeed apparently manipulate the camera's settings (again, outside of python) via:

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

and observe that:

v4l2-ctl -V

indeed suggests that something has been 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 pop into the python shell, the above code behaves exactly the same as before (printing zeros when trying to set the properties and obtaining an image that is 640x480).

Being able to bump up the resolution of the capture is pretty mission critical for me, so I'd greatly appreciate any pointers anyone can provide.

Squib answered 17/7, 2012 at 12:46 Comment(4)
I'm having the same problem with cv2 (OpenCV 2.4.2) in ubuntu 12.04. Previously, it used to work fine.Rai
@Froyo: What versions of opencv and ubuntu did you have this working on? I might just revert my system to get this working...Squib
OpenCV 2.3.1 and ubuntu 11.10Rai
I recently answered a similar question and think the answer should work for you, too.Shuttlecock
G
3

From the docs,

The function cvSetCaptureProperty sets the specified property of video capturing. Currently the function supports only 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 function contents are hidden if 0 is used and returned).

Glauce answered 26/7, 2012 at 8:25 Comment(0)
B
3

I had the same problem as you. Ended up going into the OpenCV source and changing the default parameters in modules/highgui/src/cap_v4l.cpp, lines 245-246 and rebuilding the project.

#define DEFAULT_V4L_WIDTH  1920
#define DEFAULT_V4L_HEIGHT 1080

This is for OpenCV 2.4.8

Bigham answered 10/3, 2014 at 16:4 Comment(1)
Also works for OpenCV 3.0.0. (I reduced resolution to 320x240 in my case.) File to edit has changed to modules/videoio/src/cap_v4l.cpp for OpenCV 3.0.Shuttlecock
D
2

It seems to be variable by cammera.

AFIK, Logitech cameras have particularly bad linux support (though It;s gotten better) Most of their issues are with advanced features like focus control. i would advise sticking with basic cameras (IE manual focus Logitech cameras) just to play it safe.

My built in laptop camera has no issue and displays at normal resolution.
My external logitech pro has issues initalizing.

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

Yes, they are the same as you used.

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 throws errors but the resolution is fine.

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

Desegregate answered 24/11, 2012 at 2:19 Comment(0)
W
1

Not sure if it works, but you can try to force the parameters to your values after you instantiate 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)

That's a bit hacky, so expect a crash.

Workroom answered 17/7, 2012 at 12:51 Comment(2)
Good idea, but no luck. When I try the os.system() calls, I get print-out that says VIDIOC_S_FMT: failed: Device or resource busy, which suggests to me that when cam = cv.CaptureFromCAM(-1) is called, opencv locks the camera as a resource, preventing v4l2-ctl from modifying it.Squib
hmmm, something I should have expected... You can try working on OpenCV code, to see what commands it sends to the camera, and what command should be sent in order to work. It may be that a simple one-line change in OpenCV will make it work.Workroom
W
1
## 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)
Weigela answered 19/12, 2013 at 13:32 Comment(0)
C
0

Set camera property after open (opencv_version 4.4.0):

if (!camera.open(0)) {
    cameraStatus = CAM_ERROR;
    return;
}
        
camera.set(cv::CAP_PROP_FPS, 30);
camera.set(cv::CAP_PROP_FRAME_WIDTH, 1920);
camera.set(cv::CAP_PROP_FRAME_HEIGHT, 1080);
Clarissa answered 1/12, 2023 at 10:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.