Configuring camera settings in OpenCV / Python

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

I am doing image analysis in a video stream, and I would like to be able to change some camera settings from my video stream. It is confusing that I can change some properties of the camera, but not all of them, and I'm not sure what I'm doing wrong.

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

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() 

For reference, the first argument to cap.set () refers to listing 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) 

My questions:

Is it possible to set the camera exposure time (or other camera parameters) via python / opencv?

If not, how would I decide to set these parameters?

Note. The C ++ code provided by the camera manufacturer shows how to do this, but I am not an expert (long shot) in C ++ and would appreciate any python-based solution.

Thank you in advance!

+57
python opencv camera
Jul 10 '12 at 19:45
source share
5 answers

Not all options are supported by all cameras - in fact, this is one of the most unpleasant parts of the OpenCV library. Each type of camera - from android cameras to USB cameras to professional ones - offers a different interface for changing its parameters. There are many branches in OpenCV code to support many of them, but, of course, not all features are covered.

What you can do is investigate your camera driver, write a patch for OpenCV and send it to code.opencv.org. In this way, others will enjoy your work, just as you like others. "

It is also likely that your camera does not support your request - most USB cameras are cheap and simple. Perhaps this option is simply not available for modifications.

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

+36
Jul 11 '12 at 13:00
source share

To avoid using integer values ​​to identify the properties of VideoCapture , you can use, for example, cv2.cv.CV_CAP_PROP_FPS in OpenCV 2.4 and cv2.CAP_PROP_FPS in OpenCV 3.0. (See also Stephen's comment below.)

Here's a utility function that works for both OpenCV 2.4 and 3.0:

 # returns OpenCV VideoCapture property id given, eg, "FPS" def capPropId(prop): return getattr(cv2 if OPCV3 else cv2.cv, ("" if OPCV3 else "CV_") + "CAP_PROP_" + prop) 

OPCV3 installed earlier in my utility code as follows:

 from pkg_resources import parse_version OPCV3 = parse_version(cv2.__version__) >= parse_version('3') 
+22
Feb 08 '13 at 16:09
source share

I had the same problem with openCV on a raspberry Pi ... I don't know if this could solve your problem, but what worked for me was

 import time import cv2 cap = cv2.VideoCapture(0) cap.set(3,1280) cap.set(4,1024) time.sleep(2) cap.set(15, -8.0) 

the time you should use may vary

+18
Jan 24 '13 at 20:57
source share

I also could not solve the OpenCV problem, but the workaround method video4linux (V4L2) works with OpenCV when using Linux. At least this is happening on my Raspberry Pi with race and my cheap webcam. It's not as solid, lightweight, and portable as you would like, but for some situations it can be very useful.

Make sure the v4l2-ctl application is installed, for example. from the Debian v4l-utils package. Then execute (before running the python application or from the inside) the command:

 v4l2-ctl -d /dev/video1 -c exposure_auto=1 -c exposure_auto_priority=0 -c exposure_absolute=10 

It overwrites the camera shutter time for manual settings and changes the shutter time (in milliseconds) with the last parameter (in this example) 10. The lower this value, the darker the image.

+6
Aug 04 '14 at 11:58 a.m.
source share

If someone is still wondering what might be in CV_CAP_PROP_EXPOSURE :

It depends. For my cheap webcam, I need to enter the desired value directly, for example. 0.1 in 1/10 seconds. For my expensive industrial camera, I have to enter -5 to get an exposure time of 2 ^ -5s = 1 / 32s.

+1
May 29 '16 at 23:12
source share



All Articles