• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
15import os.path
16import its.caps
17import its.device
18import its.image
19import its.objects
20import matplotlib
21from matplotlib import pylab
22
23NAME = os.path.basename(__file__).split('.')[0]
24BAYER_LIST = ['R', 'GR', 'GB', 'B']
25DIFF_THRESH = 0.0005  # absolute variance delta threshold
26FRAC_THRESH = 0.2  # relative variance delta threshold
27NUM_STEPS = 4
28STATS_GRID = 49  # center 2.04% of image for calculations
29
30
31def main():
32    """Verify that the DNG raw model parameters are correct."""
33
34    # Pass if the difference between expected and computed variances is small,
35    # defined as being within an absolute variance delta or relative variance
36    # delta of the expected variance, whichever is larger. This is to allow the
37    # test to pass in the presence of some randomness (since this test is
38    # measuring noise of a small patch) and some imperfect scene conditions
39    # (since ITS doesn't require a perfectly uniformly lit scene).
40
41    with its.device.ItsSession() as cam:
42
43        props = cam.get_camera_properties()
44        its.caps.skip_unless(its.caps.raw(props) and
45                             its.caps.raw16(props) and
46                             its.caps.manual_sensor(props) and
47                             its.caps.read_3a(props) and
48                             its.caps.per_frame_control(props))
49        debug = its.caps.debug_mode()
50
51        white_level = float(props['android.sensor.info.whiteLevel'])
52        cfa_idxs = its.image.get_canonical_cfa_order(props)
53        aax = props['android.sensor.info.activeArraySize']['left']
54        aay = props['android.sensor.info.activeArraySize']['top']
55        aaw = props['android.sensor.info.activeArraySize']['right']-aax
56        aah = props['android.sensor.info.activeArraySize']['bottom']-aay
57
58        # Expose for the scene with min sensitivity
59        sens_min, sens_max = props['android.sensor.info.sensitivityRange']
60        sens_step = (sens_max - sens_min) / NUM_STEPS
61        s_ae, e_ae, _, _, f_dist = cam.do_3a(get_results=True)
62        s_e_prod = s_ae * e_ae
63        sensitivities = range(sens_min, sens_max, sens_step)
64
65        var_expected = [[], [], [], []]
66        var_measured = [[], [], [], []]
67        x = STATS_GRID/2  # center in H of STATS_GRID
68        y = STATS_GRID/2  # center in W of STATS_GRID
69        for sens in sensitivities:
70
71            # Capture a raw frame with the desired sensitivity
72            exp = int(s_e_prod / float(sens))
73            req = its.objects.manual_capture_request(sens, exp, f_dist)
74            if debug:
75                cap = cam.do_capture(req, cam.CAP_RAW)
76                planes = its.image.convert_capture_to_planes(cap, props)
77            else:
78                cap = cam.do_capture(req, {'format': 'rawStats',
79                                           'gridWidth': aaw/STATS_GRID,
80                                           'gridHeight': aah/STATS_GRID})
81                mean_img, var_img = its.image.unpack_rawstats_capture(cap)
82
83            # Test each raw color channel (R, GR, GB, B)
84            noise_profile = cap['metadata']['android.sensor.noiseProfile']
85            assert len(noise_profile) == len(BAYER_LIST)
86            for i in range(len(BAYER_LIST)):
87                # Get the noise model parameters for this channel of this shot.
88                ch = cfa_idxs[i]
89                s, o = noise_profile[ch]
90
91                # Use a very small patch to ensure gross uniformity (i.e. so
92                # non-uniform lighting or vignetting doesn't affect the variance
93                # calculation)
94                black_level = its.image.get_black_level(i, props,
95                                                        cap['metadata'])
96                level_range = white_level - black_level
97                if debug:
98                    plane = ((planes[i] * white_level - black_level) /
99                             level_range)
100                    tile = its.image.get_image_patch(plane, 0.49, 0.49,
101                                                     0.02, 0.02)
102                    mean_img_ch = tile.mean()
103                    var_measured[i].append(
104                            its.image.compute_image_variances(tile)[0])
105                else:
106                    mean_img_ch = (mean_img[x, y, ch]-black_level)/level_range
107                    var_measured[i].append(var_img[x, y, ch]/level_range**2)
108                var_expected[i].append(s * mean_img_ch + o)
109
110    for i, ch in enumerate(BAYER_LIST):
111        pylab.plot(sensitivities, var_expected[i], 'rgkb'[i],
112                   label=ch+' expected')
113        pylab.plot(sensitivities, var_measured[i], 'rgkb'[i]+'--',
114                   label=ch+' measured')
115    pylab.xlabel('Sensitivity')
116    pylab.ylabel('Center patch variance')
117    pylab.legend(loc=2)
118    matplotlib.pyplot.savefig('%s_plot.png' % NAME)
119
120    # PASS/FAIL check
121    for i, ch in enumerate(BAYER_LIST):
122        diffs = [var_measured[i][j] - var_expected[i][j]
123                 for j in range(NUM_STEPS)]
124        print 'Diffs (%s):'%(ch), diffs
125        for j, diff in enumerate(diffs):
126            thresh = max(DIFF_THRESH, FRAC_THRESH*var_expected[i][j])
127            assert diff <= thresh
128
129if __name__ == '__main__':
130    main()
131
132