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

Muhammad Abdullah picture Muhammad Abdullah · Aug 16, 2017 · Viewed 13.1k times · Source

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?

Answer

Muhammad Abdullah picture Muhammad Abdullah · Aug 18, 2017

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)