Is there a way to adjust shutter speed or exposure time of a webcam using Python and OpenCV

17,306

There is a method available to change properties of VideoCapture object in OpenCV which can be used to set exposure of input image.

cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_EXPOSURE, 40) 

However this parameter is not supported by all cameras. Each camera type offers a different interface to set its parameters. There are many branches in OpenCV code to support as many of them, but of course not all the possibilities are covered.

Same is the case with my camera. So I had to find a different solution. That is using v4l2_ctl utility from command line terminal.

v4l2-ctl -d /dev/video0 -c exposure_absolute=40

But this retains its value for current video session only. That means you have to start video preview first and then set this property As soon as VideoCapture is released, the exposure value is restored to default.

I wanted to control exposure within my python script, so I used subprocess module to run linux bash command. e.g.

import subprocess
subprocess.check_call("v4l2-ctl -d /dev/video0 -c exposure_absolute=40",shell=True)
Share:
17,306

Related videos on Youtube

Muhammad Abdullah
Author by

Muhammad Abdullah

I developed my interest in computer technologies when started developing a website to write my feelings for my one sided crush during school days. Later on I used to make her a flash animated card on every occasion including her birthdays, festivals, new years etc. Alas any of this could never reach her but now that she is with me, I give her the credit for my career development. I have always wished to built a lab like Dexter's Laboratory where I can play with embedded computers and electronic devices and develop some home automation, intelligent assistance and IoT solutions. While same girl who caused me to dive me in this pool, is getting jealous of my divided attention to my wife-to-be and my interest in technology. :D

Updated on October 26, 2022

Comments

  • Muhammad Abdullah
    Muhammad Abdullah over 1 year

    In my robotic vision project, I need to detect a marker of a moving object but motion causes blurring effect in the image. Deconvolution methods are quite slow. So I was thinking to use a higher fps camera. Someone said I don't need higher fps, instead I need shorter exposure time.

    OpenCV's Python Interface cv2 provides a method to change the settings of camera but it does not include "Exposure Time" or "Shutter Speed" settings. I'm also afraid that webcams don't even support this kind of settings.

    Any other thoughts about:

    Eliminating blurring effect using camera setting?

    OR

    Restoration of Image with real-time performance?

    OR

    Any suggestion about a low cost camera for real-time robotic applications?

    • Dan Mašek
      Dan Mašek over 6 years
      What about CAP_PROP_EXPOSURE?
    • Muhammad Abdullah
      Muhammad Abdullah over 6 years
      It changes exposure not exposure time/shutter speed. The resultant image becomes brighter or darker. Does not affect the blurring.
    • 101
      101
      You could try cap = cv2.VideoCapture(0); print cap.get(cv2.cv.CV_CAP_PROP_EXPOSURE).
  • Hendy
    Hendy over 6 years
    Wow. I don't know how many incantations of opencv python exposure webcam or similar I searched for, but I just found this maybe 3hrs after doing this myself! For reference here's a gist showing a way to do a bunch of these. I'm new-ish to python, but I think the dict method is reasonable for cycling through lots of settings?
  • Muhammad Abdullah
    Muhammad Abdullah over 6 years
    thanks for sharing @Hendy ... It is a nice way to set multiple properties of video camera.
  • Mohammed Noureldin
    Mohammed Noureldin about 5 years
    What is exposure_absolutehere? is this a time? or what is this 40 here exactly?