Home | History | Annotate | Download | only in scene1
      1 # Copyright 2014 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 os.path
     22 
     23 def main():
     24     """Test that raw streams are not croppable.
     25     """
     26     NAME = os.path.basename(__file__).split(".")[0]
     27 
     28     DIFF_THRESH = 0.05
     29 
     30     with its.device.ItsSession() as cam:
     31         props = cam.get_camera_properties()
     32         if (not its.caps.compute_target_exposure(props) or
     33             not its.caps.raw16(props)):
     34             print "Test skipped"
     35             return
     36 
     37         a = props['android.sensor.info.activeArraySize']
     38         ax, ay = a["left"], a["top"]
     39         aw, ah = a["right"] - a["left"], a["bottom"] - a["top"]
     40         print "Active sensor region: (%d,%d %dx%d)" % (ax, ay, aw, ah)
     41 
     42         # Capture without a crop region.
     43         # Use a manual request with a linear tonemap so that the YUV and RAW
     44         # should look the same (once converted by the its.image module).
     45         e, s = its.target.get_target_exposure_combos(cam)["minSensitivity"]
     46         req = its.objects.manual_capture_request(s,e, True)
     47         cap1_raw, cap1_yuv = cam.do_capture(req, cam.CAP_RAW_YUV)
     48 
     49         # Capture with a center crop region.
     50         req["android.scaler.cropRegion"] = {
     51                 "top": ay + ah/3,
     52                 "left": ax + aw/3,
     53                 "right": ax + 2*aw/3,
     54                 "bottom": ay + 2*ah/3}
     55         cap2_raw, cap2_yuv = cam.do_capture(req, cam.CAP_RAW_YUV)
     56 
     57         reported_crops = []
     58         imgs = {}
     59         for s,cap in [("yuv_full",cap1_yuv), ("raw_full",cap1_raw),
     60                 ("yuv_crop",cap2_yuv), ("raw_crop",cap2_raw)]:
     61             img = its.image.convert_capture_to_rgb_image(cap, props=props)
     62             its.image.write_image(img, "%s_%s.jpg" % (NAME, s))
     63             r = cap["metadata"]["android.scaler.cropRegion"]
     64             x, y = a["left"], a["top"]
     65             w, h = a["right"] - a["left"], a["bottom"] - a["top"]
     66             reported_crops.append((x,y,w,h))
     67             imgs[s] = img
     68             print "Crop on %s: (%d,%d %dx%d)" % (s, x,y,w,h)
     69 
     70         # The metadata should report uncropped for all shots (since there is
     71         # at least 1 uncropped stream in each case).
     72         for (x,y,w,h) in reported_crops:
     73             assert((ax,ay,aw,ah) == (x,y,w,h))
     74 
     75         # Also check the image content; 3 of the 4 shots should match.
     76         # Note that all the shots are RGB below; the variable names correspond
     77         # to what was captured.
     78         # Average the images down 4x4 -> 1 prior to comparison to smooth out
     79         # noise.
     80         # Shrink the YUV images an additional 2x2 -> 1 to account for the size
     81         # reduction that the raw images went through in the RGB conversion.
     82         imgs2 = {}
     83         for s,img in imgs.iteritems():
     84             h,w,ch = img.shape
     85             m = 4
     86             if s in ["yuv_full", "yuv_crop"]:
     87                 m = 8
     88             img = img.reshape(h/m,m,w/m,m,3).mean(3).mean(1).reshape(h/m,w/m,3)
     89             imgs2[s] = img
     90             print s, img.shape
     91 
     92         # Strip any border pixels from the raw shots (since the raw images may
     93         # be larger than the YUV images). Assume a symmetric padded border.
     94         xpad = (imgs2["raw_full"].shape[1] - imgs2["yuv_full"].shape[1]) / 2
     95         ypad = (imgs2["raw_full"].shape[0] - imgs2["yuv_full"].shape[0]) / 2
     96         wyuv = imgs2["yuv_full"].shape[1]
     97         hyuv = imgs2["yuv_full"].shape[0]
     98         imgs2["raw_full"]=imgs2["raw_full"][ypad:ypad+hyuv:,xpad:xpad+wyuv:,::]
     99         imgs2["raw_crop"]=imgs2["raw_crop"][ypad:ypad+hyuv:,xpad:xpad+wyuv:,::]
    100         print "Stripping padding before comparison:", xpad, ypad
    101 
    102         for s,img in imgs2.iteritems():
    103             its.image.write_image(img, "%s_comp_%s.jpg" % (NAME, s))
    104 
    105         # Compute image diffs.
    106         diff_yuv = numpy.fabs((imgs2["yuv_full"] - imgs2["yuv_crop"])).mean()
    107         diff_raw = numpy.fabs((imgs2["raw_full"] - imgs2["raw_crop"])).mean()
    108         print "YUV diff (crop vs. non-crop):", diff_yuv
    109         print "RAW diff (crop vs. non-crop):", diff_raw
    110 
    111         assert(diff_yuv > DIFF_THRESH)
    112         assert(diff_raw < DIFF_THRESH)
    113 
    114 if __name__ == '__main__':
    115     main()
    116 
    117