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

2

17

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?

Glycerinate answered 16/8, 2017 at 12:16 Comment(10)
Which camera is it?Threemaster
Logitech C310 USB WebCamGlycerinate
You could try cap = cv2.VideoCapture(0); print cap.get(cv2.cv.CV_CAP_PROP_EXPOSURE).Threemaster
What about CAP_PROP_EXPOSURE?Insalubrious
It changes exposure not exposure time/shutter speed. The resultant image becomes brighter or darker. Does not affect the blurring.Glycerinate
The first sentence contradicts itself. Of course the brightness changes, if you expose for a shorter time, less light hits the sensor, so the image becomes darker. To compensate for that, you need better lighting (think of flash photography) ... or perhaps a more sensitive sensor. The device you are using probably doesn't allow very extensive adjustment. | As for which camera... that really needs much more detailed list of requirements (and quantitative, rather than qualitative -- what does "low cost" really mean?)Insalubrious
Camera cost should be less than $100. High FPS to capture a moving human. I don't know how fast a human can run,jump or fall so can't quanitfy that. Comes with a driver that allows changing the shutter,aperture and exposure settings.Glycerinate
As for the brightness, I don't know what's the problem. Once I changed the exposure to -10 and video became totally dark but now I'm trying to run the same program again but nothing changes. cap.set(cv2.CAP_PROP_EXPOSURE, -10) print cap.get(cv2.CAP_PROP_EXPOSURE) still prints infGlycerinate
@DanMašek I'm able to change exposure using v4l_ctl command line tool in linux but I'm unable to do so in Python using OpenCV. The problem with command line tool is that it works when VideoCapture is open, as soon as VideoCapture is released or Program terminates it changes back to old settings. So I need to change exposure withoin my python code.Glycerinate
You may have hit something there. Unfortunately I'm not on linux to help much with that. | To get back to the camera question, here's something to think about: You mention robotic application. What other requirements does that entail? Resolution, some FPS requirements, color/mono, size/weight requirements, interface (USB or ethernet?), power, etc. How much can you budget for lighting (that's very important).Insalubrious
G
17

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)
Glycerinate answered 18/8, 2017 at 12:15 Comment(3)
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?Pricilla
thanks for sharing @Pricilla ... It is a nice way to set multiple properties of video camera.Glycerinate
What is exposure_absolutehere? is this a time? or what is this 40 here exactly?Cuneal
O
1

For instance

I was trying with a c920 for a while whithout success, but sometimes worked, other not using this:

cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_EXPOSURE, -4) 
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240) # after not change the exposure

But finally I realized that I was setting the width and height just after, and so I change the order and now is working fine!! Don´t blow your mind trying to disable the CAP_PROP_AUTO_EXPOSURE flag!! Its not necessary(at least with c920 on windows)!!

cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240) # before works fine
cap.set(cv2.CAP_PROP_EXPOSURE, -4) 

By the way, the range of exposure in C920 is from -2 to -11!!(on windows 10)

Thanks a lot

Opprobrium answered 21/6, 2022 at 16:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.