test_param_color_correction.py revision f783eb794a43d7ed945bd2834a4f26b7e8dcc9ba
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.colorCorrection.* params are applied when set.
25
26    Takes shots with different transform and gains values, and tests that
27    they look correspondingly different. The transform and gains are chosen
28    to make the output go redder or bluer.
29
30    Uses a linear tonemap.
31    """
32    NAME = os.path.basename(__file__).split(".")[0]
33
34    THRESHOLD_MAX_DIFF = 0.1
35
36    # Capture requests:
37    # 1. With unit gains, and identity transform.
38    # 2. With a higher red gain, and identity transform.
39    # 3. With unit gains, and a transform that boosts blue.
40
41    linear_tonemap = sum([[i/31.0,i/31.0] for i in range(32)], [])
42
43    # Baseline request
44    req = its.objects.capture_request( {
45        "android.control.mode": 0,
46        "android.control.aeMode": 0,
47        "android.control.awbMode": 0,
48        "android.control.afMode": 0,
49        "android.colorCorrection.mode": 0,
50        "android.sensor.frameDuration": 0,
51        "android.sensor.sensitivity": 200,
52        "android.sensor.exposureTime": 100*1000*1000,
53        "android.tonemap.mode": 0,
54        "android.tonemap.curveRed": linear_tonemap,
55        "android.tonemap.curveGreen": linear_tonemap,
56        "android.tonemap.curveBlue": linear_tonemap
57        })
58
59    # Transforms:
60    # 1. Identity
61    # 2. Identity
62    # 3. Boost blue
63    transforms = [its.objects.int_to_rational([1,0,0, 0,1,0, 0,0,1]),
64                  its.objects.int_to_rational([1,0,0, 0,1,0, 0,0,1]),
65                  its.objects.int_to_rational([1,0,0, 0,1,0, 0,0,2])]
66
67    # Gains:
68    # 1. Unit
69    # 2. Boost red
70    # 3. Unit
71    gains = [[1,1,1,1], [2,1,1,1], [1,1,1,1]]
72
73    r_means = []
74    g_means = []
75    b_means = []
76
77    with its.device.ItsSession() as cam:
78        for i in range(len(transforms)):
79            req['captureRequest']["android.colorCorrection.transform"] = (
80                    transforms[i])
81            req['captureRequest']["android.colorCorrection.gains"] = gains[i]
82            fname, w, h, md_obj = cam.do_capture(req)
83            img = its.image.load_yuv420_to_rgb_image(fname, w, h)
84            its.image.write_image(img, "%s_req=%d.jpg" % (NAME, i))
85            tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
86            rgb_means = its.image.compute_image_means(tile)
87            r_means.append(rgb_means[0])
88            g_means.append(rgb_means[1])
89            b_means.append(rgb_means[2])
90            ratios = [rgb_means[0] / rgb_means[1], rgb_means[2] / rgb_means[1]]
91            print "Means = ", rgb_means, "   Ratios =", ratios
92
93    # Draw a plot.
94    domain = range(len(transforms))
95    pylab.plot(domain, r_means, 'r')
96    pylab.plot(domain, g_means, 'g')
97    pylab.plot(domain, b_means, 'b')
98    pylab.ylim([0,1])
99    matplotlib.pyplot.savefig("%s_plot_means.png" % (NAME))
100
101    # Expect G0 == G1 == G2, R0 == 0.5*R1 == R2, B0 == B1 == 0.5*B2
102    # Also need to ensure that the imasge is not clamped to white/black.
103    assert(all(g_means[i] > 0.2 and g_means[i] < 0.8 for i in xrange(3)))
104    assert(abs(g_means[1] - g_means[0]) < THRESHOLD_MAX_DIFF)
105    assert(abs(g_means[2] - g_means[1]) < THRESHOLD_MAX_DIFF)
106    assert(abs(r_means[2] - r_means[0]) < THRESHOLD_MAX_DIFF)
107    assert(abs(r_means[1] - 2.0 * r_means[0]) < THRESHOLD_MAX_DIFF)
108    assert(abs(b_means[1] - b_means[0]) < THRESHOLD_MAX_DIFF)
109    assert(abs(b_means[2] - 2.0 * b_means[0]) < THRESHOLD_MAX_DIFF)
110
111if __name__ == '__main__':
112    main()
113
114