如何更改 OpenCV 不支持但 v4l2 API 支持的网络摄像头属性? [英] How can I change webcam properties that OpenCV doesn't support but v4l2 API does?

查看:95
本文介绍了如何更改 OpenCV 不支持但 v4l2 API 支持的网络摄像头属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 OpenCV 3.1Python 2.7 从我的网络摄像头 Logitech C270 捕获视频帧.我也在使用 video4linux2(v4l2) 来设置我的相机的属性,但这导致了一些问题.我的操作系统是 Ubuntu 15.04.

I'm using OpenCV 3.1 and Python 2.7 to capture video frames from my webcam, Logitech C270. I'm also using video4linux2(v4l2) to set the properties of my camera but this led to a few problems. My OS is Ubuntu 15.04.

我要更改的特定属性是absolute_exposure.

The specific property I'm trying to change is absolute_exposure.

我可以通过终端使用 v4l2 API 手动更改它,使用命令 v4l2-ctl --set-ctrl Exposure_absolute=40,效果很好但我需要为此任务编写脚本.

I'm able to change it manually using v4l2 API via terminal, with the command v4l2-ctl --set-ctrl exposure_absolute=40, and it works nice but I need to write a script for this task.

使用 OpenCV 的 set(cv2.CAP_PROP_EXPOSURE, 20) 会导致视频错误:V4L:设备不支持属性曝光(15)".我确定网络摄像头支持更改此属性,因为可以使用 v4l2 这样做,然后我假设问题出在 OpenCV 的包装器上.

Using OpenCV's set(cv2.CAP_PROP_EXPOSURE, 20) leads to "VIDEOIO ERROR: V4L: Property Exposure(15) not supported by device". I'm sure the webcam supports the change of this property since it's possible to do so using v4l2, then I assume the problem is with OpenCV's wrapper.

我还尝试使用 subprocess 库发送终端命令并使用 v4l2 更改属性.命令是subprocess.call('v4l2-ctl --device=/dev/video0 --set-ctrl Exposure_absolute=20', shell=True).

I also tried to use subprocess lib to send a terminal command and change the property using v4l2. The command is subprocess.call('v4l2-ctl --device=/dev/video0 --set-ctrl exposure_absolute=20', shell=True).

结果是 Exposure_absolute 发生了变化,但它不适用于我当前的视频捕获.图 1 显示了通过脚本设置属性后的结果.图 2 显示了通过终端设置相同属性后的结果,相同的视频捕获处于活动状态.

The result is that exposure_absolute changes but it isn't applied to my current video capture. Image 1 shows the result after setting the property via script. Image 2 shows the result after setting the same property via terminal, with the same video capture active.

通过脚本设置exposure_absolute(图1)

通过终端设置exposure_absolute(图片2)

图像 2 是在图像 1 之后拍摄的,突出显示的线条与图像 1 相同.

Image 2 was taken right after image 1, the highlighted line is the same of image 1.

我在子进程调用中做错了什么吗?或者如何使用脚本更改此属性?

Am I doing something wrong on the subprocess call? Or how can I make the change of this property using a script?

另外,为什么 cv2.VideoCapture(id) 重置相机属性,在运行脚本之前更改它们没有用,是否可以停止?

Also, why cv2.VideoCapture(id) resets the camera properties, it's no use changing them before running the script, and is it possible to stop that?

__________________________________________________

我可能找到了解决此问题的方法.子进程调用确实是正确的,我只需要在更改属性之前使用 cv2.read() 一次,显然第一个 cv2.read() 是相机属性的位置被重置.我仍然不知道如何阻止它自动重置网络摄像头的属性.

I maybe found a workaround for this problem. The subprocess call is indeed right, I just had to use cv2.read() once before changing the properties, apparently the first cv2.read() is where the camera properties are reset. I still don't know how to stop it from automatically resetting webcam's properties though.

推荐答案

如果您使用 GStreamer 支持构建 opencv(标志:-D WITH_GSTREAMER=ON),您可以使用 GStreamer 管道打开 VideoCapture,您可以在其中指定所有类型的参数对于 v4l2:

If you build opencv with GStreamer support (flag: -D WITH_GSTREAMER=ON) you can open a VideoCapture using a GStreamer pipeline where you can specify all kind of parameters for v4l2:

std::string cameraPipeline;
cameraPipeline ="v4l2src device=/dev/video0 extra-controls=\"c,exposure_auto=1,exposure_absolute=500\" ! ";
cameraPipeline+="video/x-raw, format=BGR, framerate=30/1, width=(int)1280,height=(int)720 ! ";
cameraPipeline+="appsink";

VideoCapture cap;
cap.open(cameraPipeline);

这适用于 C++ 和 Python.您可以通过在终端中输入以下内容来获取完整的控件列表:v4l2-ctl --list-ctrls-menus

This is works in C++ and Python. You can get the full list of controls by typing this in a terminal : v4l2-ctl --list-ctrls-menus

这篇关于如何更改 OpenCV 不支持但 v4l2 API 支持的网络摄像头属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆