• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
32NAME = os.path.splitext(os.path.basename(__file__))[0]
33NUM_STEPS = 6
34PATCH_H = 0.1  # center 10% patch params
35PATCH_W = 0.1
36PATCH_X = 0.5 - PATCH_W/2
37PATCH_Y = 0.5 - PATCH_H/2
38RESIDUAL_THRESH = 0.0003  # sample error of ~2/255 in np.arange(0, 0.5, 0.1)
39VGA_W, VGA_H = 640, 480
40
41# HAL3.2 spec requires curves up to 64 control points in length be supported
42L = 63
43GAMMA_LUT = np.array(
44    sum([[i/L, math.pow(i/L, 1/2.2)] for i in range(L+1)], []))
45INV_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
69      # Load chart for scene
70      its_session_utils.load_scene(
71          cam, props, self.scene, self.tablet, self.chart_distance)
72
73      # Determine sensitivities to test over
74      e_mid, s_mid = target_exposure_utils.get_target_exposure_combos(
75          self.log_path, cam)['midSensitivity']
76      sens_range = props['android.sensor.info.sensitivityRange']
77      sensitivities = [s_mid*x/NUM_STEPS for x in range(1, NUM_STEPS)]
78      sensitivities = [s for s in sensitivities
79                       if s > sens_range[0] and s < sens_range[1]]
80
81      # Initialize capture request
82      req = capture_request_utils.manual_capture_request(0, e_mid)
83      req['android.blackLevel.lock'] = True
84      req['android.tonemap.mode'] = 0
85      req['android.tonemap.curve'] = {'red': GAMMA_LUT.tolist(),
86                                      'green': GAMMA_LUT.tolist(),
87                                      'blue': GAMMA_LUT.tolist()}
88      # Do captures and calculate center patch RGB means
89      r_means = []
90      g_means = []
91      b_means = []
92      fmt = {'format': 'yuv', 'width': VGA_W, 'height': VGA_H}
93      for sens in sensitivities:
94        req['android.sensor.sensitivity'] = sens
95        cap = its_session_utils.do_capture_with_latency(
96            cam, req, sync_latency, fmt)
97        img = image_processing_utils.convert_capture_to_rgb_image(cap)
98        img_name = '%s_sens=%.04d.jpg' % (
99            os.path.join(self.log_path, NAME), sens)
100        image_processing_utils.write_image(img, img_name)
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(
121          '%s_plot_means.png' % os.path.join(self.log_path, NAME))
122
123      # Assert plot curves are linear w/ + slope by examining polyfit residual
124      for means in [r_means, g_means, b_means]:
125        line, residuals, _, _, _ = np.polyfit(
126            range(len(sensitivities)), means, 1, full=True)
127        logging.debug('Line: m=%f, b=%f, resid=%f',
128                      line[0], line[1], residuals[0])
129        if residuals[0] > RESIDUAL_THRESH:
130          raise AssertionError(
131              'residual: {residuals[0]:.5f}, THRESH: {RESIDUAL_THRESH}')
132        if line[0] <= 0:
133          raise AssertionError(f'slope {line[0]:.6f} <=  0!')
134
135if __name__ == '__main__':
136  test_runner.main()
137