Home | History | Annotate | Download | only in scene1
      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.caps
     17 import its.device
     18 import its.objects
     19 import its.target
     20 import pylab
     21 import os.path
     22 import matplotlib
     23 import matplotlib.pyplot
     24 
     25 def main():
     26     """Test that the android.colorCorrection.* params are applied when set.
     27 
     28     Takes shots with different transform and gains values, and tests that
     29     they look correspondingly different. The transform and gains are chosen
     30     to make the output go redder or bluer.
     31 
     32     Uses a linear tonemap.
     33     """
     34     NAME = os.path.basename(__file__).split(".")[0]
     35 
     36     THRESHOLD_MAX_DIFF = 0.1
     37 
     38     with its.device.ItsSession() as cam:
     39         props = cam.get_camera_properties()
     40         its.caps.skip_unless(its.caps.compute_target_exposure(props) and
     41                              its.caps.per_frame_control(props))
     42 
     43         # Baseline request
     44         e, s = its.target.get_target_exposure_combos(cam)["midSensitivity"]
     45         req = its.objects.manual_capture_request(s, e, True)
     46         req["android.colorCorrection.mode"] = 0
     47 
     48         # Transforms:
     49         # 1. Identity
     50         # 2. Identity
     51         # 3. Boost blue
     52         transforms = [its.objects.int_to_rational([1,0,0, 0,1,0, 0,0,1]),
     53                       its.objects.int_to_rational([1,0,0, 0,1,0, 0,0,1]),
     54                       its.objects.int_to_rational([1,0,0, 0,1,0, 0,0,2])]
     55 
     56         # Gains:
     57         # 1. Unit
     58         # 2. Boost red
     59         # 3. Unit
     60         gains = [[1,1,1,1], [2,1,1,1], [1,1,1,1]]
     61 
     62         r_means = []
     63         g_means = []
     64         b_means = []
     65 
     66         # Capture requests:
     67         # 1. With unit gains, and identity transform.
     68         # 2. With a higher red gain, and identity transform.
     69         # 3. With unit gains, and a transform that boosts blue.
     70         for i in range(len(transforms)):
     71             req["android.colorCorrection.transform"] = transforms[i]
     72             req["android.colorCorrection.gains"] = gains[i]
     73             cap = cam.do_capture(req)
     74             img = its.image.convert_capture_to_rgb_image(cap)
     75             its.image.write_image(img, "%s_req=%d.jpg" % (NAME, i))
     76             tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
     77             rgb_means = its.image.compute_image_means(tile)
     78             r_means.append(rgb_means[0])
     79             g_means.append(rgb_means[1])
     80             b_means.append(rgb_means[2])
     81             ratios = [rgb_means[0] / rgb_means[1], rgb_means[2] / rgb_means[1]]
     82             print "Means = ", rgb_means, "   Ratios =", ratios
     83 
     84         # Draw a plot.
     85         domain = range(len(transforms))
     86         pylab.plot(domain, r_means, 'r')
     87         pylab.plot(domain, g_means, 'g')
     88         pylab.plot(domain, b_means, 'b')
     89         pylab.ylim([0,1])
     90         matplotlib.pyplot.savefig("%s_plot_means.png" % (NAME))
     91 
     92         # Expect G0 == G1 == G2, R0 == 0.5*R1 == R2, B0 == B1 == 0.5*B2
     93         # Also need to ensure that the image is not clamped to white/black.
     94         assert(all(g_means[i] > 0.2 and g_means[i] < 0.8 for i in xrange(3)))
     95         assert(abs(g_means[1] - g_means[0]) < THRESHOLD_MAX_DIFF)
     96         assert(abs(g_means[2] - g_means[1]) < THRESHOLD_MAX_DIFF)
     97         assert(abs(r_means[2] - r_means[0]) < THRESHOLD_MAX_DIFF)
     98         assert(abs(r_means[1] - 2.0 * r_means[0]) < THRESHOLD_MAX_DIFF)
     99         assert(abs(b_means[1] - b_means[0]) < THRESHOLD_MAX_DIFF)
    100         assert(abs(b_means[2] - 2.0 * b_means[0]) < THRESHOLD_MAX_DIFF)
    101 
    102 if __name__ == '__main__':
    103     main()
    104 
    105