mevada.ravikumar
mevada.ravikumar

Reputation: 109

Pi Camera exposure control using OpenCV

I am using a Raspberry Pi V2.1 camera. I wanted to control the camera’s exposure time, shutter speed, etc using OpenCV. I am following the OpenCV flags for video I/O documentation. The link is here:

https://docs.opencv.org/3.4/d4/d15/group__videoio__flags__base.html

For ex:

I have tried using

cv2.CAP_PROP_AUTO_EXPOSURE = 0.25 and 0.75

It seems like auto exposure is turning on and off. But when I try to set the value manually using

cv2.CAP_PROP_EXPOSURE = -1 to -13 (according to some online blogs)

the camera is not responding.

The same goes for other flags. Most of them do not seem to be responding at all. I have read the online documentation and get to know that flags are camera dependent. The OpenCV documentation, in this case, is not helpful at all.

So my question is How can I find out which flags are useful for the Pi camera and What are the valid values of these flags?

Thank you in advance.

Upvotes: 2

Views: 4950

Answers (1)

Simon
Simon

Reputation: 61

I'm no expert on that topic but I managed to manually set the exposure for an RPi 4 with a camera v2.1. I set the CAP_PROP_AUTO_EXPOSURE to 0.75 and CAP_PROP_EXPOSURE to 0. That left me with a black frame (as expected I guess). Increasing the exposure value gives gradually brighter images. For values above something like 80 it stopped getting any brighter.

This code gradually increases the exposure after each displayed frame and works for me:

import cv2

# Open Pi Camera
cap = cv2.VideoCapture(0)
# Set auto exposure to false
cap.set(cv2.CAP_PROP_AUTO_EXPOSURE, 0.75)

exposure = 0
while cap.isOpened():
    # Grab frame
    ret, frame = cap.read()
    # Display if there is a frame
    if ret:
        cv2.imshow('Frame', frame)
        if cv2.waitKey(25) & 0xFF == ord('q'):
            break
    # Set exposure manually
    cap.set(cv2.CAP_PROP_EXPOSURE, exposure)
    # Increase exposure for every frame that is displayed
    exposure += 0.5

# Close everything
cap.release()
cv2.destroyAllWindows()

Cheers,

Simon

Upvotes: 6

Related Questions