blob: 26c398ddacb8387f047ce00d663cb7664c81b485 [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
Yin-Chia Yeh29df8142015-04-23 14:59:44 -070038 THRESHOLD_MAX_LEVEL_DIFF = 0.025
Yin-Chia Yeh15653b72015-08-27 19:53:03 -070039 THRESHOLD_MAX_LEVEL_DIFF_WIDE_RANGE = 0.05
Ruben Brunk370e2432014-10-14 18:33:23 -070040
41 mults = []
42 r_means = []
43 g_means = []
44 b_means = []
Yin-Chia Yeh15653b72015-08-27 19:53:03 -070045 threshold_max_level_diff = THRESHOLD_MAX_LEVEL_DIFF
Ruben Brunk370e2432014-10-14 18:33:23 -070046
47 with its.device.ItsSession() as cam:
48 props = cam.get_camera_properties()
Chien-Yu Chen34fa85d2014-10-22 16:58:08 -070049 its.caps.skip_unless(its.caps.compute_target_exposure(props) and
50 its.caps.per_frame_control(props))
Ruben Brunk370e2432014-10-14 18:33:23 -070051
52 e,s = its.target.get_target_exposure_combos(cam)["minSensitivity"]
53 expt_range = props['android.sensor.info.exposureTimeRange']
54 sens_range = props['android.sensor.info.sensitivityRange']
55
56 m = 1
57 while s*m < sens_range[1] and e/m > expt_range[0]:
58 mults.append(m)
59 req = its.objects.manual_capture_request(s*m, e/m)
60 cap = cam.do_capture(req)
61 img = its.image.convert_capture_to_rgb_image(cap)
Yin-Chia Yeh15653b72015-08-27 19:53:03 -070062 its.image.write_image(img, "%s_mult=%3.2f.jpg" % (NAME, m))
Ruben Brunk370e2432014-10-14 18:33:23 -070063 tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
64 rgb_means = its.image.compute_image_means(tile)
65 r_means.append(rgb_means[0])
66 g_means.append(rgb_means[1])
67 b_means.append(rgb_means[2])
Yin-Chia Yeh15653b72015-08-27 19:53:03 -070068 # Test 3 steps per 2x gain
69 m = m * pow(2, 1.0 / 3)
70
71 # Allow more threshold for devices with wider exposure range
72 if m >= 64.0:
73 threshold_max_level_diff = THRESHOLD_MAX_LEVEL_DIFF_WIDE_RANGE
Ruben Brunk370e2432014-10-14 18:33:23 -070074
75 # Draw a plot.
76 pylab.plot(mults, r_means, 'r')
77 pylab.plot(mults, g_means, 'g')
78 pylab.plot(mults, b_means, 'b')
79 pylab.ylim([0,1])
80 matplotlib.pyplot.savefig("%s_plot_means.png" % (NAME))
81
Yin-Chia Yeh29df8142015-04-23 14:59:44 -070082 # Check for linearity. Verify sample pixel mean values are close to each
83 # other. Also ensure that the images aren't clamped to 0 or 1
Ruben Brunk370e2432014-10-14 18:33:23 -070084 # (which would make them look like flat lines).
85 for chan in xrange(3):
86 values = [r_means, g_means, b_means][chan]
87 m, b = numpy.polyfit(mults, values, 1).tolist()
Yin-Chia Yeh29df8142015-04-23 14:59:44 -070088 max_val = max(values)
89 min_val = min(values)
90 max_diff = max_val - min_val
Ruben Brunk370e2432014-10-14 18:33:23 -070091 print "Channel %d line fit (y = mx+b): m = %f, b = %f" % (chan, m, b)
Yin-Chia Yeh29df8142015-04-23 14:59:44 -070092 print "Channel max %f min %f diff %f" % (max_val, min_val, max_diff)
Yin-Chia Yeh15653b72015-08-27 19:53:03 -070093 assert(max_diff < threshold_max_level_diff)
Ruben Brunk370e2432014-10-14 18:33:23 -070094 assert(b > THRESHOLD_MIN_LEVEL and b < THRESHOLD_MAX_LEVEL)
95 for v in values:
96 assert(v > THRESHOLD_MIN_LEVEL and v < THRESHOLD_MAX_LEVEL)
97 assert(abs(v - b) < THRESHOLD_MAX_OUTLIER_DIFF)
98
99if __name__ == '__main__':
100 main()
101