1# Copyright 2015 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.caps 17import its.device 18import its.objects 19import its.target 20import math 21import matplotlib 22import matplotlib.pyplot 23import numpy 24import os.path 25import pylab 26 27def main(): 28 """Test that the android.noiseReduction.mode param is applied when set for 29 reprocessing requests. 30 31 Capture reprocessed images with the camera dimly lit. Uses a high analog 32 gain to ensure the captured image is noisy. 33 34 Captures three reprocessed images, for NR off, "fast", and "high quality". 35 Also captures a reprocessed image with low gain and NR off, and uses the 36 variance of this as the baseline. 37 """ 38 39 NAME = os.path.basename(__file__).split(".")[0] 40 41 NUM_SAMPLES_PER_MODE = 4 42 SNR_TOLERANCE = 3 # unit in db 43 44 with its.device.ItsSession() as cam: 45 props = cam.get_camera_properties() 46 47 its.caps.skip_unless(its.caps.compute_target_exposure(props) and 48 its.caps.per_frame_control(props) and 49 its.caps.noise_reduction_mode(props, 0) and 50 (its.caps.yuv_reprocess(props) or 51 its.caps.private_reprocess(props))) 52 53 # If reprocessing is supported, ZSL NR mode must be avaiable. 54 assert(its.caps.noise_reduction_mode(props, 4)) 55 56 reprocess_formats = [] 57 if (its.caps.yuv_reprocess(props)): 58 reprocess_formats.append("yuv") 59 if (its.caps.private_reprocess(props)): 60 reprocess_formats.append("private") 61 62 for reprocess_format in reprocess_formats: 63 # List of variances for R, G, B. 64 snrs = [[], [], []] 65 nr_modes_reported = [] 66 67 # NR mode 0 with low gain 68 e, s = its.target.get_target_exposure_combos(cam)["minSensitivity"] 69 req = its.objects.manual_capture_request(s, e) 70 req["android.noiseReduction.mode"] = 0 71 72 # Test reprocess_format->JPEG reprocessing 73 # TODO: Switch to reprocess_format->YUV when YUV reprocessing is 74 # supported. 75 size = its.objects.get_available_output_sizes("jpg", props)[0] 76 out_surface = {"width":size[0], "height":size[1], "format":"jpg"} 77 cap = cam.do_capture(req, out_surface, reprocess_format) 78 img = its.image.decompress_jpeg_to_rgb_image(cap["data"]) 79 its.image.write_image(img, "%s_low_gain_fmt=jpg.jpg" % (NAME)) 80 tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1) 81 ref_snr = its.image.compute_image_snrs(tile) 82 print "Ref SNRs:", ref_snr 83 84 e, s = its.target.get_target_exposure_combos(cam)["maxSensitivity"] 85 for nr_mode in range(5): 86 # Skip unavailable modes 87 if not its.caps.noise_reduction_mode(props, nr_mode): 88 nr_modes_reported.append(nr_mode) 89 for channel in range(3): 90 snrs[channel].append(0) 91 continue 92 93 rgb_snr_list = [] 94 # Capture several images to account for per frame noise 95 # variations 96 for n in range(NUM_SAMPLES_PER_MODE): 97 req = its.objects.manual_capture_request(s, e) 98 req["android.noiseReduction.mode"] = nr_mode 99 cap = cam.do_capture(req, out_surface, reprocess_format) 100 101 img = its.image.decompress_jpeg_to_rgb_image(cap["data"]) 102 if n == 0: 103 its.image.write_image( 104 img, 105 "%s_high_gain_nr=%d_fmt=jpg.jpg" 106 %(NAME, nr_mode)) 107 nr_modes_reported.append( 108 cap["metadata"]["android.noiseReduction.mode"]) 109 110 tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1) 111 # Get the variances for R, G, and B channels 112 rgb_snrs = its.image.compute_image_snrs(tile) 113 rgb_snr_list.append(rgb_snrs) 114 115 r_snrs = [rgb[0] for rgb in rgb_snr_list] 116 g_snrs = [rgb[1] for rgb in rgb_snr_list] 117 b_snrs = [rgb[2] for rgb in rgb_snr_list] 118 rgb_snrs = [numpy.mean(r_snrs), 119 numpy.mean(g_snrs), 120 numpy.mean(b_snrs)] 121 print "NR mode", nr_mode, "SNRs:" 122 print " R SNR:", rgb_snrs[0],\ 123 "Min:", min(r_snrs), "Max:", max(r_snrs) 124 print " G SNR:", rgb_snrs[1],\ 125 "Min:", min(g_snrs), "Max:", max(g_snrs) 126 print " B SNR:", rgb_snrs[2],\ 127 "Min:", min(b_snrs), "Max:", max(b_snrs) 128 129 for chan in range(3): 130 snrs[chan].append(rgb_snrs[chan]) 131 132 # Draw a plot. 133 for channel in range(3): 134 pylab.plot(range(5), snrs[channel], "rgb"[channel]) 135 136 matplotlib.pyplot.savefig("%s_plot_%s_SNRs.png" % 137 (NAME, reprocess_format)) 138 139 assert(nr_modes_reported == [0,1,2,3,4]) 140 141 for j in range(3): 142 # Larger is better 143 # Verify OFF(0) is not better than FAST(1) 144 assert(snrs[j][0] < 145 snrs[j][1] + SNR_TOLERANCE) 146 # Verify FAST(1) is not better than HQ(2) 147 assert(snrs[j][1] < 148 snrs[j][2] + SNR_TOLERANCE) 149 # Verify HQ(2) is better than OFF(0) 150 assert(snrs[j][0] < snrs[j][2]) 151 if its.caps.noise_reduction_mode(props, 3): 152 # Verify OFF(0) is not better than MINIMAL(3) 153 assert(snrs[j][0] < 154 snrs[j][3] + SNR_TOLERANCE) 155 # Verify MINIMAL(3) is not better than HQ(2) 156 assert(snrs[j][3] < 157 snrs[j][2] + SNR_TOLERANCE) 158 # Verify ZSL(4) is close to MINIMAL(3) 159 assert(numpy.isclose(snrs[j][4], snrs[j][3], 160 atol=SNR_TOLERANCE)) 161 else: 162 # Verify ZSL(4) is close to OFF(0) 163 assert(numpy.isclose(snrs[j][4], snrs[j][0], 164 atol=SNR_TOLERANCE)) 165 166if __name__ == '__main__': 167 main() 168 169