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 numpy 20 import os.path 21 import matplotlib 22 import matplotlib.pyplot 23 24 def main(): 25 """Test that a constant exposure is seen as ISO and exposure time vary. 26 27 Take a series of shots that have ISO and exposure time chosen to balance 28 each other; result should be the same brightness, but over the sequence 29 the images should get noisier. 30 """ 31 NAME = os.path.basename(__file__).split(".")[0] 32 33 THRESHOLD_MAX_OUTLIER_DIFF = 0.1 34 THRESHOLD_MIN_LEVEL = 0.1 35 THRESHOLD_MAX_LEVEL = 0.9 36 THRESHOLD_MAX_ABS_GRAD = 0.001 37 38 mults = range(1, 100, 9) 39 r_means = [] 40 g_means = [] 41 b_means = [] 42 43 with its.device.ItsSession() as cam: 44 for m in mults: 45 req = its.objects.manual_capture_request(100*m, 40.0/m) 46 fname, w, h, md_obj = cam.do_capture(req) 47 img = its.image.load_yuv420_to_rgb_image(fname, w, h) 48 its.image.write_image(img, "%s_mult=%02d.jpg" % (NAME, m)) 49 tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1) 50 rgb_means = its.image.compute_image_means(tile) 51 r_means.append(rgb_means[0]) 52 g_means.append(rgb_means[1]) 53 b_means.append(rgb_means[2]) 54 55 # Draw a plot. 56 pylab.plot(mults, r_means, 'r') 57 pylab.plot(mults, g_means, 'g') 58 pylab.plot(mults, b_means, 'b') 59 pylab.ylim([0,1]) 60 matplotlib.pyplot.savefig("%s_plot_means.png" % (NAME)) 61 62 63 # Check for linearity. For each R,G,B channel, fit a line y=mx+b, and 64 # assert that the gradient is close to 0 (flat) and that there are no 65 # crazy outliers. Also ensure that the images aren't clamped to 0 or 1 66 # (which would make them look like flat lines). 67 for chan in xrange(3): 68 values = [r_means, g_means, b_means][chan] 69 m, b = numpy.polyfit(mults, values, 1).tolist() 70 print "Channel %d line fit (y = mx+b): m = %f, b = %f" % (chan, m, b) 71 assert(abs(m) < THRESHOLD_MAX_ABS_GRAD) 72 assert(b > THRESHOLD_MIN_LEVEL and b < THRESHOLD_MAX_LEVEL) 73 for v in values: 74 assert(v > THRESHOLD_MIN_LEVEL and v < THRESHOLD_MAX_LEVEL) 75 assert(abs(v - b) < THRESHOLD_MAX_OUTLIER_DIFF) 76 77 if __name__ == '__main__': 78 main() 79 80