# SPDX-License-Identifier: BSD-2-Clause
#
# Copyright (C) 2019, Raspberry Pi Ltd
#
# camera tuning tool for lux level
from ctt_tools import *
"""
Find lux values from metadata and calculate Y
"""
def lux(Cam, Img):
shutter_speed = Img.exposure
gain = Img.againQ8_norm
aperture = 1
Cam.log += '\nShutter speed = {}'.format(shutter_speed)
Cam.log += '\nGain = {}'.format(gain)
Cam.log += '\nAperture = {}'.format(aperture)
patches = [Img.patches[i] for i in Img.order]
channels = [Img.channels[i] for i in Img.order]
return lux_calc(Cam, Img, patches, channels), shutter_speed, gain
"""
perform lux calibration on bayer channels
"""
def lux_calc(Cam, Img, patches, channels):
"""
find means color channels on grey patches
"""
ap_r = np.mean(patches[0][3::4])
ap_g = (np.mean(patches[1][3::4])+np.mean(patches[2][3::4]))/2
ap_b = np.mean(patches[3][3::4])
Cam.log += '\nAverage channel values on grey patches:'
Cam.log += '\nRed = {:.0f} Green = {:.0f} Blue = {:.0f}'.format(ap_r, ap_b, ap_g)
# print(ap_r, ap_g, ap_b)
"""
calculate channel gains
"""
gr = ap_g/ap_r
gb = ap_g/ap_b
Cam.log += '\nChannel gains: Red = {:.3f} Blue = {:.3f}'.format(gr, gb)
"""
find means color channels on image and scale by gain
note greens are averaged together (treated as one channel)
"""
a_r = np.mean(channels[0])*gr
a_g = (np.mean(channels[1])+np.mean(channels[2]))/2
a_b = np.mean(channels[3])*gb
Cam.log += '\nAverage channel values over entire image scaled by channel gains:'
Cam.log += '\nRed = {:.0f} Green = {:.0f} Blue = {:.0f}'.format(a_r, a_b, a_g)
# print(a_r, a_g, a_b)
"""
Calculate y with top row of yuv matrix
"""
y = 0.299*a_r + 0.587*a_g + 0.114*a_b
Cam.log += '\nY value calculated: {}'.format(int(y))
# print(y)
return int(y)
'>jmondi/pinephone
jmondi/pinephonepro
jmondi/pinephonepro-af
jmondi/rk3399/google-dru-scarlet
jmondi/rockpi/af/imx519
jmondi/rpi5-on-mainline
multicontext/rpi/v3
pfc/rkisp1-free-run
pfc/rkisp1-free-run-upstreaming
pfc/rkisp1-free-run-v2
rcar-gen4
yaml-emitter
Jacopo Mondi's clone of libcamera git repository hosting on libcamera.org
blob: 1ab986d65e2bc0c8e074bb0c6725f8078bc58d12 (
plain )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# colors.py - Program to convert from RGB to LAB color space
def RGB_to_LAB ( RGB): # where RGB is a 1x3 array. e.g RGB = [100, 255, 230]
num = 0
XYZ = [ 0 , 0 , 0 ]
# converted all the three R, G, B to X, Y, Z
X = RGB[ 0 ] * 0.4124 + RGB[ 1 ] * 0.3576 + RGB[ 2 ] * 0.1805
Y = RGB[ 0 ] * 0.2126 + RGB[ 1 ] * 0.7152 + RGB[ 2 ] * 0.0722
Z = RGB[ 0 ] * 0.0193 + RGB[ 1 ] * 0.1192 + RGB[ 2 ] * 0.9505
XYZ[ 0 ] = X / 255 * 100
XYZ[ 1 ] = Y / 255 * 100 # XYZ Must be in range 0 -> 100, so scale down from 255
XYZ[ 2 ] = Z / 255 * 100
XYZ[ 0 ] = XYZ[ 0 ] / 95.047 # ref_X = 95.047 Observer= 2°, Illuminant= D65
XYZ[ 1 ] = XYZ[ 1 ] / 100.0 # ref_Y = 100.000
XYZ[ 2 ] = XYZ[ 2 ] / 108.883 # ref_Z = 108.883
num = 0
for value in XYZ:
if value > 0.008856 :
value = value ** ( 0.3333333333333333 )
else :