1# Copyright 2016 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"""Test lens shading and color uniformity with diffuser over camera.""" 15 16 17import logging 18import math 19import os.path 20 21import cv2 22from mobly import test_runner 23import numpy 24 25import its_base_test 26import camera_properties_utils 27import capture_request_utils 28import image_processing_utils 29import its_session_utils 30 31_NAME = os.path.basename(__file__).split('.')[0] 32_NSEC_TO_MSEC = 1E-6 33 34# List to create NUM-1 blocks around the center block for sampling grid in image 35_NUM_RADIUS = 8 36_BLOCK_R = 1/2/(_NUM_RADIUS*2-1) # 'radius' of block (x/2 & y/2 in rel values) 37_BLOCK_POSITION_LIST = numpy.arange(_BLOCK_R, 1/2, _BLOCK_R*2) 38 39# Thresholds for PASS/FAIL 40_THRESH_SHADING_CT = 0.9 # len shading allowance for center 41_THRESH_SHADING_CN = 0.6 # len shading allowance for corner 42_THRESH_SHADING_HIGH = 0.2 # max allowed % for patch to be brighter than center 43_THRESH_UNIFORMITY = 0.2 # uniformity allowance 44 45# cv2 drawing colors 46_CV2_RED = (1, 0, 0) # blocks failed the test 47_CV2_GREEN = (0, 0.7, 0.3) # blocks passed the test 48 49 50def _calc_block_lens_shading_thresh_l( 51 block_center_x, block_center_y, center_luma, img_w, img_h, dist_max): 52 dist_to_img_center = math.sqrt(pow(abs(block_center_x-0.5)*img_w, 2) + 53 pow(abs(block_center_y-0.5)*img_h, 2)) 54 return ((_THRESH_SHADING_CT - _THRESH_SHADING_CN) * 55 (1 - dist_to_img_center/dist_max) + _THRESH_SHADING_CN) * center_luma 56 57 58def _calc_color_plane_ratios(img_rgb): 59 """Calculate R/G and B/G ratios.""" 60 img_g_plus_delta = img_rgb[:, :, 1] + 0.001 # in case G channel has 0 value. 61 img_r_g = img_rgb[:, :, 0] / img_g_plus_delta 62 img_b_g = img_rgb[:, :, 2] / img_g_plus_delta 63 return img_r_g, img_b_g 64 65 66def _create_block_center_vals(block_center): 67 """Create lists of x and y values for sub-block centers.""" 68 num_sample = int(((1-block_center*2)/_BLOCK_R/2 + 1).item()) 69 center_xs = numpy.concatenate( 70 (numpy.arange(block_center, 1-block_center+_BLOCK_R, _BLOCK_R*2), 71 block_center*numpy.ones((num_sample-1)), 72 (1-block_center)*numpy.ones((num_sample-1)), 73 numpy.arange(block_center, 1-block_center+_BLOCK_R, _BLOCK_R*2))) 74 center_ys = numpy.concatenate( 75 (block_center*numpy.ones(num_sample+1), 76 numpy.arange(block_center+_BLOCK_R*2, 1-block_center, _BLOCK_R*2), 77 numpy.arange(block_center+_BLOCK_R*2, 1-block_center, _BLOCK_R*2), 78 (1-block_center)*numpy.ones(num_sample+1))) 79 return zip(center_xs, center_ys) 80 81 82def _assert_results(ls_test_failed, cu_test_failed, center_luma, ls_thresh_h): 83 """Check the lens shading and color uniformity results.""" 84 if ls_test_failed: 85 logging.error('Lens shading test summary') 86 logging.error('Center block average Y value: %.3f', center_luma) 87 logging.error('Blocks failed in the lens shading test:') 88 for block in ls_test_failed: 89 top, bottom, left, right = block['position'] 90 logging.error('Block[top: %d, bottom: %d, left: %d, right: %d]; ' 91 'avg Y value: %.3f; valid range: %.3f ~ %.3f', top, bottom, 92 left, right, block['val'], block['thresh_l'], ls_thresh_h) 93 if cu_test_failed: 94 logging.error('Color uniformity test summary') 95 logging.error('Valid color uniformity range: 0 ~ %.2f', _THRESH_UNIFORMITY) 96 logging.error('Areas that failed the color uniformity test:') 97 for rd in cu_test_failed: 98 logging.error('Radius position: %.3f; R/G uniformity: %.3f; B/G ' 99 'uniformity: %.3f', rd['position'], rd['uniformity_r_g'], 100 rd['uniformity_b_g']) 101 if ls_test_failed: 102 raise AssertionError('Lens shading test failed.') 103 if cu_test_failed: 104 raise AssertionError('Color uniformity test failed.') 105 106 107def _draw_legend(img, texts, text_org, font_scale, text_offset, color, 108 line_width): 109 """Draw legend on an image. 110 111 Args: 112 img: Numpy float image array in RGB, with pixel values in [0,1]. 113 texts: List of legends. Each element in the list is a line of legend. 114 text_org: Tuple of the bottom left corner of the text position in 115 pixels, horizontal and vertical. 116 font_scale: Float number. Font scale of the basic font size. 117 text_offset: Text line width in pixels. 118 color: Text color in rgb value. 119 line_width: Text line width in pixels. 120 """ 121 for text in texts: 122 cv2.putText(img, text, (text_org[0], text_org[1]), 123 cv2.FONT_HERSHEY_SIMPLEX, font_scale, color, line_width) 124 text_org[1] += text_offset 125 126 127class LensShadingAndColorUniformityTest(its_base_test.ItsBaseTest): 128 """Test lens shading correction and uniform scene is evenly distributed. 129 130 Test runs with a diffuser (manually) placed in front of the camera. 131 Performs this test on a YUV frame with auto 3A. Lens shading is evaluated 132 based on the Y channel. Measure the average Y value for each sample block 133 specified, and then determine PASS/FAIL by comparing with the center Y value. 134 135 Evaluates the color uniformity in R/G and B/G color space. At specified 136 radius of the image, the variance of R/G and B/G values need to be less than 137 a threshold in order to pass the test. 138 """ 139 140 def test_lens_shading_and_color_uniformity(self): 141 142 with its_session_utils.ItsSession( 143 device_id=self.dut.serial, 144 camera_id=self.camera_id, 145 hidden_physical_id=self.hidden_physical_id) as cam: 146 props = cam.get_camera_properties() 147 props = cam.override_with_hidden_physical_camera_props(props) 148 debug_mode = self.debug_mode 149 name_with_log_path = os.path.join(self.log_path, _NAME) 150 151 # Check SKIP conditions. 152 camera_properties_utils.skip_unless( 153 camera_properties_utils.ae_lock(props) and 154 camera_properties_utils.awb_lock(props)) 155 156 if camera_properties_utils.read_3a(props): 157 # Converge 3A and get the estimates. 158 sens, exp, awb_gains, awb_xform, _ = cam.do_3a( 159 get_results=True, do_af=False, lock_ae=True, lock_awb=True) 160 logging.debug('AE sensitivity: %d, exp: %dms', sens, exp*_NSEC_TO_MSEC) 161 logging.debug('AWB gains: %s', str(awb_gains)) 162 logging.debug('AWB transform: %s', str(awb_xform)) 163 164 req = capture_request_utils.auto_capture_request() 165 w, h = capture_request_utils.get_available_output_sizes('yuv', props)[0] 166 out_surface = {'format': 'yuv', 'width': w, 'height': h} 167 if debug_mode: 168 out_surfaces = [{'format': 'raw'}, out_surface] 169 cap_raw, cap = cam.do_capture(req, out_surfaces) 170 img_raw = image_processing_utils.convert_capture_to_rgb_image( 171 cap_raw, props=props) 172 image_processing_utils.write_image( 173 img_raw, f'{name_with_log_path}_raw.png', True) 174 logging.debug('Captured RAW %dx%d', img_raw.shape[1], img_raw.shape[0]) 175 else: 176 cap = cam.do_capture(req, out_surface) 177 logging.debug('Captured YUV %dx%d', w, h) 178 # Get Y channel 179 img_y = image_processing_utils.convert_capture_to_planes(cap)[0] 180 image_processing_utils.write_image( 181 img_y, f'{name_with_log_path}_y_plane.png', True) 182 # Convert RGB image & calculate R/G, R/B ratioed images 183 img_rgb = image_processing_utils.convert_capture_to_rgb_image(cap) 184 img_r_g, img_b_g = _calc_color_plane_ratios(img_rgb) 185 186 # Make copies for images with legends and set legend parameters. 187 img_lens_shading = numpy.copy(img_rgb) 188 img_uniformity = numpy.copy(img_rgb) 189 line_width = max(2, int(max(h, w)/500)) # line width of legend 190 font_scale = line_width / 7.0 # font scale of the basic font size 191 font_line_width = int(line_width/2) 192 text_height = cv2.getTextSize('gf', cv2.FONT_HERSHEY_SIMPLEX, 193 font_scale, line_width)[0][1] 194 text_offset = int(text_height*1.5) 195 196 # Calculate center block average Y, R/G, and B/G values. 197 top = int((0.5-_BLOCK_R)*h) 198 bottom = int((0.5+_BLOCK_R)*h) 199 left = int((0.5-_BLOCK_R)*w) 200 right = int((0.5+_BLOCK_R)*w) 201 center_luma = numpy.mean(img_y[top:bottom, left:right]) 202 center_r_g = numpy.mean(img_r_g[top:bottom, left:right]) 203 center_b_g = numpy.mean(img_b_g[top:bottom, left:right]) 204 205 # Add center patch legend to lens shading and color uniformity images 206 cv2.rectangle(img_lens_shading, (left, top), (right, bottom), _CV2_GREEN, 207 line_width) 208 _draw_legend(img_lens_shading, [f'Y: {center_luma}:.2f'], 209 [left+text_offset, bottom-text_offset], 210 font_scale, text_offset, _CV2_GREEN, font_line_width) 211 212 cv2.rectangle(img_uniformity, (left, top), (right, bottom), _CV2_GREEN, 213 line_width) 214 _draw_legend(img_uniformity, 215 [f'R/G: {center_r_g}:.2f', f'B/G: {center_b_g}:.2f'], 216 [left+text_offset, bottom-text_offset*2], 217 font_scale, text_offset, _CV2_GREEN, font_line_width) 218 219 # Evaluate Y, R/G, and B/G for each block 220 ls_test_failed = [] 221 cu_test_failed = [] 222 ls_thresh_h = center_luma * (1 + _THRESH_SHADING_HIGH) 223 dist_max = math.sqrt(pow(w, 2)+pow(h, 2))/2 224 for position in _BLOCK_POSITION_LIST: 225 # Create sample block centers' positions in all directions around center 226 block_centers = _create_block_center_vals(position) 227 228 blocks_info = [] 229 max_r_g = 0 230 min_r_g = float('inf') 231 max_b_g = 0 232 min_b_g = float('inf') 233 for block_center_x, block_center_y in block_centers: 234 top = int((block_center_y-_BLOCK_R)*h) 235 bottom = int((block_center_y+_BLOCK_R)*h) 236 left = int((block_center_x-_BLOCK_R)*w) 237 right = int((block_center_x+_BLOCK_R)*w) 238 239 # Compute block average values and running mins and maxes 240 block_y = numpy.mean(img_y[top:bottom, left:right]) 241 block_r_g = numpy.mean(img_r_g[top:bottom, left:right]) 242 block_b_g = numpy.mean(img_b_g[top:bottom, left:right]) 243 max_r_g = max(max_r_g, block_r_g) 244 min_r_g = min(min_r_g, block_r_g) 245 max_b_g = max(max_b_g, block_b_g) 246 min_b_g = min(min_b_g, block_b_g) 247 blocks_info.append({'position': [top, bottom, left, right], 248 'block_r_g': block_r_g, 249 'block_b_g': block_b_g}) 250 # Check lens shading 251 ls_thresh_l = _calc_block_lens_shading_thresh_l( 252 block_center_x, block_center_y, center_luma, w, h, dist_max) 253 254 if not ls_thresh_h > block_y > ls_thresh_l: 255 ls_test_failed.append({'position': [top, bottom, left, right], 256 'val': block_y, 257 'thresh_l': ls_thresh_l}) 258 legend_color = _CV2_RED 259 else: 260 legend_color = _CV2_GREEN 261 262 # Overlay legend rectangle on lens shading image. 263 text_bottom = bottom - text_offset 264 cv2.rectangle(img_lens_shading, (left, top), (right, bottom), 265 legend_color, line_width) 266 _draw_legend(img_lens_shading, [f'Y: {block_y:.2f}'], 267 [left+text_offset, text_bottom], font_scale, 268 text_offset, legend_color, int(line_width/2)) 269 270 # Check color uniformity 271 uniformity_r_g = (max_r_g-min_r_g) / center_r_g 272 uniformity_b_g = (max_b_g-min_b_g) / center_b_g 273 if (uniformity_r_g > _THRESH_UNIFORMITY or 274 uniformity_b_g > _THRESH_UNIFORMITY): 275 cu_test_failed.append({'position': position, 276 'uniformity_r_g': uniformity_r_g, 277 'uniformity_b_g': uniformity_b_g}) 278 legend_color = _CV2_RED 279 else: 280 legend_color = _CV2_GREEN 281 282 # Overlay legend blocks on uniformity image based on PASS/FAIL above. 283 for block in blocks_info: 284 top, bottom, left, right = block['position'] 285 cv2.rectangle(img_uniformity, (left, top), (right, bottom), 286 legend_color, line_width) 287 texts = [f"R/G: {block['block_r_g']:.2f}", 288 f"B/G: {block['block_b_g']:.2f}"] 289 text_bottom = bottom - text_offset * 2 290 _draw_legend(img_uniformity, texts, 291 [left+text_offset, text_bottom], font_scale, 292 text_offset, legend_color, font_line_width) 293 294 # Save images 295 image_processing_utils.write_image( 296 img_uniformity, f'{name_with_log_path}_color_uniformity_result.png', 297 True) 298 image_processing_utils.write_image( 299 img_lens_shading, f'{name_with_log_path}_lens_shading_result.png', 300 True) 301 302 # Assert results 303 _assert_results(ls_test_failed, cu_test_failed, center_luma, ls_thresh_h) 304 305 306if __name__ == '__main__': 307 test_runner.main() 308