test_param_noise_reduction.py revision 12339cdd90b2c98eb4f7adf794ddca8de53992b8
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
15import its.image
16import its.device
17import its.objects
18import pylab
19import os.path
20import matplotlib
21import matplotlib.pyplot
22
23def main():
24    """Test that the android.noiseReduction.mode param is applied when set.
25
26    Capture images with the camera dimly lit. Uses a long exposure
27    and a high analog gain to ensure the captured image is noisy.
28
29    Captures three images, for NR off, "fast", and "high quality".
30    Also captures an image with low gain and NR off, and uses the variance
31    of this as the baseline.
32    """
33    NAME = os.path.basename(__file__).split(".")[0]
34
35    req = its.objects.capture_request( {
36        "android.control.aeMode": 0,
37        "android.sensor.frameDuration": 0
38        })
39
40    # List of variances for Y,U,V.
41    variances = [[],[],[]]
42
43    # Reference (baseline) variance for each of Y,U,V.
44    ref_variance = []
45
46    with its.device.ItsSession() as cam:
47        # NR mode 0 with low gain
48        req["captureRequest"]["android.noiseReduction.mode"] = 0
49        req["captureRequest"]["android.sensor.sensitivity"] = 100
50        req["captureRequest"]["android.sensor.exposureTime"] = 100*1000*1000
51        fname, w, h, md_obj = cam.do_capture(req)
52        its.image.write_image(
53                its.image.load_yuv420_to_rgb_image(fname, w, h),
54                "%s_low_gain.jpg" % (NAME))
55        planes = its.image.load_yuv420_to_yuv_planes(fname, w, h)
56        for j in range(3):
57            img = planes[j]
58            tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
59            ref_variance.append(its.image.compute_image_variances(tile)[0])
60
61        for i in range(3):
62            # NR modes 0, 1, 2 with high gain
63            req["captureRequest"]["android.noiseReduction.mode"] = i
64            req["captureRequest"]["android.sensor.sensitivity"] = 1600
65            req["captureRequest"]["android.sensor.exposureTime"] = (
66                    100*1000*1000/16)
67            fname, w, h, md_obj = cam.do_capture(req)
68            its.image.write_image(
69                    its.image.load_yuv420_to_rgb_image(fname, w, h),
70                    "%s_high_gain_nr=%d.jpg" % (NAME, i))
71            planes = its.image.load_yuv420_to_yuv_planes(fname, w, h)
72            for j in range(3):
73                img = planes[j]
74                tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
75                variance = its.image.compute_image_variances(tile)[0]
76                variances[j].append(variance / ref_variance[j])
77
78    # Draw a plot.
79    for j in range(3):
80        pylab.plot(range(3), variances[j], "rgb"[j])
81    matplotlib.pyplot.savefig("%s_plot_variances.png" % (NAME))
82
83if __name__ == '__main__':
84    main()
85
86