test_blc_lsc.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 BLC and LSC look reasonable.
25    """
26    NAME = os.path.basename(__file__).split(".")[0]
27
28    r_means_center = []
29    g_means_center = []
30    b_means_center = []
31    r_means_corner = []
32    g_means_corner = []
33    b_means_corner = []
34
35    with its.device.ItsSession() as cam:
36        # Get AE+AWB lock first, so the auto values in the capture result are
37        # populated properly.
38        r = [0,0,1,1]
39        ae_sen,ae_exp,awb_gains,awb_transform,_ = \
40                cam.do_3a(r,r,r,True,True,False)
41        ae_exp = ae_exp / 1000000.0
42        print "AE:", ae_sen, ae_exp
43        print "AWB:", awb_gains, awb_transform
44
45        # Set analog gain (sensitivity) to 800
46        ae_exp = ae_exp * ae_sen / 800
47        ae_sen = 800
48
49        # Capture range of exposures from 1/100x to 4x of AE estimate.
50        exposures = [ae_exp*x/100.0 for x in [1]+range(10,401,20)]
51        print "Exposures:", exposures
52
53        # Convert the transform back to rational.
54        awb_transform_rat = [{"numerator":int(100*x),"denominator":100}
55                             for x in awb_transform]
56
57        # Linear tonemap
58        tmap = sum([[i/63.0,i/63.0] for i in range(64)], [])
59
60        reqs = []
61        for e in exposures:
62            req = its.objects.manual_capture_request(ae_sen,e)
63            req["android.tonemap.mode"] = 0
64            req["android.tonemap.curveRed"] = tmap
65            req["android.tonemap.curveGreen"] = tmap
66            req["android.tonemap.curveBlue"] = tmap
67            req["android.colorCorrection.transform"] = awb_transform_rat
68            req["android.colorCorrection.gains"] = awb_gains
69            reqs.append(req)
70
71        fnames, w, h, cap_mds = cam.do_capture(reqs)
72        for i,fname in enumerate(fnames):
73            img = its.image.load_yuv420_to_rgb_image(fname, w, h)
74
75            tile_center = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
76            rgb_means = its.image.compute_image_means(tile_center)
77            r_means_center.append(rgb_means[0])
78            g_means_center.append(rgb_means[1])
79            b_means_center.append(rgb_means[2])
80
81            tile_corner = its.image.get_image_patch(img, 0.0, 0.0, 0.1, 0.1)
82            rgb_means = its.image.compute_image_means(tile_corner)
83            r_means_corner.append(rgb_means[0])
84            g_means_corner.append(rgb_means[1])
85            b_means_corner.append(rgb_means[2])
86
87    fig = matplotlib.pyplot.figure()
88    pylab.plot(exposures, r_means_center, 'r')
89    pylab.plot(exposures, g_means_center, 'g')
90    pylab.plot(exposures, b_means_center, 'b')
91    pylab.ylim([0,1])
92    matplotlib.pyplot.savefig("%s_plot_means_center.png" % (NAME))
93
94    fig = matplotlib.pyplot.figure()
95    pylab.plot(exposures, r_means_corner, 'r')
96    pylab.plot(exposures, g_means_corner, 'g')
97    pylab.plot(exposures, b_means_corner, 'b')
98    pylab.ylim([0,1])
99    matplotlib.pyplot.savefig("%s_plot_means_corner.png" % (NAME))
100
101if __name__ == '__main__':
102    main()
103
104