在 OpenCV/Python 中设置相机参数 [英] Setting Camera Parameters in OpenCV/Python

查看:120
本文介绍了在 OpenCV/Python 中设置相机参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 OpenCV (2.4) 和 Python (2.7.3) 以及来自 Thorlabs (DC1545M) 的 USB 摄像头.

I am using OpenCV (2.4) and Python (2.7.3) with a USB camera from Thorlabs (DC1545M).

我正在对视频流进行一些图像分析,我希望能够从我的视频流中更改一些相机参数.令人困惑的是,我可以更改某些相机属性,但不能更改所有属性,而且我不确定自己做错了什么.

I am doing some image analysis on a video stream and I would like to be able to change some of the camera parameters from my video stream. The confusing thing is that I am able to change some of the camera properties but not all of them, and I am unsure of what I am doing wrong.

这是代码,使用 Python 中的 cv2 绑定,我可以确认它运行:

Here is the code, using the cv2 bindings in Python, and I can confirm that it runs:

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()命令中的第一个参数是指相机属性的枚举,如下所示:

For reference, the first argument in the cap.set() command refers to the enumeration of the camera properties, listed below:

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)

(Please note, as commenter Markus Weber pointed out below, in OpenCV 4 you have to remove the "CV" prefix from the property name, eg cv2.CV_CAP_PROP_FRAME_HEIGHT -> cv2.CAP_PROP_FRAME_HEIGHT)

我的问题是:

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

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

注意:相机制造商提供了 C++ 代码,展示了如何执行此操作,但我不是 C++ 专家(从长远来看),并且会欣赏任何基于 Python 的解决方案.

Note: There is C++ code provided by the camera manufacturer showing how to do this, but I'm not an expert (by a long shot) in C++ and would appreciate any python-based solution.

提前致谢!

推荐答案

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

Not all parameters are supported by all cameras - actually, they are one of the most troublesome part of the OpenCV library. Each camera type - from android cameras to USB cameras to professional ones offer a different interface to modify its parameters. There are many branches in OpenCV code to support as many of them, but of course not all possibilities are covered.

您可以做的是调查您的相机驱动程序,为 OpenCV 编写补丁并将其发送到 code.opencv.org.这样别人会喜欢你的工作,就像你喜欢别人的一样.

What you can do is to investigate your camera driver, write a patch for OpenCV and send it to code.opencv.org. This way others will enjoy your work, the same way you enjoy others'.

您的相机也有可能不支持您的请求 - 大多数 USB 摄像头既便宜又简单.也许那个参数只是不能修改.

There is also a possibility that your camera does not support your request - most USB cams are cheap and simple. Maybe that parameter is just not available for modifications.

如果您确定相机支持给定的参数(您说相机制造商提供了一些代码)并且不想弄乱 OpenCV,您可以使用 boost::python 将该示例代码包装在 C++ 中,以使其可用在 Python 中.然后,享受使用它.

If you are sure the camera supports a given param (you say the camera manufacturer provides some code) and do not want to mess with OpenCV, you can wrap that sample code in C++ with boost::python, to make it available in Python. Then, enjoy using it.

这篇关于在 OpenCV/Python 中设置相机参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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