Setting Camera Parameters in OpenCV/Python
Asked Answered
P

5

107

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.

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(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 1024)
cap.set(cv2.cv.CV_CAP_PROP_EXPOSURE, 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 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)

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

My questions are:

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

If not, how would I go about setting these parameters?

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.

Pierre answered 10/7, 2012 at 19:45 Comment(8)
So what is 15th option, ie CV_CAP_PROP_EXPOSURE ? Isn't it exposure time?Lilybel
It is. The point I was making is that trying to modify that does produce any measurable result. (that's the cap.set(15, x), where no value of x produces a measurable change.) By the way, thank you for your blog. It has been extremely useful to me in getting up to speed with opencv through python. You get an upvote for that :)Pierre
last line: cv2.VideoCapture(0).release() can simply be cap.release() :)Patty
Is option 2 omitted by mistake?Headway
He misnumbered them. They're all ONE TOO HIGH. Someone please edit it.Bibliophage
@KatasticVoyage done, seems right to me now :)Shabby
bring the largest frame set cam.set(3,4000) cam.set(4,4000) worked for meLounge
For opencv 4 I have to remove the "CV" prefix from the property names. So cv2.CV_CAP_PROP_FRAME_HEIGHT -> cv2.CAP_PROP_FRAME_HEIGHTOletta
W
66

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.

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'.

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.

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.

Wavy answered 11/7, 2012 at 13:0 Comment(2)
is there a working list of supported cameras for opencv on mac linux windows? it sounds like in the future i will start with the software and then work back to the hardware before purchase. writing a camera driver is a good goal but it will be a while before i can learn to do that.Widgeon
@sammy I've posted a question about how to do this. Would you care to elaborate here: #27915280Consideration
T
31

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

import time
import cv2


cap = cv2.VideoCapture(0)

cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)

cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1024)

time.sleep(2)

cap.set(cv2.CAP_PROP_EXPOSURE, -8.0)

the time you have to use can be different

Testudinal answered 24/1, 2013 at 20:57 Comment(7)
wow, your code made my logitech c615 webcam work on my macbook with opencv! "it just works!" i had been trying this for days. i don't think it is quite HD and not sure if i can change the focus parameters, but a really good start.Widgeon
I'm by no means an expert, and 3, 4 and 15 are magic numbers to me. Is it a replacement for e.g. CV_CAP_PROP_FRAME_WIDTH?Conditional
what does cap.set(15, -8.0) mean? I am trying to set the exposure time on my logitech webcam.Widgeon
@Widgeon have you set exposure time on Logitech webcam?Protozoal
@AllanNørgaard yes, the flags are just integers in disguiseRecognizor
It's very bad practice to use the actual integers instead of the constants. It defeats the whole idea of constants and will break your code eventually.Tuft
I've fixed the code to use named constants. people are copying this code, thinking it's good...Trenatrenail
O
26

To avoid using integer values to identify the VideoCapture properties, one can use, e.g., cv2.cv.CV_CAP_PROP_FPS in OpenCV 2.4 and cv2.CAP_PROP_FPS in OpenCV 3.0. (See also Stefan's comment below.)

Here a utility function that works for both OpenCV 2.4 and 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 is set earlier in my utilities code like this:

from pkg_resources import parse_version
OPCV3 = parse_version(cv2.__version__) >= parse_version('3')
Ottilie answered 8/2, 2013 at 16:9 Comment(3)
I spent 10 minutes trying to find where those constants were defined in Python - thank you!Gintz
you are right - if you use a version like 2.4.9 (2014-04-25 the stable download from opencv.org) you have to use the cv2.cv.CV_CAP_PROP_XXXX thing - for more infos about this see code.opencv.org/issues/3181 if you use the latest branch you can use cv2.CAP_PROP_XXXXTamper
The latest list of Video Capture generic properties is here (OpenCV 4.3)Brewton
C
9

I wasn't able to fix the problem OpenCV either, but a video4linux (V4L2) workaround does work with OpenCV when using Linux. At least, it does on my Raspberry Pi with Rasbian and my cheap webcam. This is not as solid, light and portable as you'd like it to be, but for some situations it might be very useful nevertheless.

Make sure you have the v4l2-ctl application installed, e.g. from the Debian v4l-utils package. Than run (before running the python application, or from within) the command:

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

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

Courtesan answered 4/8, 2014 at 11:58 Comment(2)
This worked on my Toshiba Intel-based laptop running Ubuntu 14.04. I used v4l2-ctl to set any option I wanted, including frame rate. I did try these settings with OpenCV but got the same errors as those above.Hochheimer
v4l2-ctl -l to get all parameters.Insensible
C
6

If anyone is still wondering what the value in CV_CAP_PROP_EXPOSURE might be:

Depends. For my cheap webcam I have to enter the desired value directly, e.g. 0.1 for 1/10s. For my expensive industrial camera I have to enter -5 to get an exposure time of 2^-5s = 1/32s.

Cessation answered 29/5, 2016 at 23:12 Comment(1)
the set function of OpenCV can handle integer number?Singer

© 2022 - 2024 — McMap. All rights reserved.