test_param_color_correction.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.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    # Capture requests:
35    # 1. With unit gains, and identity transform.
36    # 2. With a higher red gain, and identity transform.
37    # 3. With unit gains, and a transform that boosts blue.
38
39    linear_tonemap = sum([[i/31.0,i/31.0] for i in range(32)], [])
40
41    # Baseline request
42    req = its.objects.capture_request( {
43        "android.control.mode": 0,
44        "android.control.aeMode": 0,
45        "android.control.awbMode": 0,
46        "android.control.afMode": 0,
47        "android.colorCorrection.mode": 0,
48        "android.sensor.frameDuration": 0,
49        "android.sensor.sensitivity": 200,
50        "android.sensor.exposureTime": 100*1000*1000,
51        "android.tonemap.mode": 0,
52        "android.tonemap.curveRed": linear_tonemap,
53        "android.tonemap.curveGreen": linear_tonemap,
54        "android.tonemap.curveBlue": linear_tonemap
55        })
56
57    # Transforms:
58    # 1. Identity
59    # 2. Identity
60    # 3. Boost blue
61    transforms = [its.objects.int_to_rational([1,0,0, 0,1,0, 0,0,1]),
62                  its.objects.int_to_rational([1,0,0, 0,1,0, 0,0,1]),
63                  its.objects.int_to_rational([1,0,0, 0,1,0, 0,0,2])]
64
65    # Gains:
66    # 1. Unit
67    # 2. Boost red
68    # 3. Unit
69    gains = [[1,1,1,1], [2,1,1,1], [1,1,1,1]]
70
71    r_means = []
72    g_means = []
73    b_means = []
74
75    with its.device.ItsSession() as cam:
76        for i in range(len(transforms)):
77            req['captureRequest']["android.colorCorrection.transform"] = (
78                    transforms[i])
79            req['captureRequest']["android.colorCorrection.gains"] = gains[i]
80            fname, w, h, md_obj = cam.do_capture(req)
81            img = its.image.load_yuv420_to_rgb_image(fname, w, h)
82            its.image.write_image(img, "%s_req=%d.jpg" % (NAME, i))
83            tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
84            rgb_means = its.image.compute_image_means(tile)
85            r_means.append(rgb_means[0])
86            g_means.append(rgb_means[1])
87            b_means.append(rgb_means[2])
88            ratios = [rgb_means[0] / rgb_means[1], rgb_means[2] / rgb_means[1]]
89            print "Means = ", rgb_means, "   Ratios =", ratios
90
91    # Draw a plot.
92    domain = range(len(transforms))
93    pylab.plot(domain, r_means, 'r')
94    pylab.plot(domain, g_means, 'g')
95    pylab.plot(domain, b_means, 'b')
96    pylab.ylim([0,1])
97    matplotlib.pyplot.savefig("%s_plot_means.png" % (NAME))
98
99if __name__ == '__main__':
100    main()
101
102