问题描述
我使用来自Thorlabs(DC1545M)的USB相机使用OpenCV(2.4)和Python(2.7.3)。
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视频的当前位置
0.
CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds.
- CV_CAP_PROP_POS_FRAMES下一个要解码/捕获的帧的基于0的索引。
- CV_CAP_PROP_POS_AVI_RATIO视频文件的相对位置
- CV_CAP_PROP_FRAME_WIDTH视频流中的帧宽度。
- CV_CAP_PROP_FRAME_HEIGHT视频流中的帧高
- CV_CAP_PROP_FPS帧率。
- CV_CAP_PROP_FOURCC编解码器的4个字符代码。
- CV_CAP_PROP_FORMAT由retrieve()返回的Mat对象的格式。
- CV_CAP_PROP_MODE表示当前捕获模式的后端特定值。 CV_CAP_PROP_CONTRAST图片的对比度(仅适用于相机)。 $ b
- CV_CAP_PROP_BRIGHTNESS图片的亮度(仅适用于相机) $ b
- CV_CAP_PROP_SATURATION图片的饱和度(仅适用于相机)。
- CV_CAP_PROP_HUE图片的色调(仅适用于相机)。
- CV_CAP_PROP_GAIN
- CV_CAP_PROP_CONVERT_RGB指示图片是否应转换为图片的布尔值标记
- CV_CAP_PROP_RECTIFICATION立体相机的校正标志(注意:目前只有DC1394 v 2.x后端支持) CV_CAP_PROP_WHITE_BALANCE目前不支持 >
- CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
- CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file
- CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
- CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
- CV_CAP_PROP_FPS Frame rate.
- CV_CAP_PROP_FOURCC 4-character code of codec.
- CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
- CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
- CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
- CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
- CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
- CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
- CV_CAP_PROP_HUE Hue of the image (only for cameras).
- CV_CAP_PROP_GAIN Gain of the image (only for cameras).
- CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
- CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
- CV_CAP_PROP_WHITE_BALANCE Currently unsupported
- CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)
我的问题是:
可以设置相机曝光时间(或其他相机参数)通过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库中最麻烦的部分之一。每个摄像机类型 - 从android摄像机到usb摄像机到专业摄像机提供了一个不同的接口来设置其参数。在OpenCV代码中有许多分支可以支持尽可能多的分支,但是当然不会涵盖所有的可能性。
Not all the parameters are supported by all the cameras - actually, they are one of the most troublesome part of the OpenCV library. Each camera type - from android cameras to usb cameras to proffesional ones offer a different interface to set its parameters. And there are many branches in OpenCV code to support as many of them, but of course not all the possibilities are covered.
你可以做的是调查你的相机驱动程序,对OpenCV进行补丁并将其发送到code.opencv.org。
What you can do is to investigate your camera driver, make a patch to OpenCV and send it to code.opencv.org. This way others will enjoy your work, the same way you enjoy other's.
还有一个可能性是你的相机不支持你的要求 - 大多数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,你可以包装示例代码在C + + boost :: python,使其可用在python。然后,享受使用它。
If you are sure the camera support 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中设置摄像机参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!