Ruben Brunk | 370e243 | 2014-10-14 18:33:23 -0700 | [diff] [blame] | 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.caps |
| 17 | import its.device |
| 18 | import its.objects |
| 19 | import its.target |
| 20 | import numpy |
| 21 | import math |
Yin-Chia Yeh | 2970012 | 2016-10-10 10:54:07 -0700 | [diff] [blame] | 22 | from matplotlib import pylab |
Ruben Brunk | 370e243 | 2014-10-14 18:33:23 -0700 | [diff] [blame] | 23 | import os.path |
| 24 | import matplotlib |
| 25 | import matplotlib.pyplot |
| 26 | |
Clemenz Portmann | 231c296 | 2016-12-15 13:55:33 -0800 | [diff] [blame^] | 27 | NAME = os.path.basename(__file__).split('.')[0] |
| 28 | RESIDUAL_THRESHOLD = 0.0003 # approximately each sample is off by 2/255 |
| 29 | # The HAL3.2 spec requires that curves up to 64 control points in length |
| 30 | # must be supported. |
| 31 | L = 64 |
| 32 | LM1 = float(L-1) |
| 33 | |
| 34 | |
Ruben Brunk | 370e243 | 2014-10-14 18:33:23 -0700 | [diff] [blame] | 35 | def main(): |
| 36 | """Test that device processing can be inverted to linear pixels. |
| 37 | |
| 38 | Captures a sequence of shots with the device pointed at a uniform |
| 39 | target. Attempts to invert all the ISP processing to get back to |
| 40 | linear R,G,B pixel data. |
| 41 | """ |
Ruben Brunk | 370e243 | 2014-10-14 18:33:23 -0700 | [diff] [blame] | 42 | gamma_lut = numpy.array( |
Clemenz Portmann | 231c296 | 2016-12-15 13:55:33 -0800 | [diff] [blame^] | 43 | sum([[i/LM1, math.pow(i/LM1, 1/2.2)] for i in xrange(L)], [])) |
Ruben Brunk | 370e243 | 2014-10-14 18:33:23 -0700 | [diff] [blame] | 44 | inv_gamma_lut = numpy.array( |
Clemenz Portmann | 231c296 | 2016-12-15 13:55:33 -0800 | [diff] [blame^] | 45 | sum([[i/LM1, math.pow(i/LM1, 2.2)] for i in xrange(L)], [])) |
Ruben Brunk | 370e243 | 2014-10-14 18:33:23 -0700 | [diff] [blame] | 46 | |
| 47 | with its.device.ItsSession() as cam: |
| 48 | props = cam.get_camera_properties() |
Chien-Yu Chen | 34fa85d | 2014-10-22 16:58:08 -0700 | [diff] [blame] | 49 | its.caps.skip_unless(its.caps.compute_target_exposure(props) and |
| 50 | its.caps.per_frame_control(props)) |
Ruben Brunk | 370e243 | 2014-10-14 18:33:23 -0700 | [diff] [blame] | 51 | |
Clemenz Portmann | 231c296 | 2016-12-15 13:55:33 -0800 | [diff] [blame^] | 52 | e, s = its.target.get_target_exposure_combos(cam)['midSensitivity'] |
Ruben Brunk | 370e243 | 2014-10-14 18:33:23 -0700 | [diff] [blame] | 53 | s /= 2 |
| 54 | sens_range = props['android.sensor.info.sensitivityRange'] |
| 55 | sensitivities = [s*1.0/3.0, s*2.0/3.0, s, s*4.0/3.0, s*5.0/3.0] |
| 56 | sensitivities = [s for s in sensitivities |
Clemenz Portmann | 231c296 | 2016-12-15 13:55:33 -0800 | [diff] [blame^] | 57 | if s > sens_range[0] and s < sens_range[1]] |
Ruben Brunk | 370e243 | 2014-10-14 18:33:23 -0700 | [diff] [blame] | 58 | |
| 59 | req = its.objects.manual_capture_request(0, e) |
Clemenz Portmann | 231c296 | 2016-12-15 13:55:33 -0800 | [diff] [blame^] | 60 | req['android.blackLevel.lock'] = True |
| 61 | req['android.tonemap.mode'] = 0 |
| 62 | req['android.tonemap.curveRed'] = gamma_lut.tolist() |
| 63 | req['android.tonemap.curveGreen'] = gamma_lut.tolist() |
| 64 | req['android.tonemap.curveBlue'] = gamma_lut.tolist() |
Ruben Brunk | 370e243 | 2014-10-14 18:33:23 -0700 | [diff] [blame] | 65 | |
| 66 | r_means = [] |
| 67 | g_means = [] |
| 68 | b_means = [] |
| 69 | |
| 70 | for sens in sensitivities: |
Clemenz Portmann | 231c296 | 2016-12-15 13:55:33 -0800 | [diff] [blame^] | 71 | req['android.sensor.sensitivity'] = sens |
Ruben Brunk | 370e243 | 2014-10-14 18:33:23 -0700 | [diff] [blame] | 72 | cap = cam.do_capture(req) |
| 73 | img = its.image.convert_capture_to_rgb_image(cap) |
| 74 | its.image.write_image( |
Clemenz Portmann | 231c296 | 2016-12-15 13:55:33 -0800 | [diff] [blame^] | 75 | img, '%s_sens=%04d.jpg' % (NAME, sens)) |
Ruben Brunk | 370e243 | 2014-10-14 18:33:23 -0700 | [diff] [blame] | 76 | img = its.image.apply_lut_to_image(img, inv_gamma_lut[1::2] * LM1) |
| 77 | tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1) |
| 78 | rgb_means = its.image.compute_image_means(tile) |
| 79 | r_means.append(rgb_means[0]) |
| 80 | g_means.append(rgb_means[1]) |
| 81 | b_means.append(rgb_means[2]) |
| 82 | |
Clemenz Portmann | 231c296 | 2016-12-15 13:55:33 -0800 | [diff] [blame^] | 83 | pylab.title(NAME) |
| 84 | pylab.plot(sensitivities, r_means, '-ro') |
| 85 | pylab.plot(sensitivities, g_means, '-go') |
| 86 | pylab.plot(sensitivities, b_means, '-bo') |
| 87 | pylab.xlim([sens_range[0], sens_range[1]/2]) |
| 88 | pylab.ylim([0, 1]) |
| 89 | pylab.xlabel('sensitivity(ISO)') |
| 90 | pylab.ylabel('RGB avg [0, 1]') |
| 91 | matplotlib.pyplot.savefig('%s_plot_means.png' % (NAME)) |
Ruben Brunk | 370e243 | 2014-10-14 18:33:23 -0700 | [diff] [blame] | 92 | |
| 93 | # Check that each plot is actually linear. |
| 94 | for means in [r_means, g_means, b_means]: |
Clemenz Portmann | 231c296 | 2016-12-15 13:55:33 -0800 | [diff] [blame^] | 95 | line, residuals, _, _, _ = numpy.polyfit(range(len(sensitivities)), |
| 96 | means, 1, full=True) |
| 97 | print 'Line: m=%f, b=%f, resid=%f'%(line[0], line[1], residuals[0]) |
| 98 | assert residuals[0] < RESIDUAL_THRESHOLD |
Ruben Brunk | 370e243 | 2014-10-14 18:33:23 -0700 | [diff] [blame] | 99 | |
| 100 | if __name__ == '__main__': |
| 101 | main() |
| 102 | |