test_param_noise_reduction.py revision 4902d71d230d21639be817a83e8998230a6ff5e0
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    THRESHOLD_MIN_VARIANCE_RATIO = 0.7
36
37    req = {
38        "android.control.mode": 0,
39        "android.control.aeMode": 0,
40        "android.control.awbMode": 0,
41        "android.control.afMode": 0,
42        "android.sensor.frameDuration": 0
43        }
44
45    # List of variances for Y,U,V.
46    variances = [[],[],[]]
47
48    # Reference (baseline) variance for each of Y,U,V.
49    ref_variance = []
50
51    nr_modes_reported = []
52
53    with its.device.ItsSession() as cam:
54        # NR mode 0 with low gain
55        req["android.noiseReduction.mode"] = 0
56        req["android.sensor.sensitivity"] = 100
57        req["android.sensor.exposureTime"] = 20*1000*1000
58        fname, w, h, md_obj = cam.do_capture(req)
59        its.image.write_image(
60                its.image.load_yuv420_to_rgb_image(fname, w, h),
61                "%s_low_gain.jpg" % (NAME))
62        planes = its.image.load_yuv420_to_yuv_planes(fname, w, h)
63        for j in range(3):
64            img = planes[j]
65            tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
66            ref_variance.append(its.image.compute_image_variances(tile)[0])
67
68        for i in range(3):
69            # NR modes 0, 1, 2 with high gain
70            req["android.noiseReduction.mode"] = i
71            req["android.sensor.sensitivity"] = 100*16
72            req["android.sensor.exposureTime"] = (20*1000*1000/16)
73            fname, w, h, md_obj = cam.do_capture(req)
74            nr_modes_reported.append(md_obj["android.noiseReduction.mode"])
75            its.image.write_image(
76                    its.image.load_yuv420_to_rgb_image(fname, w, h),
77                    "%s_high_gain_nr=%d.jpg" % (NAME, i))
78            planes = its.image.load_yuv420_to_yuv_planes(fname, w, h)
79            for j in range(3):
80                img = planes[j]
81                tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
82                variance = its.image.compute_image_variances(tile)[0]
83                variances[j].append(variance / ref_variance[j])
84
85    # Draw a plot.
86    for j in range(3):
87        pylab.plot(range(3), variances[j], "rgb"[j])
88    matplotlib.pyplot.savefig("%s_plot_variances.png" % (NAME))
89
90    assert(nr_modes_reported == [0,1,2])
91
92    # Check that the variance of the NR=0 image is much higher than for the
93    # NR=1 and NR=2 images.
94    for j in range(3):
95        for i in range(1,3):
96            assert(variances[j][i] / variances[j][0] <
97                   THRESHOLD_MIN_VARIANCE_RATIO)
98
99if __name__ == '__main__':
100    main()
101
102