Mik*_*ike 77 python camera opencv python-2.7
我正在使用OpenCV(2.4)和Python(2.7.3)以及Thorlabs的USB摄像头(DC1545M).
我正在对视频流进行一些图像分析,我希望能够从我的视频流中更改一些摄像机参数.令人困惑的是,我能够改变一些相机属性而不是所有相机属性,我不确定我做错了什么.
这是代码,使用Python中的cv2绑定,我可以确认它运行:
import cv2
#capture from camera at location 0
cap = cv2.VideoCapture(0)
#set the width and height, and UNSUCCESSFULLY set the exposure time
cap.set(3,1280)
cap.set(4,1024)
cap.set(15, 0.1)
while True:
ret, img = cap.read()
cv2.imshow("input", img)
#cv2.imshow("thresholded", imgray*thresh2)
key = cv2.waitKey(10)
if key == 27:
break
cv2.destroyAllWindows()
cv2.VideoCapture(0).release()
Run Code Online (Sandbox Code Playgroud)
作为参考,cap.set()命令中的第一个参数引用了相机属性的枚举,如下所示:
0. CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds.
1. CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
2. CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file
3. CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
4. CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
5. CV_CAP_PROP_FPS Frame rate.
6. CV_CAP_PROP_FOURCC 4-character code of codec.
7. CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
8. CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
9. CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
10. CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
11. CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
12. CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
13. CV_CAP_PROP_HUE Hue of the image (only for cameras).
14. CV_CAP_PROP_GAIN Gain of the image (only for cameras).
15. CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
16. CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
17. CV_CAP_PROP_WHITE_BALANCE Currently unsupported
18. CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)
Run Code Online (Sandbox Code Playgroud)
我的问题是:
是否可以通过python/opencv设置相机曝光时间(或其他相机参数)?
如果没有,我将如何设置这些参数?
注意:相机制造商提供的C++代码显示了如何执行此操作,但我不是C++中的专家(通过长镜头),并且会欣赏任何基于python的解决方案.
提前致谢!
Sam*_*Sam 47
并非所有摄像机都支持所有参数 - 实际上,它们是OpenCV库中最麻烦的部分之一.每种相机类型 - 从Android相机到USB相机到专业相机都提供了不同的界面来修改其参数.OpenCV代码中有许多分支支持尽可能多的分支,但当然并非所有可能性都包括在内.
您可以做的是调查您的相机驱动程序,为OpenCV编写补丁并将其发送到code.opencv.org.通过这种方式,其他人将享受您的工作,就像您享受他人一样.
您的相机也可能不支持您的要求 - 大多数USB摄像头都很便宜且简单.也许该参数不可用于修改.
如果您确定相机支持给定的参数(您说相机制造商提供了一些代码)并且不想弄乱OpenCV,您可以使用boost :: python将该示例代码包装在C++中,以使其在Python中可用.然后,享受使用它.
小智 25
我在Raspberry Pi上遇到了与openCV相同的问题...不知道这是否可以解决你的问题,但对我有用的是
import time
import cv2
cap = cv2.VideoCapture(0)
cap.set(3,1280)
cap.set(4,1024)
time.sleep(2)
cap.set(15, -8.0)
Run Code Online (Sandbox Code Playgroud)
你必须使用的时间可能会有所不同
Ulr*_*ern 25
为了避免使用整数值来标识VideoCapture属性,可以使用例如cv2.cv.CV_CAP_PROP_FPSOpenCV 2.4和cv2.CAP_PROP_FPSOpenCV 3.0.(另见Stefan的评论如下.)
这里有一个适用于OpenCV 2.4和3.0的实用程序功能:
# returns OpenCV VideoCapture property id given, e.g., "FPS"
def capPropId(prop):
return getattr(cv2 if OPCV3 else cv2.cv,
("" if OPCV3 else "CV_") + "CAP_PROP_" + prop)
Run Code Online (Sandbox Code Playgroud)
OPCV3 在我的实用程序代码中设置如下:
from pkg_resources import parse_version
OPCV3 = parse_version(cv2.__version__) >= parse_version('3')
Run Code Online (Sandbox Code Playgroud)
小智 7
我无法解决OpenCV的问题,但是在使用Linux时,video4linux(V4L2)解决方法可以与 OpenCV一起使用.至少,它是我的Raspberry Pi与Rasbian和我的廉价网络摄像头.这不像你想要的那样坚固,轻便和便携,但在某些情况下它可能非常有用.
确保安装了v4l2-ctl应用程序,例如从Debian v4l-utils软件包中安装.比运行(在运行python应用程序之前,或从内部运行)命令:
v4l2-ctl -d /dev/video1 -c exposure_auto=1 -c exposure_auto_priority=0 -c exposure_absolute=10
Run Code Online (Sandbox Code Playgroud)
它会将相机快门时间覆盖为手动设置,并将最后一个参数的快门时间(以毫秒为单位)更改为(在此示例中)10.此值越低,图像越暗.
小智 6
如果有人仍然想知道其中的价值CV_CAP_PROP_EXPOSURE可能是什么:
依靠。对于我的廉价网络摄像头,我必须直接输入所需的值,例如 0.1 表示 1/10 秒。对于我昂贵的工业相机,我必须输入 -5 才能获得 2^-5s = 1/32s 的曝光时间。