在OpenCV/Python中设置相机参数

2024-03-29 02:19:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用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)

我的问题是:

是否可以通过python/opencv设置相机曝光时间(或其他相机参数)?

如果没有,我将如何设置这些参数?

注:由相机制造商提供的C++代码显示了如何做到这一点,但我不是C++中的专家(长镜头),并且会欣赏任何基于Python的解决方案。

提前谢谢!


Tags: oftheinimageonlyfor参数video
3条回答

并非所有相机都支持所有参数-实际上,它们是OpenCV库中最麻烦的部分之一。每种相机类型-从android相机到USB相机再到专业相机,都提供不同的界面来修改其参数。OpenCV代码中有许多分支可以支持其中的许多分支,但当然并不是所有的可能性都包含在内。

你能做的就是调查你的相机驱动程序,为OpenCV写一个补丁并发送到code.OpenCV.org。这样别人会喜欢你的工作,就像你喜欢别人一样。

还有一种可能是你的相机不支持你的要求-大多数USB摄像头是便宜和简单的。也许这个参数不能用于修改。

如果你确信相机支持给定的PARAM(你说相机制造商提供了一些代码),并且不想弄乱OpenCV,那么你可以在C++中用Boosi::Python包装这个示例代码,使其在Python中可用。然后,享受使用它。

我对覆盆子皮的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)

你必须使用的时间可能不同

为了避免使用整数值来标识VideoCapture属性,可以在OpenCV 2.4中使用cv2.cv.CV_CAP_PROP_FPS,在OpenCV 3.0中使用cv2.CAP_PROP_FPS。(另见斯特凡下面的评论。)

这里有一个对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)

OPCV3在我的实用程序代码中设置如下:

from pkg_resources import parse_version
OPCV3 = parse_version(cv2.__version__) >= parse_version('3')

相关问题 更多 >