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.

Mike Lawrence
  • 1,641
  • 5
  • 20
  • 40

5 Answers5

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).

Community
  • 1
  • 1
Babu
  • 2,548
  • 3
  • 30
  • 47
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

wennho
  • 123
  • 1
  • 7
  • 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. – Ulrich Stern Aug 18 '15 at 19:45
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.

adam
  • 21
  • 1
  • 2
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.

Sam
  • 19,708
  • 4
  • 59
  • 82
  • 1
    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. – Mike Lawrence Jul 17 '12 at 13:15
  • 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. – Sam Jul 17 '12 at 13:23
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)
petezurich
  • 9,280
  • 9
  • 43
  • 57