• Raspberry Pi 4 Model B
  • Ubuntu 20.04 64-bit
  • Raspberry Pi Camera V2.1

I'm using OpenCV's VideoCapture to capture images using the Raspberry Pi camera module, as the picamera module isn't supported on 64-bit systems at the time of writing. While both raspistill and raspivid produce decent looking images and videos, I can't seem to get a proper image using OpenCV. Occasionally it'll work, and provide a decent image, but it's about a 1 in 100 chance.

Below is a simplified version of my code and a sample image. Any tips on getting rid of the blue hue on the image would be greatly appreciated!

import cv2

cap = cv2.VideoCapture(0)
if not cap.isOpened():
    print('could not open video device')

cap.set(cv2.CAP_PROP_FRAME_WIDTH, 480)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 320)
cap.set(cv2.CAP_PROP_FPS, 30)

ret, frame = cap.read()

if ret == True:
    cv2.imwrite('/home/ubuntu/img/img-001.jpg', frame)

Sample image


Here's the output from running v4l2-ctl -l:

User Controls

                     brightness 0x00980900 (int)    : min=0 max=100 step=1 default=50 value=50 flags=slider
                       contrast 0x00980901 (int)    : min=-100 max=100 step=1 default=0 value=0 flags=slider
                     saturation 0x00980902 (int)    : min=-100 max=100 step=1 default=0 value=0 flags=slider
                    red_balance 0x0098090e (int)    : min=1 max=7999 step=1 default=1000 value=1000 flags=slider
                   blue_balance 0x0098090f (int)    : min=1 max=7999 step=1 default=1000 value=1000 flags=slider
                horizontal_flip 0x00980914 (bool)   : default=0 value=0
                  vertical_flip 0x00980915 (bool)   : default=0 value=0
           power_line_frequency 0x00980918 (menu)   : min=0 max=3 default=1 value=1
                      sharpness 0x0098091b (int)    : min=-100 max=100 step=1 default=0 value=0 flags=slider
                  color_effects 0x0098091f (menu)   : min=0 max=15 default=0 value=0
                         rotate 0x00980922 (int)    : min=0 max=360 step=90 default=0 value=0 flags=modify-layout
             color_effects_cbcr 0x0098092a (int)    : min=0 max=65535 step=1 default=32896 value=32896

Codec Controls

             video_bitrate_mode 0x009909ce (menu)   : min=0 max=1 default=0 value=0 flags=update
                  video_bitrate 0x009909cf (int)    : min=25000 max=25000000 step=25000 default=10000000 value=10000000
         repeat_sequence_header 0x009909e2 (bool)   : default=0 value=0
            h264_i_frame_period 0x00990a66 (int)    : min=0 max=2147483647 step=1 default=60 value=60
                     h264_level 0x00990a67 (menu)   : min=0 max=11 default=11 value=11
                   h264_profile 0x00990a6b (menu)   : min=0 max=4 default=4 value=4

Camera Controls

                  auto_exposure 0x009a0901 (menu)   : min=0 max=3 default=0 value=0
         exposure_time_absolute 0x009a0902 (int)    : min=1 max=10000 step=1 default=1000 value=1000
     exposure_dynamic_framerate 0x009a0903 (bool)   : default=0 value=0
             auto_exposure_bias 0x009a0913 (intmenu): min=0 max=24 default=12 value=12
      white_balance_auto_preset 0x009a0914 (menu)   : min=0 max=10 default=1 value=1
            image_stabilization 0x009a0916 (bool)   : default=0 value=0
                iso_sensitivity 0x009a0917 (intmenu): min=0 max=4 default=0 value=0
           iso_sensitivity_auto 0x009a0918 (menu)   : min=0 max=1 default=1 value=1
         exposure_metering_mode 0x009a0919 (menu)   : min=0 max=2 default=0 value=0
                     scene_mode 0x009a091a (menu)   : min=0 max=13 default=0 value=0

JPEG Compression Controls

            compression_quality 0x009d0903 (int)    : min=1 max=100 step=1 default=30 value=30

The camera seems to properly adjust to the lighting conditions, based on the images below. There's options to set the brightness, contrast, saturation, hue, gain and exposure using OpenCV (see here), but it seems to manage them automatically, so I'd rather have it automatically adjust correctly.

First Image

Second Image


Try running v4l2-ctl -l and search for "white balance" or "color temperature" in the output. Usually, there's an automatic setting (a boolean) which lets the camera manage the white balance on its own, and a manual setting which lets you override that.

Those properties should be mapped to something like CV_CAP_PROP_HUE and CV_CAP_PROP_WHITE_BALANCE in OpenCV's set().

  • I've added the output for v4l2-ctl -l to the question. It seems to automatically adjust the camera settings based on the environment just fine, so I'd rather not manually set those. I've added two more photos to the question, showing that it does adjust the camera when placed outside. – Jasper Mar 2 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.