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 15import its.image 16import its.caps 17import its.device 18import its.objects 19import its.target 20import pylab 21import numpy 22import os.path 23import matplotlib 24import matplotlib.pyplot 25 26def main(): 27 """Test that a constant exposure is seen as ISO and exposure time vary. 28 29 Take a series of shots that have ISO and exposure time chosen to balance 30 each other; result should be the same brightness, but over the sequence 31 the images should get noisier. 32 """ 33 NAME = os.path.basename(__file__).split(".")[0] 34 35 THRESHOLD_MAX_OUTLIER_DIFF = 0.1 36 THRESHOLD_MIN_LEVEL = 0.1 37 THRESHOLD_MAX_LEVEL = 0.9 38 THRESHOLD_MAX_LEVEL_DIFF = 0.045 39 THRESHOLD_MAX_LEVEL_DIFF_WIDE_RANGE = 0.06 40 THRESHOLD_ROUND_DOWN_GAIN = 0.1 41 THRESHOLD_ROUND_DOWN_EXP = 0.05 42 43 mults = [] 44 r_means = [] 45 g_means = [] 46 b_means = [] 47 threshold_max_level_diff = THRESHOLD_MAX_LEVEL_DIFF 48 49 with its.device.ItsSession() as cam: 50 props = cam.get_camera_properties() 51 its.caps.skip_unless(its.caps.compute_target_exposure(props) and 52 its.caps.per_frame_control(props)) 53 54 debug = its.caps.debug_mode() 55 if debug: 56 fmt = its.objects.get_largest_yuv_format(props) 57 else: 58 fmt = its.objects.get_smallest_yuv_format(props) 59 60 e,s = its.target.get_target_exposure_combos(cam)["minSensitivity"] 61 s_e_product = s*e 62 expt_range = props['android.sensor.info.exposureTimeRange'] 63 sens_range = props['android.sensor.info.sensitivityRange'] 64 65 m = 1.0 66 while s*m < sens_range[1] and e/m > expt_range[0]: 67 mults.append(m) 68 s_test = round(s*m) 69 e_test = s_e_product / s_test 70 print "Testing s:", s_test, "e:", e_test 71 req = its.objects.manual_capture_request( 72 s_test, e_test, 0.0, True, props) 73 cap = cam.do_capture(req, fmt) 74 s_res = cap["metadata"]["android.sensor.sensitivity"] 75 e_res = cap["metadata"]["android.sensor.exposureTime"] 76 assert(0 <= s_test - s_res < s_test * THRESHOLD_ROUND_DOWN_GAIN) 77 assert(0 <= e_test - e_res < e_test * THRESHOLD_ROUND_DOWN_EXP) 78 s_e_product_res = s_res * e_res 79 request_result_ratio = s_e_product / s_e_product_res 80 print "Capture result s:", s_test, "e:", e_test 81 img = its.image.convert_capture_to_rgb_image(cap) 82 its.image.write_image(img, "%s_mult=%3.2f.jpg" % (NAME, m)) 83 tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1) 84 rgb_means = its.image.compute_image_means(tile) 85 # Adjust for the difference between request and result 86 r_means.append(rgb_means[0] * request_result_ratio) 87 g_means.append(rgb_means[1] * request_result_ratio) 88 b_means.append(rgb_means[2] * request_result_ratio) 89 # Test 3 steps per 2x gain 90 m = m * pow(2, 1.0 / 3) 91 92 # Allow more threshold for devices with wider exposure range 93 if m >= 64.0: 94 threshold_max_level_diff = THRESHOLD_MAX_LEVEL_DIFF_WIDE_RANGE 95 96 # Draw a plot. 97 pylab.plot(mults, r_means, 'r.-') 98 pylab.plot(mults, g_means, 'g.-') 99 pylab.plot(mults, b_means, 'b.-') 100 pylab.ylim([0,1]) 101 matplotlib.pyplot.savefig("%s_plot_means.png" % (NAME)) 102 103 # Check for linearity. Verify sample pixel mean values are close to each 104 # other. Also ensure that the images aren't clamped to 0 or 1 105 # (which would make them look like flat lines). 106 for chan in xrange(3): 107 values = [r_means, g_means, b_means][chan] 108 m, b = numpy.polyfit(mults, values, 1).tolist() 109 max_val = max(values) 110 min_val = min(values) 111 max_diff = max_val - min_val 112 print "Channel %d line fit (y = mx+b): m = %f, b = %f" % (chan, m, b) 113 print "Channel max %f min %f diff %f" % (max_val, min_val, max_diff) 114 assert(max_diff < threshold_max_level_diff) 115 assert(b > THRESHOLD_MIN_LEVEL and b < THRESHOLD_MAX_LEVEL) 116 for v in values: 117 assert(v > THRESHOLD_MIN_LEVEL and v < THRESHOLD_MAX_LEVEL) 118 assert(abs(v - b) < THRESHOLD_MAX_OUTLIER_DIFF) 119 120if __name__ == '__main__': 121 main() 122