【发布时间】:2012-07-10 08:50:23
【问题描述】:
我将 OpenCV (2.4) 和 Python (2.7.3) 与 Thorlabs (DC1545M) 的 USB 摄像头一起使用。
我正在对视频流进行一些图像分析,我希望能够从我的视频流中更改一些相机参数。令人困惑的是,我可以更改某些相机属性,但不能更改所有属性,而且我不确定自己做错了什么。
这是代码,在 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()
作为参考,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)
(请注意,正如评论者 Markus Weber 在下面指出的那样,在 OpenCV 4 中,您必须从属性名称中删除“CV”前缀,例如
cv2.CV_CAP_PROP_FRAME_HEIGHT -> cv2.CAP_PROP_FRAME_HEIGHT)
我的问题是:
是否可以通过 python/opencv 设置相机曝光时间(或其他相机参数)?
如果没有,我将如何设置这些参数?
注意:相机制造商提供的 C++ 代码显示了如何执行此操作,但我不是 C++ 专家(从长远来看),我希望任何基于 python 的解决方案。
提前致谢!
【问题讨论】:
-
那么第 15 个选项是什么,即 CV_CAP_PROP_EXPOSURE ?不是曝光时间吗?
-
是的。我要说的是,尝试修改确实会产生任何可衡量的结果。 (这是
cap.set(15, x),x的值不会产生可衡量的变化。)顺便说一句,谢谢你的博客。它对我通过 python 熟悉 opencv 非常有用。你会为此投赞成票:) -
他编号错误。他们都太高了。请有人编辑它。
-
@KatasticVoyage 完成,现在对我来说似乎是正确的 :)
-
对于 opencv 4,我必须从属性名称中删除“CV”前缀。所以
cv2.CV_CAP_PROP_FRAME_HEIGHT->cv2.CAP_PROP_FRAME_HEIGHT
标签: python opencv camera python-2.7