xref: /aosp_15_r20/cts/apps/CameraITS/tests/scene1_2/test_raw_exposure.py (revision b7c941bb3fa97aba169d73cee0bed2de8ac964bf)
1# Copyright 2018 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 exposure times on RAW images."""
15
16
17import logging
18import math
19import os.path
20
21from matplotlib import pyplot as plt
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
30
31_BAYER_COLORS = ('R', 'Gr', 'Gb', 'B')
32_BLK_LVL_RTOL = 0.1
33_BURST_LEN = 10  # break captures into burst of BURST_LEN requests
34_EXP_LONG_THRESH = 1E6  # 1ms
35_EXP_MULT_SHORT = pow(2, 1.0/3)  # Test 3 steps per 2x exposure
36_EXP_MULT_LONG = pow(10, 1.0/3)  # Test 3 steps per 10x exposure
37_IMG_DELTA_THRESH = 0.99  # Each shot must be > 0.99*previous
38_IMG_INCREASING_ATOL = 2  # Require images get at lease 2x black level
39_IMG_SAT_RTOL = 0.01  # 1%
40_IMG_STATS_GRID = 9  # find used to find the center 11.11%
41_NAME = os.path.splitext(os.path.basename(__file__))[0]
42_NS_TO_MS_FACTOR = 1.0E-6
43_NUM_ISO_STEPS = 5
44
45
46def create_test_exposure_list(e_min, e_max):
47  """Create the list of exposure values to test."""
48  e_list = []
49  mult = 1.0
50  while e_min*mult < e_max:
51    e_list.append(int(e_min*mult))
52    if e_min*mult < _EXP_LONG_THRESH:
53      mult *= _EXP_MULT_SHORT
54    else:
55      mult *= _EXP_MULT_LONG
56  if e_list[-1] < e_max*_IMG_DELTA_THRESH:
57    e_list.append(int(e_max))
58  return e_list
59
60
61def define_raw_stats_fmt(props):
62  """Define format with active array width and height."""
63  aax = props['android.sensor.info.preCorrectionActiveArraySize']['left']
64  aay = props['android.sensor.info.preCorrectionActiveArraySize']['top']
65  aaw = props['android.sensor.info.preCorrectionActiveArraySize']['right']-aax
66  aah = props['android.sensor.info.preCorrectionActiveArraySize']['bottom']-aay
67  return {'format': 'rawStats',
68          'gridWidth': aaw // _IMG_STATS_GRID,
69          'gridHeight': aah // _IMG_STATS_GRID}
70
71
72def create_plot(exps, means, sens, log_path):
73  """Create plots R, Gr, Gb, B vs exposures.
74
75  Args:
76    exps: array of exposure times in ms
77    means: array of means for RAW captures
78    sens: int value for ISO gain
79    log_path: path to write plot file
80  Returns:
81    None
82  """
83  # means[0] is black level value
84  r = [m[0] for m in means[1:]]
85  gr = [m[1] for m in means[1:]]
86  gb = [m[2] for m in means[1:]]
87  b = [m[3] for m in means[1:]]
88  plt.figure(f'{_NAME}_{sens}')
89  plt.plot(exps, r, 'r.-', label='R')
90  plt.plot(exps, gr, 'g.-', label='Gr')
91  plt.plot(exps, gb, 'k.-', label='Gb')
92  plt.plot(exps, b, 'b.-', label='B')
93  plt.xscale('log')
94  plt.yscale('log')
95  plt.title(f'{_NAME} ISO={sens}')
96  plt.xlabel('Exposure time (ms)')
97  plt.ylabel('Center patch pixel mean')
98  plt.legend(loc='lower right', numpoints=1, fancybox=True)
99  plt.savefig(f'{os.path.join(log_path, _NAME)}_s={sens}.png')
100  plt.clf()
101
102
103def assert_increasing_means(means, exps, sens, black_levels, white_level):
104  """Assert that each image increases unless over/undersaturated.
105
106  Args:
107    means: BAYER COLORS means for set of images
108    exps: exposure times in ms
109    sens: ISO gain value
110    black_levels: BAYER COLORS black_level values
111    white_level: full scale value
112  Returns:
113    None
114  """
115  lower_thresh = np.array(black_levels) * (1 + _BLK_LVL_RTOL)
116  logging.debug('Lower threshold for check: %s', lower_thresh)
117  allow_under_saturated = True
118  image_increasing = False
119  for i in range(1, len(means)):
120    prev_mean = means[i-1]
121    mean = means[i]
122
123    if max(mean) > min(black_levels) * _IMG_INCREASING_ATOL:
124      image_increasing = True
125
126    if math.isclose(max(mean), white_level, rel_tol=_IMG_SAT_RTOL):
127      logging.debug('Saturated: white_level %f, max_mean %f',
128                    white_level, max(mean))
129      break
130
131    if allow_under_saturated and min(mean-lower_thresh) < 0:
132      # All channel means are close to black level
133      continue
134    allow_under_saturated = False
135
136    # Check pixel means are increasing (with small tolerance)
137    logging.debug('iso: %d, exp: %.3f, means: %s', sens, exps[i-1], mean)
138    for ch, color in enumerate(_BAYER_COLORS):
139      if mean[ch] <= prev_mean[ch] * _IMG_DELTA_THRESH:
140        e_msg = (f'{color} not increasing with increased exp time! '
141                 f'ISO: {sens}, ')
142        if i == 1:
143          e_msg += f'black_level: {black_levels[ch]}, '
144        else:
145          e_msg += (f'exp[i-1]: {exps[i-2]:.3f}ms, '
146                    f'mean[i-1]: {prev_mean[ch]:.2f}, ')
147        e_msg += (f'exp[i]: {exps[i-1]:.3f}ms, mean[i]: {mean[ch]}, '
148                  f'RTOL: {_IMG_DELTA_THRESH}')
149        raise AssertionError(e_msg)
150
151  # Check image increases
152  if not image_increasing:
153    raise AssertionError('Image does not increase with exposure!')
154
155
156class RawExposureTest(its_base_test.ItsBaseTest):
157  """Capture RAW images with increasing exp time and measure pixel values."""
158
159  def test_raw_exposure(self):
160    logging.debug('Starting %s', _NAME)
161    with its_session_utils.ItsSession(
162        device_id=self.dut.serial,
163        camera_id=self.camera_id,
164        hidden_physical_id=self.hidden_physical_id) as cam:
165      props = cam.get_camera_properties()
166      props = cam.override_with_hidden_physical_camera_props(props)
167      camera_properties_utils.skip_unless(
168          camera_properties_utils.raw16(props) and
169          camera_properties_utils.manual_sensor(props) and
170          camera_properties_utils.per_frame_control(props) and
171          not camera_properties_utils.mono_camera(props))
172      log_path = self.log_path
173
174      # Load chart for scene
175      its_session_utils.load_scene(
176          cam, props, self.scene, self.tablet,
177          its_session_utils.CHART_DISTANCE_NO_SCALING)
178
179      # Create list of exposures
180      e_min, e_max = props['android.sensor.info.exposureTimeRange']
181      logging.debug('exposureTimeRange(ns): %d, %d', e_min, e_max)
182      e_test = create_test_exposure_list(e_min, e_max)
183      e_test_ms = [e*_NS_TO_MS_FACTOR for e in e_test]
184
185      # Capture with rawStats to reduce capture times
186      fmt = define_raw_stats_fmt(props)
187
188      # Create sensitivity range from min to max analog sensitivity
189      sens_min, _ = props['android.sensor.info.sensitivityRange']
190      sens_max = props['android.sensor.maxAnalogSensitivity']
191      sens_step = (sens_max - sens_min) // _NUM_ISO_STEPS
192      white_level = float(props['android.sensor.info.whiteLevel'])
193      black_levels = image_processing_utils.get_black_levels(props)
194
195      # Do captures with exposure list over sensitivity range
196      for s in range(sens_min, sens_max, sens_step):
197        # Break caps into bursts and do captures
198        burst_len = _BURST_LEN
199        caps = []
200        reqs = [capture_request_utils.manual_capture_request(
201            s, e, 0) for e in e_test]
202        # Eliminate burst len==1. Error because returns [[]], not [{}, ...]
203        while len(reqs) % burst_len == 1:
204          burst_len -= 1
205        # Break caps into bursts
206        for i in range(len(reqs) // burst_len):
207          caps += cam.do_capture(reqs[i*burst_len:(i+1)*burst_len], fmt)
208        last_n = len(reqs) % burst_len
209        if last_n:
210          caps += cam.do_capture(reqs[-last_n:], fmt)
211
212        # Extract means for each capture
213        means = []
214        means.append(black_levels)
215        for i, cap in enumerate(caps):
216          mean_image, _ = image_processing_utils.unpack_rawstats_capture(cap)
217          mean = mean_image[_IMG_STATS_GRID // 2, _IMG_STATS_GRID // 2]
218          logging.debug('ISO=%d, exp_time=%.3fms, mean=%s',
219                        s, (e_test[i] * _NS_TO_MS_FACTOR), str(mean))
220          means.append(mean)
221
222        # Create plot
223        create_plot(e_test_ms, means, s, log_path)
224
225        # Each shot mean should be brighter (except under/overexposed scene)
226        assert_increasing_means(means, e_test_ms, s, black_levels, white_level)
227
228if __name__ == '__main__':
229  test_runner.main()
230