1# Copyright 2014 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 sensitivities on RAW images.""" 15 16 17import logging 18import os.path 19import matplotlib 20from matplotlib import pylab 21from mobly import test_runner 22 23import its_base_test 24import camera_properties_utils 25import capture_request_utils 26import image_processing_utils 27import its_session_utils 28import opencv_processing_utils 29 30GR_PLANE_IDX = 1 # GR plane index in RGGB data 31IMG_STATS_GRID = 9 # Center 11.11% 32NAME = os.path.splitext(os.path.basename(__file__))[0] 33NUM_SENS_STEPS = 5 34VAR_THRESH = 1.01 # Each shot must be 1% noisier than previous 35 36 37def define_raw_stats_fmt(props): 38 """Define format with active array width and height.""" 39 aaw = (props['android.sensor.info.preCorrectionActiveArraySize']['right'] - 40 props['android.sensor.info.preCorrectionActiveArraySize']['left']) 41 aah = (props['android.sensor.info.preCorrectionActiveArraySize']['bottom'] - 42 props['android.sensor.info.preCorrectionActiveArraySize']['top']) 43 logging.debug('Active array W,H: %d,%d', aaw, aah) 44 return {'format': 'rawStats', 45 'gridWidth': aaw // IMG_STATS_GRID, 46 'gridHeight': aah // IMG_STATS_GRID} 47 48 49class RawSensitivityTest(its_base_test.ItsBaseTest): 50 """Capture a set of raw images with increasing gains and measure the noise.""" 51 52 def test_raw_sensitivity(self): 53 logging.debug('Starting %s', NAME) 54 with its_session_utils.ItsSession( 55 device_id=self.dut.serial, 56 camera_id=self.camera_id, 57 hidden_physical_id=self.hidden_physical_id) as cam: 58 props = cam.get_camera_properties() 59 props = cam.override_with_hidden_physical_camera_props(props) 60 camera_properties_utils.skip_unless( 61 camera_properties_utils.raw16(props) and 62 camera_properties_utils.manual_sensor(props) and 63 camera_properties_utils.read_3a(props) and 64 camera_properties_utils.per_frame_control(props) and 65 not camera_properties_utils.mono_camera(props)) 66 name_with_log_path = os.path.join(self.log_path, NAME) 67 camera_fov = float(cam.calc_camera_fov(props)) 68 69 # Load chart for scene 70 its_session_utils.load_scene( 71 cam, props, self.scene, self.tablet, self.chart_distance) 72 73 # Expose for the scene with min sensitivity 74 sens_min, _ = props['android.sensor.info.sensitivityRange'] 75 # Digital gains might not be visible on RAW data 76 sens_max = props['android.sensor.maxAnalogSensitivity'] 77 sens_step = (sens_max - sens_min) // NUM_SENS_STEPS 78 79 # Skip AF if TELE camera 80 if camera_fov <= opencv_processing_utils.FOV_THRESH_TELE: 81 s_ae, e_ae, _, _, _ = cam.do_3a(do_af=False, get_results=True) 82 f_dist = 0 83 else: 84 s_ae, e_ae, _, _, f_dist = cam.do_3a(get_results=True) 85 s_e_prod = s_ae * e_ae 86 87 sensitivities = list(range(sens_min, sens_max, sens_step)) 88 variances = [] 89 for s in sensitivities: 90 e = int(s_e_prod / float(s)) 91 req = capture_request_utils.manual_capture_request(s, e, f_dist) 92 93 # Capture in rawStats to reduce test run time 94 fmt = define_raw_stats_fmt(props) 95 cap = cam.do_capture(req, fmt) 96 97 if self.debug_mode: 98 img = image_processing_utils.convert_capture_to_rgb_image( 99 cap, props=props) 100 image_processing_utils.write_image( 101 img, f'{name_with_log_path}_{s}_{e}ns.jpg', True) 102 103 # Measure variance 104 _, var_image = image_processing_utils.unpack_rawstats_capture(cap) 105 cfa_idxs = image_processing_utils.get_canonical_cfa_order(props) 106 white_level = float(props['android.sensor.info.whiteLevel']) 107 var = var_image[IMG_STATS_GRID//2, IMG_STATS_GRID//2, 108 cfa_idxs[GR_PLANE_IDX]]/white_level**2 109 logging.debug('s=%d, e=%d, var=%e', s, e, var) 110 variances.append(var) 111 112 # Create plot 113 pylab.figure(NAME) 114 pylab.plot(sensitivities, variances, '-ro') 115 pylab.xticks(sensitivities) 116 pylab.xlabel('Sensitivities') 117 pylab.ylabel('Image Center Patch Variance') 118 pylab.ticklabel_format(axis='y', style='sci', scilimits=(-6, -6)) 119 pylab.title(NAME) 120 matplotlib.pyplot.savefig(f'{name_with_log_path}_variances.png') 121 122 # Test that each shot is noisier than previous 123 for i in range(len(variances) - 1): 124 if variances[i] >= variances[i+1]/VAR_THRESH: 125 raise AssertionError(f'variances [i]: {variances[i]:5f}, [i+1]: ' 126 f'{variances[i+1]:.5f}, THRESH: {VAR_THRESH}') 127 128if __name__ == '__main__': 129 test_runner.main() 130