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 import pylab 19 import os.path 20 import matplotlib 21 import matplotlib.pyplot 22 23 def main(): 24 """Test that the android.noiseReduction.mode param is applied when set. 25 26 Capture images with the camera dimly lit. Uses a long exposure 27 and a high analog gain to ensure the captured image is noisy. 28 29 Captures three images, for NR off, "fast", and "high quality". 30 Also captures an image with low gain and NR off, and uses the variance 31 of this as the baseline. 32 """ 33 NAME = os.path.basename(__file__).split(".")[0] 34 35 THRESHOLD_MIN_VARIANCE_RATIO = 0.7 36 37 req = its.objects.capture_request( { 38 "android.control.mode": 0, 39 "android.control.aeMode": 0, 40 "android.control.awbMode": 0, 41 "android.control.afMode": 0, 42 "android.sensor.frameDuration": 0 43 }) 44 45 # List of variances for Y,U,V. 46 variances = [[],[],[]] 47 48 # Reference (baseline) variance for each of Y,U,V. 49 ref_variance = [] 50 51 nr_modes_reported = [] 52 53 with its.device.ItsSession() as cam: 54 # NR mode 0 with low gain 55 req["captureRequest"]["android.noiseReduction.mode"] = 0 56 req["captureRequest"]["android.sensor.sensitivity"] = 100 57 req["captureRequest"]["android.sensor.exposureTime"] = 20*1000*1000 58 fname, w, h, md_obj = cam.do_capture(req) 59 its.image.write_image( 60 its.image.load_yuv420_to_rgb_image(fname, w, h), 61 "%s_low_gain.jpg" % (NAME)) 62 planes = its.image.load_yuv420_to_yuv_planes(fname, w, h) 63 for j in range(3): 64 img = planes[j] 65 tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1) 66 ref_variance.append(its.image.compute_image_variances(tile)[0]) 67 68 for i in range(3): 69 # NR modes 0, 1, 2 with high gain 70 req["captureRequest"]["android.noiseReduction.mode"] = i 71 req["captureRequest"]["android.sensor.sensitivity"] = 100*16 72 req["captureRequest"]["android.sensor.exposureTime"] = ( 73 20*1000*1000/16) 74 fname, w, h, md_obj = cam.do_capture(req) 75 nr_modes_reported.append( 76 md_obj["captureResult"]["android.noiseReduction.mode"]) 77 its.image.write_image( 78 its.image.load_yuv420_to_rgb_image(fname, w, h), 79 "%s_high_gain_nr=%d.jpg" % (NAME, i)) 80 planes = its.image.load_yuv420_to_yuv_planes(fname, w, h) 81 for j in range(3): 82 img = planes[j] 83 tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1) 84 variance = its.image.compute_image_variances(tile)[0] 85 variances[j].append(variance / ref_variance[j]) 86 87 # Draw a plot. 88 for j in range(3): 89 pylab.plot(range(3), variances[j], "rgb"[j]) 90 matplotlib.pyplot.savefig("%s_plot_variances.png" % (NAME)) 91 92 assert(nr_modes_reported == [0,1,2]) 93 94 # Check that the variance of the NR=0 image is much higher than for the 95 # NR=1 and NR=2 images. 96 for j in range(3): 97 for i in range(1,3): 98 assert(variances[j][i] / variances[j][0] < 99 THRESHOLD_MIN_VARIANCE_RATIO) 100 101 if __name__ == '__main__': 102 main() 103 104