1# Copyright 2014 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 os.path 16import its.caps 17import its.device 18import its.image 19import its.objects 20import matplotlib 21from matplotlib import pylab 22 23NAME = os.path.basename(__file__).split('.')[0] 24BAYER_LIST = ['R', 'GR', 'GB', 'B'] 25DIFF_THRESH = 0.0012 # absolute variance delta threshold 26FRAC_THRESH = 0.2 # relative variance delta threshold 27NUM_STEPS = 4 28STATS_GRID = 49 # center 2.04% of image for calculations 29 30 31def main(): 32 """Verify that the DNG raw model parameters are correct.""" 33 34 # Pass if the difference between expected and computed variances is small, 35 # defined as being within an absolute variance delta or relative variance 36 # delta of the expected variance, whichever is larger. This is to allow the 37 # test to pass in the presence of some randomness (since this test is 38 # measuring noise of a small patch) and some imperfect scene conditions 39 # (since ITS doesn't require a perfectly uniformly lit scene). 40 41 with its.device.ItsSession() as cam: 42 props = cam.get_camera_properties() 43 props = cam.override_with_hidden_physical_camera_props(props) 44 its.caps.skip_unless(its.caps.raw(props) and 45 its.caps.raw16(props) and 46 its.caps.manual_sensor(props) and 47 its.caps.read_3a(props) and 48 its.caps.per_frame_control(props) and 49 not its.caps.mono_camera(props)) 50 51 debug = its.caps.debug_mode() 52 53 white_level = float(props['android.sensor.info.whiteLevel']) 54 cfa_idxs = its.image.get_canonical_cfa_order(props) 55 aax = props['android.sensor.info.preCorrectionActiveArraySize']['left'] 56 aay = props['android.sensor.info.preCorrectionActiveArraySize']['top'] 57 aaw = props['android.sensor.info.preCorrectionActiveArraySize']['right']-aax 58 aah = props['android.sensor.info.preCorrectionActiveArraySize']['bottom']-aay 59 60 # Expose for the scene with min sensitivity 61 sens_min, sens_max = props['android.sensor.info.sensitivityRange'] 62 sens_step = (sens_max - sens_min) / NUM_STEPS 63 s_ae, e_ae, _, _, f_dist = cam.do_3a(get_results=True) 64 s_e_prod = s_ae * e_ae 65 sensitivities = range(sens_min, sens_max, sens_step) 66 67 var_expected = [[], [], [], []] 68 var_measured = [[], [], [], []] 69 x = STATS_GRID/2 # center in H of STATS_GRID 70 y = STATS_GRID/2 # center in W of STATS_GRID 71 for sens in sensitivities: 72 73 # Capture a raw frame with the desired sensitivity 74 exp = int(s_e_prod / float(sens)) 75 req = its.objects.manual_capture_request(sens, exp, f_dist) 76 if debug: 77 cap = cam.do_capture(req, cam.CAP_RAW) 78 planes = its.image.convert_capture_to_planes(cap, props) 79 else: 80 cap = cam.do_capture(req, {'format': 'rawStats', 81 'gridWidth': aaw/STATS_GRID, 82 'gridHeight': aah/STATS_GRID}) 83 mean_img, var_img = its.image.unpack_rawstats_capture(cap) 84 85 # Test each raw color channel (R, GR, GB, B) 86 noise_profile = cap['metadata']['android.sensor.noiseProfile'] 87 assert len(noise_profile) == len(BAYER_LIST) 88 for i in range(len(BAYER_LIST)): 89 # Get the noise model parameters for this channel of this shot. 90 ch = cfa_idxs[i] 91 s, o = noise_profile[ch] 92 93 # Use a very small patch to ensure gross uniformity (i.e. so 94 # non-uniform lighting or vignetting doesn't affect the variance 95 # calculation) 96 black_level = its.image.get_black_level(i, props, 97 cap['metadata']) 98 level_range = white_level - black_level 99 if debug: 100 plane = ((planes[i] * white_level - black_level) / 101 level_range) 102 tile = its.image.get_image_patch(plane, 0.49, 0.49, 103 0.02, 0.02) 104 mean_img_ch = tile.mean() 105 var_measured[i].append( 106 its.image.compute_image_variances(tile)[0]) 107 else: 108 mean_img_ch = (mean_img[x, y, ch]-black_level)/level_range 109 var_measured[i].append(var_img[x, y, ch]/level_range**2) 110 var_expected[i].append(s * mean_img_ch + o) 111 112 for i, ch in enumerate(BAYER_LIST): 113 pylab.plot(sensitivities, var_expected[i], 'rgkb'[i], 114 label=ch+' expected') 115 pylab.plot(sensitivities, var_measured[i], 'rgkb'[i]+'--', 116 label=ch+' measured') 117 pylab.xlabel('Sensitivity') 118 pylab.ylabel('Center patch variance') 119 pylab.legend(loc=2) 120 matplotlib.pyplot.savefig('%s_plot.png' % NAME) 121 122 # PASS/FAIL check 123 for i, ch in enumerate(BAYER_LIST): 124 diffs = [abs(var_measured[i][j] - var_expected[i][j]) 125 for j in range(len(sensitivities))] 126 print 'Diffs (%s):'%(ch), diffs 127 for j, diff in enumerate(diffs): 128 thresh = max(DIFF_THRESH, FRAC_THRESH*var_expected[i][j]) 129 assert diff <= thresh, 'diff: %.5f, thresh: %.4f' % (diff, thresh) 130 131if __name__ == '__main__': 132 main() 133