• 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 its.device
16import its.caps
17import its.objects
18import its.image
19import os.path
20import pylab
21import matplotlib
22import matplotlib.pyplot
23
24def main():
25    """Capture a set of raw images with increasing gains and measure the noise.
26    """
27    NAME = os.path.basename(__file__).split(".")[0]
28
29    # Each shot must be 1% noisier (by the variance metric) than the previous
30    # one.
31    VAR_THRESH = 1.01
32
33    NUM_STEPS = 5
34
35    with its.device.ItsSession() as cam:
36
37        props = cam.get_camera_properties()
38        if (not its.caps.raw16(props) or
39            not its.caps.manual_sensor(props) or
40            not its.caps.read_3a(props)):
41            print "Test skipped"
42            return
43
44        # Expose for the scene with min sensitivity
45        sens_min, sens_max = props['android.sensor.info.sensitivityRange']
46        sens_step = (sens_max - sens_min) / NUM_STEPS
47        s_ae,e_ae,_,_,_  = cam.do_3a(get_results=True)
48        s_e_prod = s_ae * e_ae
49
50        variances = []
51        for s in range(sens_min, sens_max, sens_step):
52
53            e = int(s_e_prod / float(s))
54            req = its.objects.manual_capture_request(s, e)
55
56            # Capture raw+yuv, but only look at the raw.
57            cap,_ = cam.do_capture(req, cam.CAP_RAW_YUV)
58
59            # Measure the variance. Each shot should be noisier than the
60            # previous shot (as the gain is increasing).
61            plane = its.image.convert_capture_to_planes(cap, props)[1]
62            tile = its.image.get_image_patch(plane, 0.45,0.45,0.1,0.1)
63            var = its.image.compute_image_variances(tile)[0]
64            variances.append(var)
65
66            img = its.image.convert_capture_to_rgb_image(cap, props=props)
67            its.image.write_image(img, "%s_s=%05d_var=%f.jpg" % (NAME,s,var))
68            print "s=%d, e=%d, var=%e"%(s,e,var)
69
70        pylab.plot(range(len(variances)), variances)
71        matplotlib.pyplot.savefig("%s_variances.png" % (NAME))
72
73        # Test that each shot is noisier than the previous one.
74        for i in range(len(variances) - 1):
75            assert(variances[i] < variances[i+1] / VAR_THRESH)
76
77if __name__ == '__main__':
78    main()
79
80