test_param_noise_reduction.py revision 87b68b020dd72c4cdcf3b8c1f9196c060f947991
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.5
36
37    req = its.objects.capture_request( {
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    with its.device.ItsSession() as cam:
52        # NR mode 0 with low gain
53        req["captureRequest"]["android.noiseReduction.mode"] = 0
54        req["captureRequest"]["android.sensor.sensitivity"] = 100
55        req["captureRequest"]["android.sensor.exposureTime"] = 20*1000*1000
56        fname, w, h, md_obj = cam.do_capture(req)
57        its.image.write_image(
58                its.image.load_yuv420_to_rgb_image(fname, w, h),
59                "%s_low_gain.jpg" % (NAME))
60        planes = its.image.load_yuv420_to_yuv_planes(fname, w, h)
61        for j in range(3):
62            img = planes[j]
63            tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
64            ref_variance.append(its.image.compute_image_variances(tile)[0])
65
66        for i in range(3):
67            # NR modes 0, 1, 2 with high gain
68            req["captureRequest"]["android.noiseReduction.mode"] = i
69            req["captureRequest"]["android.sensor.sensitivity"] = 1600
70            req["captureRequest"]["android.sensor.exposureTime"] = (
71                    20*1000*1000/16)
72            fname, w, h, md_obj = cam.do_capture(req)
73            its.image.write_image(
74                    its.image.load_yuv420_to_rgb_image(fname, w, h),
75                    "%s_high_gain_nr=%d.jpg" % (NAME, i))
76            planes = its.image.load_yuv420_to_yuv_planes(fname, w, h)
77            for j in range(3):
78                img = planes[j]
79                tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
80                variance = its.image.compute_image_variances(tile)[0]
81                variances[j].append(variance / ref_variance[j])
82
83    # Draw a plot.
84    for j in range(3):
85        pylab.plot(range(3), variances[j], "rgb"[j])
86    matplotlib.pyplot.savefig("%s_plot_variances.png" % (NAME))
87
88    # Check that the variance of the NR=0 image is much higher than for the
89    # NR=1 and NR=2 images.
90    for j in range(3):
91        for i in range(1,3):
92            assert(variances[j][i] / variances[j][0] <
93                   THRESHOLD_MIN_VARIANCE_RATIO)
94
95if __name__ == '__main__':
96    main()
97
98