blob: 867635883eeb53bd5ae4e3628aa2bb4e15672c3f [file] [log] [blame]
Ruben Brunk370e2432014-10-14 18:33:23 -07001# 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_ABS_GRAD = 0.001
39
40 mults = []
41 r_means = []
42 g_means = []
43 b_means = []
44
45 with its.device.ItsSession() as cam:
46 props = cam.get_camera_properties()
47 if not its.caps.compute_target_exposure(props):
48 print "Test skipped"
49 return
50
51 e,s = its.target.get_target_exposure_combos(cam)["minSensitivity"]
52 expt_range = props['android.sensor.info.exposureTimeRange']
53 sens_range = props['android.sensor.info.sensitivityRange']
54
55 m = 1
56 while s*m < sens_range[1] and e/m > expt_range[0]:
57 mults.append(m)
58 req = its.objects.manual_capture_request(s*m, e/m)
59 cap = cam.do_capture(req)
60 img = its.image.convert_capture_to_rgb_image(cap)
61 its.image.write_image(img, "%s_mult=%02d.jpg" % (NAME, m))
62 tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
63 rgb_means = its.image.compute_image_means(tile)
64 r_means.append(rgb_means[0])
65 g_means.append(rgb_means[1])
66 b_means.append(rgb_means[2])
67 m = m + 4
68
69 # Draw a plot.
70 pylab.plot(mults, r_means, 'r')
71 pylab.plot(mults, g_means, 'g')
72 pylab.plot(mults, b_means, 'b')
73 pylab.ylim([0,1])
74 matplotlib.pyplot.savefig("%s_plot_means.png" % (NAME))
75
76 # Check for linearity. For each R,G,B channel, fit a line y=mx+b, and
77 # assert that the gradient is close to 0 (flat) and that there are no
78 # crazy outliers. Also ensure that the images aren't clamped to 0 or 1
79 # (which would make them look like flat lines).
80 for chan in xrange(3):
81 values = [r_means, g_means, b_means][chan]
82 m, b = numpy.polyfit(mults, values, 1).tolist()
83 print "Channel %d line fit (y = mx+b): m = %f, b = %f" % (chan, m, b)
84 assert(abs(m) < THRESHOLD_MAX_ABS_GRAD)
85 assert(b > THRESHOLD_MIN_LEVEL and b < THRESHOLD_MAX_LEVEL)
86 for v in values:
87 assert(v > THRESHOLD_MIN_LEVEL and v < THRESHOLD_MAX_LEVEL)
88 assert(abs(v - b) < THRESHOLD_MAX_OUTLIER_DIFF)
89
90if __name__ == '__main__':
91 main()
92