Clarify white balance code.

This commit is contained in:
Julian Stirling 2025-06-23 17:47:18 +01:00
parent cdae90013a
commit febb0d2690

View file

@ -255,6 +255,8 @@ def adjust_white_balance_from_raw(
camera.configure(config) camera.configure(config)
camera.start() camera.start()
channels = channels_from_bayer_array(camera.capture_array("raw")) channels = channels_from_bayer_array(camera.capture_array("raw"))
# TODO: read black level from camera rather than hard-coding 64
blacklevel = 64
if luminance is not None and Cr is not None and Cb is not None: if luminance is not None and Cr is not None and Cb is not None:
# Reconstruct a low-resolution image from the lens shading tables # Reconstruct a low-resolution image from the lens shading tables
# and use it to normalise the raw image, to compensate for # and use it to normalise the raw image, to compensate for
@ -269,17 +271,23 @@ def adjust_white_balance_from_raw(
channels = channels * channel_gains channels = channels * channel_gains
logging.info(f"After gains, channel maxima are {np.max(channels, axis=(1, 2))}") logging.info(f"After gains, channel maxima are {np.max(channels, axis=(1, 2))}")
if method == "centre": if method == "centre":
_, h, w = channels.shape _, height, width = channels.shape
blue, g1, g2, red = ( # Cut out the central 10% from 9/20 to 11/20...
np.mean( low_y_range = 9 * height // 20
channels[:, 9 * h // 20 : 11 * h // 20, 9 * w // 20 : 11 * w // 20], hi_y_range = 11 * height // 20
axis=(1, 2), low_x_range = 9 * width // 20
) hi_x_range = 11 * width // 20
- 64 # ... and then take the mean of each bayer channel.
centre_means = np.mean(
channels[:, low_y_range:hi_y_range, low_x_range:hi_x_range],
axis=(1, 2),
) )
# Subtract blacklevel before splitting into channels
blue, g1, g2, red = centre_means - blacklevel
else: else:
# TODO: read black level from camera rather than hard-coding 64 blue, g1, g2, red = (
blue, g1, g2, red = np.percentile(channels, percentile, axis=(1, 2)) - 64 np.percentile(channels, percentile, axis=(1, 2)) - blacklevel
)
green = (g1 + g2) / 2.0 green = (g1 + g2) / 2.0
new_awb_gains = (green / red, green / blue) new_awb_gains = (green / red, green / blue)
if Cr is not None and Cb is not None: if Cr is not None and Cb is not None: