1# Copyright 2013 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14"""Verifies linear behavior in exposure/gain space.""" 15 16 17import logging 18import math 19import os.path 20import matplotlib 21from matplotlib import pylab 22from mobly import test_runner 23import numpy as np 24 25import its_base_test 26import camera_properties_utils 27import capture_request_utils 28import image_processing_utils 29import its_session_utils 30import target_exposure_utils 31 32_NAME = os.path.splitext(os.path.basename(__file__))[0] 33_NUM_STEPS = 6 34_PATCH_H = 0.1 # center 10% patch params 35_PATCH_W = 0.1 36_PATCH_X = 0.5 - _PATCH_W/2 37_PATCH_Y = 0.5 - _PATCH_H/2 38_RESIDUAL_THRESH = 0.0003 # sample error of ~2/255 in np.arange(0, 0.5, 0.1) 39_VGA_W, _VGA_H = 640, 480 40 41# HAL3.2 spec requires curves up to 64 control points in length be supported 42_L = 63 43_GAMMA_LUT = np.array( 44 sum([[i/_L, math.pow(i/_L, 1/2.2)] for i in range(_L+1)], [])) 45_INV_GAMMA_LUT = np.array( 46 sum([[i/_L, math.pow(i/_L, 2.2)] for i in range(_L+1)], [])) 47 48 49class LinearityTest(its_base_test.ItsBaseTest): 50 """Test that device processing can be inverted to linear pixels. 51 52 Captures a sequence of shots with the device pointed at a uniform 53 target. Attempts to invert all the ISP processing to get back to 54 linear R,G,B pixel data. 55 """ 56 57 def test_linearity(self): 58 logging.debug('Starting %s', _NAME) 59 with its_session_utils.ItsSession( 60 device_id=self.dut.serial, 61 camera_id=self.camera_id, 62 hidden_physical_id=self.hidden_physical_id) as cam: 63 props = cam.get_camera_properties() 64 props = cam.override_with_hidden_physical_camera_props(props) 65 camera_properties_utils.skip_unless( 66 camera_properties_utils.compute_target_exposure(props)) 67 sync_latency = camera_properties_utils.sync_latency(props) 68 name_with_log_path = os.path.join(self.log_path, _NAME) 69 70 # Load chart for scene 71 its_session_utils.load_scene( 72 cam, props, self.scene, self.tablet, self.chart_distance) 73 74 # Determine sensitivities to test over 75 e_mid, s_mid = target_exposure_utils.get_target_exposure_combos( 76 self.log_path, cam)['midSensitivity'] 77 sens_range = props['android.sensor.info.sensitivityRange'] 78 sensitivities = [s_mid*x/_NUM_STEPS for x in range(1, _NUM_STEPS)] 79 sensitivities = [s for s in sensitivities 80 if s > sens_range[0] and s < sens_range[1]] 81 82 # Initialize capture request 83 req = capture_request_utils.manual_capture_request(0, e_mid) 84 req['android.blackLevel.lock'] = True 85 req['android.tonemap.mode'] = 0 86 req['android.tonemap.curve'] = {'red': _GAMMA_LUT.tolist(), 87 'green': _GAMMA_LUT.tolist(), 88 'blue': _GAMMA_LUT.tolist()} 89 # Do captures and calculate center patch RGB means 90 r_means = [] 91 g_means = [] 92 b_means = [] 93 fmt = {'format': 'yuv', 'width': _VGA_W, 'height': _VGA_H} 94 for sens in sensitivities: 95 req['android.sensor.sensitivity'] = sens 96 cap = its_session_utils.do_capture_with_latency( 97 cam, req, sync_latency, fmt) 98 img = image_processing_utils.convert_capture_to_rgb_image(cap) 99 image_processing_utils.write_image( 100 img, f'{name_with_log_path}_sens={int(sens):04d}.jpg') 101 img = image_processing_utils.apply_lut_to_image( 102 img, _INV_GAMMA_LUT[1::2] * _L) 103 patch = image_processing_utils.get_image_patch( 104 img, _PATCH_X, _PATCH_Y, _PATCH_W, _PATCH_H) 105 rgb_means = image_processing_utils.compute_image_means(patch) 106 r_means.append(rgb_means[0]) 107 g_means.append(rgb_means[1]) 108 b_means.append(rgb_means[2]) 109 110 # Plot means 111 pylab.figure(_NAME) 112 pylab.plot(sensitivities, r_means, '-ro') 113 pylab.plot(sensitivities, g_means, '-go') 114 pylab.plot(sensitivities, b_means, '-bo') 115 pylab.title(_NAME) 116 pylab.xlim([sens_range[0], sens_range[1]/2]) 117 pylab.ylim([0, 1]) 118 pylab.xlabel('sensitivity(ISO)') 119 pylab.ylabel('RGB avg [0, 1]') 120 matplotlib.pyplot.savefig(f'{name_with_log_path}_plot_means.png') 121 channel_color = '' 122 # Assert plot curves are linear w/ + slope by examining polyfit residual 123 for means in [r_means, g_means, b_means]: 124 if means == r_means: 125 channel_color = 'Red' 126 elif means == g_means: 127 channel_color = 'Green' 128 else: 129 channel_color = 'Blue' 130 line, residuals, _, _, _ = np.polyfit( 131 range(len(sensitivities)), means, 1, full=True) 132 logging.debug('Line: m=%f, b=%f, resid=%f', 133 line[0], line[1], residuals[0]) 134 if residuals[0] > _RESIDUAL_THRESH: 135 raise AssertionError( 136 f'residual: {residuals[0]:.5f}, THRESH: {_RESIDUAL_THRESH},' 137 f' color: {channel_color}') 138 if line[0] <= 0: 139 raise AssertionError(f'slope {line[0]:.6f} <= 0!') 140 141if __name__ == '__main__': 142 test_runner.main() 143