test_black_level.py revision 87df78b48064a9bd31083968476e10242807985f
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
22import numpy
23
24def main():
25    """Black level consistence test.
26
27    Test: capture dark frames and check if black level correction is done
28    correctly.
29    1. Black level should be roughly consistent for repeating shots.
30    2. Noise distribution should be roughly centered at black level.
31
32    Shoot with the camera covered (i.e.) dark/black. The test varies the
33    sensitivity parameter.
34    """
35    NAME = os.path.basename(__file__).split(".")[0]
36
37    NUM_REPEAT = 3
38    NUM_SENSITIVITY_STEPS = 3
39
40    # Only check the center part where LSC has little effects.
41    R = 200
42
43    # The most frequent pixel value in each image; assume this is the black
44    # level, since the images are all dark (shot with the lens covered).
45    ymodes = []
46    umodes = []
47    vmodes = []
48
49    with its.device.ItsSession() as cam:
50        props = cam.get_camera_properties()
51        sens_range = props['android.sensor.info.sensitivityRange']
52        sensitivities = range(sens_range[0],
53                              sens_range[1]+1,
54                              int((sens_range[1] - sens_range[0]) /
55                                  (NUM_SENSITIVITY_STEPS - 1)))
56        print "Sensitivities:", sensitivities
57        for si, s in enumerate(sensitivities):
58            for rep in xrange(NUM_REPEAT):
59                req = its.objects.manual_capture_request(100, 1)
60                req["captureRequest"]["android.blackLevel.lock"] = True
61                req["captureRequest"]["android.sensor.sensitivity"] = s
62                fname, w, h, cap_md = cam.do_capture(req)
63                yimg,uimg,vimg = its.image.load_yuv420_to_yuv_planes(fname,w,h)
64
65                # Magnify the noise in saved images to help visualize.
66                its.image.write_image(yimg * 2,
67                                      "%s_s=%05d_y.jpg" % (NAME, s), True)
68                its.image.write_image(numpy.absolute(uimg - 0.5) * 2,
69                                      "%s_s=%05d_u.jpg" % (NAME, s), True)
70
71                yimg = yimg[w/2-R:w/2+R, h/2-R:h/2+R]
72                uimg = uimg[w/4-R/2:w/4+R/2, w/4-R/2:w/4+R/2]
73                vimg = vimg[w/4-R/2:w/4+R/2, w/4-R/2:w/4+R/2]
74                yhist,_ = numpy.histogram(yimg*255, 256, (0,256))
75                ymodes.append(numpy.argmax(yhist))
76                uhist,_ = numpy.histogram(uimg*255, 256, (0,256))
77                umodes.append(numpy.argmax(uhist))
78                vhist,_ = numpy.histogram(vimg*255, 256, (0,256))
79                vmodes.append(numpy.argmax(vhist))
80
81                # Take 32 bins from Y, U, and V.
82                # Histograms of U and V are cropped at the center of 128.
83                pylab.plot(range(32), yhist.tolist()[0:32], 'rgb'[si])
84                pylab.plot(range(32), uhist.tolist()[112:144], 'rgb'[si]+'--')
85                pylab.plot(range(32), vhist.tolist()[112:144], 'rgb'[si]+'--')
86
87    pylab.xlabel("DN: Y[0:32], U[112:144], V[112:144]")
88    pylab.ylabel("Pixel count")
89    pylab.title("Histograms for different sensitivities")
90    matplotlib.pyplot.savefig("%s_plot_histograms.png" % (NAME))
91
92    print "Y black levels:", ymodes
93    print "U black levels:", umodes
94    print "V black levels:", vmodes
95
96if __name__ == '__main__':
97    main()
98
99