Linux 上的 OpenCV(通过 python):设置框架宽度/高度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11522755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 13:49:46  来源:igfitidea点击:

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

pythonlinuxopencvwebcamv4l2

提问by Mike Lawrence

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:

我在 linux (ubuntu 12.04) 上通过 python 使用 openCV,我有一个罗技 c920,我想从中抓取图像。Cheese 能够以非常高的分辨率抓取帧,但是每当我尝试使用 openCV 时,我只能得到 640x480 的图像。我试过了:

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:

但这会在最后两行中的每一行之后产生“0”的输出,并且当我随后通过以下方式抓取帧时:

image = cv.QueryFrame(cam)

The resulting image is still 640x480.

生成的图像仍然是 640x480。

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

我试过通过(在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:

我确实可以通过以下方式操纵相机的设置(再次,在python之外):

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

但是当我弹出 python shell 时,上面的代码的行为与以前完全相同(尝试设置属性并获取 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.

能够提高捕获的分辨率对我来说非常关键,所以我非常感谢任何人可以提供的任何指示。

回答by Sam

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.

这有点hacky,所以期待崩溃。

回答by Babu

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

函数 cvSetCaptureProperty 设置视频捕获的指定属性。目前该函数仅支持视频文件: CV_CAP_PROP_POS_MSEC、CV_CAP_PROP_POS_FRAMES、CV_CAP_PROP_POS_AVI_RATIO。

注意这个函数目前在使用 FFMPEG 的 linux 上最新的 CVS 下载时没有任何作用(如果使用并返回 0,则函数内容被隐藏)。

回答by adam

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.

AFIK、Logitech 相机对 linux 的支持特别差(尽管它变得更好了)他们的大多数问题都与对焦控制等高级功能有关。我建议坚持使用基本相机(IE 手动对焦罗技相机)只是为了安全起见。

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.

请确保您设置的分辨率是您的相机支持的,否则 v4l 会对您大喊大叫。如果我设置了不受支持的原生分辨率,则成功率为零。

回答by Kieran Inductance D

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

回答by wennho

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.

我和你有同样的问题。最终进入 OpenCV 源并更改第modules/highgui/src/cap_v4l.cpp245-246 行中的默认参数并重建项目。

#define DEFAULT_V4L_WIDTH  1920
#define DEFAULT_V4L_HEIGHT 1080

This is for OpenCV 2.4.8

这是针对 OpenCV 2.4.8