Functions to set up a Raspberry Pi Camera (v2 and HQ) for scientific use.
This module provides slower, simpler functions to set the gain, exposure, and white balance of a Raspberry Pi camera, using the picamera2 Python library. It's mostly used by the OpenFlexure Microscope, though it deliberately has no hard dependencies on said software, so that it's useful on its own.
There are three main calibration steps:
- Setting exposure time and gain to get a reasonably bright image.
- Fixing the white balance to get a neutral image
- Taking a uniform white image and using it to calibrate the Lens Shading Table
The most reliable way to do this, avoiding any issues relating to "memory" or nonlinearities in the camera's image processing pipeline, is to use raw images. This is quite slow, but very reliable. The three steps above can be accomplished by:
picamera = picamera2.Picamera2()
sensor_info = IMX219_SENSOR_INFO
adjust_shutter_and_gain_from_raw(
picamera,
sensor_info,
target_white_level=sensor_info.default_target_white_level,
)
adjust_white_balance_from_raw(picamera, sensor_info)
lst = lst_from_camera(picamera, sensor_info)
picamera.lens_shading_table = lst| Class | |
Information about the sensor used for calibration and property setting. |
| Function | adjust |
Adjust exposure and analog gain based on raw images. |
| Function | lst |
Acquire a raw image and use it to calculate a lens shading table. |
| Function | recreate |
Delete and recreate the camera manager. |
| Constant | IMX219 |
Undocumented |
| Constant | IMX477 |
Undocumented |
| Constant | LOGGER |
Undocumented |
| Type Alias | |
Undocumented |
| Class | _ |
Record the results of testing the camera's current exposure settings. |
| Function | _channels |
Given the 'array' from a PiBayerArray, return the 4 channels. |
| Function | _check |
Check whether the brightness is within the specified target range. |
| Function | _downsampled |
Generate a downsampled, un-normalised image from which to calculate the LST. |
| Function | _get |
Compresses channel down to a 16x12 grid - from libcamera. |
| Function | _grids |
Convert form luminance/chrominance dict to four RGGB channels. |
| Function | _lst |
Given the 4 Bayer colour channels from a white image, generate a LST. |
| Function | _lst |
Given 4 downsampled grids, generate the luminance and chrominance tables. |
| Function | _raw |
Acquire a raw image and return a 4xNxM array of the colour channels. |
| Function | _set |
Enable manual exposure, with low gain and shutter speed. |
| Function | _test |
Evaluate current exposure settings using a raw image. |
| Function | _upsample |
Zoom an image in the last two dimensions. |
Picamera2, sensor_info: SensorInfo, target_white_level: int, max_iterations: int = 20, tolerance: float = 0.05, percentile: float = 99.9) -> float:
(source)
¶
Adjust exposure and analog gain based on raw images.
This routine is slow but effective. It uses raw images, so we are not affected by white balance or digital gain.
| Parameters | |
camera:Picamera2 | A Picamera2 object. |
sensorSensorInfo | Undocumented |
targetint | The raw value we aim for, the raw value of the brightest pixels should be approximately this bright. The value to set depends on the sensor bit depth. We recommend values of 700 for 10-bit sensors and 2800 for 12-bit sensors. This is about 70% of saturated once the blacklevel is subtracted. The maximum possible value depends on the sensor bit depth, the sensor blacklevel and the tolerance argument. |
maxint | We will terminate once we perform this many iterations, whether or not we converge. More than 10 shouldn't happen. |
tolerance:float | How close to the target value we consider "done". Expressed as a fraction of the target_white_level so 0.05 means +/- 5% |
percentile:float | Rather then use the maximum value for each channel, we calculate a percentile. This makes us robust to single pixels that are bright/noisy. 99.9% still picks the top of the brightness range, but seems much more reliable than just np.max(). |
| Returns | |
float | Undocumented |
Undocumented
| Value |
|
Undocumented
| Value |
|
Compresses channel down to a 16x12 grid - from libcamera.
This is taken from https://git.linuxtv.org/libcamera.git/tree/utils/raspberrypi/ctt/ctt_alsc.py for consistency.
Convert form luminance/chrominance dict to four RGGB channels.
Note that these will be normalised - the maximum green value is always 1. Also, note that the channels are BGGR, to be consistent with the channels_from_raw_image function. This should probably change in the future.
Given the 4 Bayer colour channels from a white image, generate a LST.
Internally, is just calls _downsampled_channels and _lst_from_grids.
Given 4 downsampled grids, generate the luminance and chrominance tables.
The grids are the 4 BAYER channels RGGB
The LST format has changed with picamera2 and now uses a fixed resolution, and is in luminance, Cr, Cb format. This function returns three ndarrays of luminance, Cr, Cb, each with shape (12, 16).
Picamera2, sensor_info: SensorInfo) -> LensShadingTables:
(source)
¶
Acquire a raw image and return a 4xNxM array of the colour channels.
Enable manual exposure, with low gain and shutter speed.
Set exposure mode to manual, analog and digital gain to 1, and shutter speed to the minimum (8us for Pi Camera v2)
Note ISO is left at auto, because this is needed for the gains to be set correctly.
Evaluate current exposure settings using a raw image.
CAMERA SHOULD BE STARTED!
We will acquire a raw image and calculate the given percentile of the pixel values. We return a dictionary containing the percentile (which will be compared to the target), as well as the camera's shutter and gain values.