UVC camera exposure timing in OpenCV
USB cameras can have the exposure set to manual (cheaper ones have only full auto mode). It’s a pretty straight forward function in OpenCV, but insufficiently documented thus hard to find exact times and calculations. Also there are two major implementations for Linux and Windows.
- Linux – exposure times are set in units 1/s (all clear here)
- Windows – exposure times are selected from a table where index ranges typically from 0 to -13. Value 0 means the longest exposure and -13 is the shortest time (fastest shutter).
Windows indexed exposure values are a logarithmic function of time. The equation is very simple EXP_TIME = 2^(-EXP_VAL) and exact values listed in the table below.
CAP_PROP_EXPOSURE | Actual exposure time |
0 | 1s |
-1 | 500ms |
-2 | 250ms |
-3 | 125ms |
-4 | 62.5ms |
-5 | 31.3ms |
-6 | 15.6ms |
-7 | 7.8ms |
-8 | 3.9ms |
-9 | 2ms |
-10 | 976.6µs |
-11 | 488.3µs |
-12 | 244.1µs |
-13 | 122.1µs |
This results in a straight graph on the logarithmic scale.
And simple python code example is listed below. It Opens the camera, prepares fro MJPEG stream, and displays the real-time view on the screen.
import cv2
print("Initializing camera")
camera = cv2.VideoCapture(0)
print("Setting camera mode")
exp_val = -6
codec = 0x47504A4D # MJPG
camera.set(cv2.CAP_PROP_FPS, 30.0)
camera.set(cv2.CAP_PROP_FOURCC, codec)
camera.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
camera.set(cv2.CAP_PROP_EXPOSURE, exp_val)
print("Starting capture")
while(1):
camera.grab()
retval, im = camera.retrieve(0)
cv2.imshow("image", im)
k = cv2.waitKey(1) & 0xff
if k == 27:
print("exit")
break
camera.release()
cv2.destroyAllWindows()
Comments ( 4 )