Home | History | Annotate | Download | only in inprog
      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 
     15 import its.image
     16 import its.device
     17 import its.objects
     18 from matplotlib import pylab
     19 import os.path
     20 import matplotlib
     21 import matplotlib.pyplot
     22 import numpy
     23 
     24 def 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_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         sens_step = (sens_range[1] - sens_range[0]) / float(NUM_STEPS-1)
     53         sensitivities = [sens_range[0] + i*sens_step for i in range(NUM_STEPS)]
     54         print "Sensitivities:", sensitivities
     55 
     56         for si, s in enumerate(sensitivities):
     57             for rep in xrange(NUM_REPEAT):
     58                 req = its.objects.manual_capture_request(100, 1*1000*1000)
     59                 req["android.blackLevel.lock"] = True
     60                 req["android.sensor.sensitivity"] = s
     61                 cap = cam.do_capture(req)
     62                 yimg,uimg,vimg = its.image.convert_capture_to_planes(cap)
     63                 w = cap["width"]
     64                 h = cap["height"]
     65 
     66                 # Magnify the noise in saved images to help visualize.
     67                 its.image.write_image(yimg * 2,
     68                                       "%s_s=%05d_y.jpg" % (NAME, s), True)
     69                 its.image.write_image(numpy.absolute(uimg - 0.5) * 2,
     70                                       "%s_s=%05d_u.jpg" % (NAME, s), True)
     71 
     72                 yimg = yimg[w/2-R:w/2+R, h/2-R:h/2+R]
     73                 uimg = uimg[w/4-R/2:w/4+R/2, w/4-R/2:w/4+R/2]
     74                 vimg = vimg[w/4-R/2:w/4+R/2, w/4-R/2:w/4+R/2]
     75                 yhist,_ = numpy.histogram(yimg*255, 256, (0,256))
     76                 ymodes.append(numpy.argmax(yhist))
     77                 uhist,_ = numpy.histogram(uimg*255, 256, (0,256))
     78                 umodes.append(numpy.argmax(uhist))
     79                 vhist,_ = numpy.histogram(vimg*255, 256, (0,256))
     80                 vmodes.append(numpy.argmax(vhist))
     81 
     82                 # Take 32 bins from Y, U, and V.
     83                 # Histograms of U and V are cropped at the center of 128.
     84                 pylab.plot(range(32), yhist.tolist()[0:32], 'rgb'[si])
     85                 pylab.plot(range(32), uhist.tolist()[112:144], 'rgb'[si]+'--')
     86                 pylab.plot(range(32), vhist.tolist()[112:144], 'rgb'[si]+'--')
     87 
     88     pylab.xlabel("DN: Y[0:32], U[112:144], V[112:144]")
     89     pylab.ylabel("Pixel count")
     90     pylab.title("Histograms for different sensitivities")
     91     matplotlib.pyplot.savefig("%s_plot_histograms.png" % (NAME))
     92 
     93     print "Y black levels:", ymodes
     94     print "U black levels:", umodes
     95     print "V black levels:", vmodes
     96 
     97 if __name__ == '__main__':
     98     main()
     99 
    100