ZuLuuuuuu
Posts: 26
Joined: Thu May 23, 2013 6:30 am

Getting consistent images (exposure-wise)

Mon Jul 06, 2015 7:46 am

Hello,

I am trying to get -very- consistent captures using Raspberry camera. Consistent in exposure/lightning.

I wrote a test script to observe the consistency of consecutive captures. This script:

1) Opens the camera
2) Fixes the framerate, resolution, shutter_speed, iso and awb_gains.
3) Captures a frame
4) Closes the camera
5) Goes to step 1 again.

Source code:

Code: Select all

import picamera
import time

use_video_port = True
white_balance_gains = None

i = 0
while True:
    with picamera.PiCamera() as camera:
        camera.framerate = 1
        camera.resolution = (2592, 1944)
        camera.shutter_speed = 8333 * 12
        camera.iso = 100
        if white_balance_gains is None:
            time.sleep(3)
            print("Fixing white balance gains.")
            white_balance_gains = camera.awb_gains

        camera.awb_mode = 'off'
        camera.awb_gains = white_balance_gains
        time.sleep(3)
        camera.exposure_mode = 'off'

        print("Capturing: " + str(i))
        print("--------------")
        print("White balance: " + str(camera.awb_gains))
        print("White balance mode: " + str(camera.awb_mode))
        print("ISO: " + str(camera.iso))
        print("Shutter speed: " + str(camera.shutter_speed))
        print("Brightness: " + str(camera.brightness))
        print("Digital gain: " + str(camera.digital_gain))
        print("Analog gain: " + str(camera.analog_gain))
        print("Exposure compensation: " + str(camera.exposure_compensation))
        print("\n")

        #print(camera._get_camera_settings())
        
        camera.capture("consistent_captures/" + str(i) + ".jpg", use_video_port=use_video_port)
        i += 1
        time.sleep(2)
And when I look at the results. Although they are pretty consistent there are still subtle exposure differences between them.

But I wrote a second script which only opens the camera "once", fixes the same parameters and then captures all the frames before closing the camera. And then I get very consistent images.

Source code:

Code: Select all

import picamera
import time

use_video_port = True
white_balance_gains = None

with picamera.PiCamera() as camera:
    camera.framerate = 1
    camera.resolution = (2592, 1944)
    camera.shutter_speed = 8333 * 12
    camera.iso = 100
    if white_balance_gains is None:
        time.sleep(3)
        print("Fixing white balance gains.")
        white_balance_gains = camera.awb_gains

    camera.awb_mode = 'off'
    camera.awb_gains = white_balance_gains
    time.sleep(3)
    camera.exposure_mode = 'off'

    i = 0
    while True:
        print("Capturing: " + str(i))
        print("--------------")
        print("White balance: " + str(camera.awb_gains))
        print("White balance mode: " + str(camera.awb_mode))
        print("ISO: " + str(camera.iso))
        print("Shutter speed: " + str(camera.shutter_speed))
        print("Brightness: " + str(camera.brightness))
        print("Digital gain: " + str(camera.digital_gain))
        print("Analog gain: " + str(camera.analog_gain))
        print("Exposure compensation: " + str(camera.exposure_compensation))
        print("\n")

        #print(camera._get_camera_settings())
        
        camera.capture("consistent_captures/" + str(i) + ".jpg", use_video_port=use_video_port)
        i += 1
        time.sleep(2)
My questions are:

1) I guess the difference of these scripts is the digital_gain and analog_gain parameters which are reset between the captures on script 1. Whereas analog_gain and digital_gain parameters do not change on script 2 between captures. Is this true?

2) We observed that there is a position change due to camera overheat if I leave the camera open for a long time. That is why I need to close the camera and open it again between captures. Is there a way to get the exact same analog and digital gain parameters on every capture using script 1?

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 7575
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Getting consistent images (exposure-wise)

Mon Jul 06, 2015 9:11 am

use_video_port allows the AGC algo to vary slightly from the specified ISO. I would recommend you don't use it. If you don't use_video_port, you also get all the EXIF info, which includes exactly the analogue and digital gains (and AWB gains) applied for the capture (it's slightly hidden in the Makernote - search for further info).

We've also just revisited the EXPOSURE_MODE_OFF setting under viewtopic.php?f=43&t=96821 and it is confirmed to work, although fixing both analogue and digital gain at x1.0. I haven't looked to see if that is plumbed through Picamera or not (I would expect it was).

Lastly you should be able to get the settings values back from the camera in realtime. With raspistill/vid it is the -set option. Again I don't know about Picamera.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

namorK
Posts: 15
Joined: Wed Dec 02, 2015 9:34 am

Re: Getting consistent images (exposure-wise)

Sat Mar 12, 2016 4:22 pm

dear everybody, dear 6by9

we recently had our second solar balloon flight to the stratosphere. unfortunately the camera did't work for the second time (the first time a slow sd card might have been the reason). we used the following code and got all black photos and videos during the long flight. and sometimes also black files during the filling up/pre flight preparations. is there a possibility that the camera switches to a different mode when exposed to bright sunlight and snow?

https://drive.google.com/file/d/0B0iLw9bRs9SGVEV0bVdVQ0dQWVE/view?usp=sharing

any suggestions or similar experience? any help highly appreciated!

greetings from grey zurich, roman and christina



----------------------------------------------------------------------------------------------------------
import picamera
import time
from datetime import datetime

count = 0
while count < 999:
with picamera.PiCamera() as camera:
camera.resolution = (2592, 1944)
time.sleep(1)
camera.start_preview()
filename = '//DATA/blue_%s_auto.jpg'%datetime.now().strftime('%Y-%m-%d-%H%M%S')
camera.capture(filename)

camera.resolution = (2592, 1944)
camera.shutter_speed = 1000
camera.exposure_mode = 'off'
camera.iso = 100
camera.awb_mode = 'sunlight'
filename = '//DATA/blue_%s_fixed.jpg'%datetime.now().strftime('%Y-%m-%d-%H%M%S')
camera.capture(filename)

camera.resolution = (1920, 1080)
camera.framerate = 25
camera.sharpness = 0
camera.contrast = -10
camera.saturation = 0
camera.ISO = 100
camera.video_stabilization = False
camera.exposure_compensation = 0
camera.exposure_mode = 'off'
time.sleep(1)
camera.start_preview()
camera.awb_mode = 'off'
camera.awb_gains = (1.45, 1.45)
camera.image_effects = 'none'
camera.color_effects = None
camera.rotation = 180
camera.hflip = False
camera.vflip = False
camera.shutter_speed = 1000
filename = '//DATA/blue_%s.h264'%datetime.now().strftime('%Y-%m-%d-%H%M%S')
camera.start_recording(filename, bitrate=25000000)
camera.wait_recording(627)
camera.stop_recording()
count = count + 1
Attachments
still_s.jpg
still_s.jpg (23.71 KiB) Viewed 1557 times

Return to “Camera board”