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.noiseReduction.mode param is applied when set.
     27 
     28     Capture images with the camera dimly lit. Uses a high analog gain to
     29     ensure the captured image is noisy.
     30 
     31     Captures three images, for NR off, "fast", and "high quality".
     32     Also captures an image with low gain and NR off, and uses the variance
     33     of this as the baseline.
     34     """
     35     NAME = os.path.basename(__file__).split(".")[0]
     36 
     37     # List of variances for Y,U,V.
     38     variances = [[],[],[]]
     39 
     40     # Reference (baseline) variance for each of Y,U,V.
     41     ref_variance = []
     42 
     43     nr_modes_reported = []
     44 
     45     with its.device.ItsSession() as cam:
     46         props = cam.get_camera_properties()
     47         its.caps.skip_unless(its.caps.compute_target_exposure(props) and
     48                              its.caps.per_frame_control(props))
     49 
     50         # NR mode 0 with low gain
     51         e, s = its.target.get_target_exposure_combos(cam)["minSensitivity"]
     52         req = its.objects.manual_capture_request(s, e)
     53         req["android.noiseReduction.mode"] = 0
     54         cap = cam.do_capture(req)
     55         its.image.write_image(
     56                 its.image.convert_capture_to_rgb_image(cap),
     57                 "%s_low_gain.jpg" % (NAME))
     58         planes = its.image.convert_capture_to_planes(cap)
     59         for j in range(3):
     60             img = planes[j]
     61             tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
     62             ref_variance.append(its.image.compute_image_variances(tile)[0])
     63         print "Ref variances:", ref_variance
     64 
     65         for i in range(3):
     66             # NR modes 0, 1, 2 with high gain
     67             e, s = its.target.get_target_exposure_combos(cam)["maxSensitivity"]
     68             req = its.objects.manual_capture_request(s, e)
     69             req["android.noiseReduction.mode"] = i
     70             cap = cam.do_capture(req)
     71             nr_modes_reported.append(
     72                     cap["metadata"]["android.noiseReduction.mode"])
     73             its.image.write_image(
     74                     its.image.convert_capture_to_rgb_image(cap),
     75                     "%s_high_gain_nr=%d.jpg" % (NAME, i))
     76             planes = its.image.convert_capture_to_planes(cap)
     77             for j in range(3):
     78                 img = planes[j]
     79                 tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
     80                 variance = its.image.compute_image_variances(tile)[0]
     81                 variances[j].append(variance / ref_variance[j])
     82         print "Variances with NR mode [0,1,2]:", variances
     83 
     84     # Draw a plot.
     85     for j in range(3):
     86         pylab.plot(range(3), variances[j], "rgb"[j])
     87     matplotlib.pyplot.savefig("%s_plot_variances.png" % (NAME))
     88 
     89     assert(nr_modes_reported == [0,1,2])
     90 
     91     # Check that the variance of the NR=0 image is higher than for the
     92     # NR=1 and NR=2 images.
     93     for j in range(3):
     94         for i in range(1,3):
     95             assert(variances[j][i] < variances[j][0])
     96 
     97 if __name__ == '__main__':
     98     main()
     99 
    100