(1)源码解析
此项是scene5当中的测试项,需要使用灰卡将摄像头遮住,用来测试摄像头成像的均匀性。
Shading 测试包括两部分:lens shading(亮度均匀性)和color shading(色彩均一性),测试摄像头成像的亮度/色彩均匀性。
def test_lens_shading_and_color_uniformity(self):
with its_session_utils.ItsSession(
device_id=self.dut.serial,
camera_id=self.camera_id,
hidden_physical_id=self.hidden_physical_id) as cam:
props = cam.get_camera_properties()
props = cam.override_with_hidden_physical_camera_props(props)
log_path = self.log_path
# Check SKIP conditions.
camera_properties_utils.skip_unless(
camera_properties_utils.ae_lock(props) and
camera_properties_utils.awb_lock(props))
if camera_properties_utils.read_3a(props):
# Converge 3A and get the estimates.
//(1)3A转换
sens, exp, awb_gains, awb_xform, _ = cam.do_3a(
get_results=True, do_af=False, lock_ae=True, lock_awb=True)
logging.debug('AE sensitivity: %d, exp: %dms', sens, exp*_NSEC_TO_MSEC)
logging.debug('AWB gains: %s', str(awb_gains))
logging.debug('AWB transform: %s', str(awb_xform))
//(2)获得宽高并拍摄照片
req = capture_request_utils.auto_capture_request()
w, h = capture_request_utils.get_available_output_sizes('yuv', props)[0]
out_surface = {'format': 'yuv', 'width': w, 'height': h}
cap = cam.do_capture(req, out_surface)
logging.debug('Captured YUV %dx%d', w, h)
# Get Y channel
//(3)获得Y通道值
img_y = image_processing_utils.convert_capture_to_planes(cap)[0]
//(4)保存原始照片
image_processing_utils.write_image(img_y, '%s_y_plane.png' %
(os.path.join(log_path, _NAME)), True)
# Convert RGB image & calculate R/G, R/B ratioed images
//(5)获取RGB,RG,BG的值
img_rgb = image_processing_utils.convert_capture_to_rgb_image(cap)
img_r_g, img_b_g = _calc_color_plane_ratios(img_rgb)
# Make copies for images with legends and set legend parameters.
img_lens_shading = numpy.copy(img_rgb)
img_uniformity = numpy.copy(img_rgb)
line_width = max(2, int(max(h, w)/500)) # line width of legend
font_scale = line_width / 7.0 # font scale of the basic font size
font_line_width = int(line_width/2)
text_height = cv2.getTextSize('gf', cv2.FONT_HERSHEY_SIMPLEX,
font_scale, line_width)[0][1]
text_offset = int(text_height*1.5)
# Calculate center block average Y, R/G, and B/G values.
top = int((0.5-_BLOCK_R)*h)
bottom = int((0.5+_BLOCK_R)*h)
left = int((0.5-_BLOCK_R)*w)
right = int((0.5+_BLOCK_R)*w)
center_luma = numpy.mean(img_y[top:bottom, left:right])
center_r_g = numpy.mean(img_r_g[top:bottom, left:right])
center_b_g = numpy.mean(img_b_g[top:bottom, left:right])
# Add center patch legend to lens shading and color uniformity images
cv2.rectangle(img_lens_shading, (left, top), (right, bottom), _CV2_GREEN,
line_width)
_draw_legend(img_lens_shading, [f'Y: {center_luma}:.2f'],
[left+text_offset, bottom-text_offset],
font_scale, text_offset, _CV2_GREEN, font_line_width)
cv2.rectangle(img_uniformity, (left, top), (right, bottom), _CV2_GREEN,
line_width)
_draw_legend(img_uniformity,
[f'R/G: {center_r_g}:.2f', f'B/G: {center_b_g}:.2f'],
[left+text_offset, bottom-text_offset*2],
font_scale, text_offset, _CV2_GREEN, font_line_width)
# Evaluate Y, R/G, and B/G for each block
ls_test_failed = []
cu_test_failed = []
ls_thresh_h = center_luma * (1 + _THRESH_SHADING_HIGH)
dist_max = math.sqrt(pow(w, 2)+pow(h, 2))/2
for position in _BLOCK_POSITION_LIST:
# Create sample block centers' positions in all directions around center
block_centers = _create_block_center_vals(position)
blocks_info = []
max_r_g = 0
min_r_g = float('inf')
max_b_g = 0
min_b_g = float('inf')
for block_center_x, block_center_y in block_centers:
top = int((block_center_y-_BLOCK_R)*h)
bottom = int((block_center_y+_BLOCK_R)*h)
left = int((block_center_x-_BLOCK_R)*w)
right = int((block_center_x+_BLOCK_R)*w)
# Compute block average values and running mins and maxes
block_y = numpy.mean(img_y[top:bottom, left:right])
block_r_g = numpy.mean(img_r_g[top:bottom, left:right])
block_b_g = numpy.mean(img_b_g[top:bottom, left:right])
max_r_g = max(max_r_g, block_r_g)
min_r_g = min(min_r_g, block_r_g)
max_b_g = max(max_b_g, block_b_g)
min_b_g = min(min_b_g, block_b_g)
blocks_info.append({'position': [top, bottom, left, right],
'block_r_g': block_r_g,
'block_b_g': block_b_g})
# Check lens shading
//(6)检查Lens Shading
ls_thresh_l = _calc_block_lens_shading_thresh_l(
block_center_x, block_center_y, center_luma, w, h, dist_max)
if not ls_thresh_h > block_y > ls_thresh_l:
ls_test_failed.append({'position': [top, bottom, left, right],
'val': block_y,
'thresh_l': ls_thresh_l})
legend_color = _CV2_RED
else:
legend_color = _CV2_GREEN
# Overlay legend rectangle on lens shading image.
text_bottom = bottom - text_offset
cv2.rectangle(img_lens_shading, (left, top), (right, bottom),
legend_color, line_width)
_draw_legend(img_lens_shading, ['Y: %.2f' % block_y],
[left+text_offset, text_bottom], font_scale,
text_offset, legend_color, int(line_width/2))
# Check color uniformity
//(7)检查color uniformity
uniformity_r_g = (max_r_g-min_r_g) / center_r_g
uniformity_b_g = (max_b_g-min_b_g) / center_b_g
if (uniformity_r_g > _THRESH_UNIFORMITY or
uniformity_b_g > _THRESH_UNIFORMITY):
cu_test_failed.append({'position': position,
'uniformity_r_g': uniformity_r_g,
'uniformity_b_g': uniformity_b_g})
legend_color = _CV2_RED
else:
legend_color = _CV2_GREEN
# Overlay legend blocks on uniformity image based on PASS/FAIL above.
for block in blocks_info:
top, bottom, left, right = block['position']
cv2.rectangle(img_uniformity, (left, top), (right, bottom),
legend_color, line_width)
texts = ['R/G: %.2f' % block['block_r_g'],
'B/G: %.2f' % block['block_b_g']]
text_bottom = bottom - text_offset * 2
_draw_legend(img_uniformity, texts,
[left+text_offset, text_bottom], font_scale,
text_offset, legend_color, font_line_width)
# Save images
//(8)保存color uniformity和lens shading的result
image_processing_utils.write_image(
img_uniformity, '%s_color_uniformity_result.png' %
(os.path.join(log_path, _NAME)), True)
image_processing_utils.write_image(
img_lens_shading, '%s_lens_shading_result.png' %
(os.path.join(log_path, _NAME)), True)
# Assert results
_assert_results(ls_test_failed, cu_test_failed, center_luma, ls_thresh_h)
判断结果的函数_assert_results,ls_test_failed表示lens shading,cu_test_failed表示color uniformity。
def _assert_results(ls_test_failed, cu_test_failed, center_luma, ls_thresh_h):
"""Check the lens shading and color uniformity results."""
if ls_test_failed:
logging.error('Lens shading test summary')
logging.error('Center block average Y value: %.3f', center_luma)
logging.error('Blocks failed in the lens shading test:')
for block in ls_test_failed:
top, bottom, left, right = block['position']
logging.error('Block[top: %d, bottom: %d, left: %d, right: %d]; '
'avg Y value: %.3f; valid range: %.3f ~ %.3f', top, bottom,
left, right, block['val'], block['thresh_l'], ls_thresh_h)
if cu_test_failed:
logging.error('Color uniformity test summary')
logging.error('Valid color uniformity range: 0 ~ %.2f', _THRESH_UNIFORMITY)
logging.error('Areas that failed the color uniformity test:')
for rd in cu_test_failed:
logging.error('Radius position: %.3f; R/G uniformity: %.3f; B/G '
'uniformity: %.3f', rd['position'], rd['uniformity_r_g'],
rd['uniformity_b_g'])
if ls_test_failed:
raise AssertionError('Lens shading test failed.')
if cu_test_failed:
raise AssertionError('Color uniformity test failed.')
Lens Shading测试原理:拍摄一张大图,把整幅图像等分成若干区域,然后算出这些区域的亮度值,以中间区域为基准,用其他区域的亮度值和中间区域的亮度值相比,得到一个比值,这个比值越接近1越好,最大差异不能超过20%,否则判定失败。
Color Shading测试原理:拍摄一张大图,把整幅图像等分成若干区域,然后算出这些区域中R/G和B/G的值,以中间区域为基准,用其他区域的比值和中间区域的比值相比,得到一个比值,这个比值越接近1越好,最大差异不能超过20%,否则判定失败。
(2)测试失败的示例
我们先看两种测试失败保存的图片。
(A)Lens Shading
(2)Color Shading
可以看到本地失败的原因是由于Lens Shading部分区域差异过大导致的。其中间值为0.618,最大值差异值不应该超过0.618*(1+0.2)= 0.742,标红的区域已经超过此范围,表示亮度均匀性不足。
可从如下Log也能看出来。
10-31 16:09:38.978 DEBUG Opening camera: 1
10-31 16:09:39.228 DEBUG Running vendor 3A on device
10-31 16:09:40.217 DEBUG AE sensitivity: 99, exp: 6ms
10-31 16:09:40.217 DEBUG AWB gains: [1.652344, 1.0, 1.0, 1.650391]
10-31 16:09:40.217 DEBUG AWB transform: [1.457031, -0.423828, -0.033203, -0.142578, 1.101563, 0.041016, 0.003906, -0.527344, 1.523438]
10-31 16:09:40.217 DEBUG Capturing 1 frame with 1 format [yuv]
10-31 16:09:41.373 DEBUG Captured YUV 3264x2448
10-31 16:09:47.230 ERROR Lens shading test summary
10-31 16:09:47.231 ERROR Center block average Y value: 0.618
10-31 16:09:47.231 ERROR Blocks failed in the lens shading test:
10-31 16:09:47.231 ERROR Block[top: 1468, bottom: 1632, left: 0, right: 217]; avg Y value: 0.745; valid range: 0.415 ~ 0.742
10-31 16:09:47.231 ERROR Block[top: 1632, bottom: 1795, left: 0, right: 217]; avg Y value: 0.755; valid range: 0.411 ~ 0.742
10-31 16:09:47.231 ERROR Block[top: 1795, bottom: 1958, left: 0, right: 217]; avg Y value: 0.766; valid range: 0.406 ~ 0.742
10-31 16:09:47.231 ERROR Block[top: 1958, bottom: 2121, left: 0, right: 217]; avg Y value: 0.779; valid range: 0.399 ~ 0.742
10-31 16:09:47.231 ERROR Block[top: 2121, bottom: 2284, left: 0, right: 217]; avg Y value: 0.789; valid range: 0.392 ~ 0.742
10-31 16:09:47.231 ERROR Block[top: 2121, bottom: 2284, left: 3046, right: 3264]; avg Y value: 0.745; valid range: 0.392 ~ 0.742
10-31 16:09:47.231 ERROR Block[top: 2284, bottom: 2448, left: 3046, right: 3264]; avg Y value: 0.759; valid range: 0.383 ~ 0.742
10-31 16:09:47.231 ERROR Block[top: 2284, bottom: 2448, left: 0, right: 217]; avg Y value: 0.797; valid range: 0.383 ~ 0.742
10-31 16:09:47.231 ERROR Block[top: 2284, bottom: 2448, left: 217, right: 435]; avg Y value: 0.778; valid range: 0.399 ~ 0.742
10-31 16:09:47.231 ERROR Block[top: 2284, bottom: 2448, left: 435, right: 652]; avg Y value: 0.755; valid range: 0.413 ~ 0.742
10-31 16:09:47.231 ERROR Block[top: 2284, bottom: 2448, left: 3046, right: 3264]; avg Y value: 0.759; valid range: 0.383 ~ 0.742
10-31 16:09:47.231 ERROR Block[top: 1958, bottom: 2121, left: 217, right: 435]; avg Y value: 0.747; valid range: 0.417 ~ 0.742
10-31 16:09:47.231 ERROR Block[top: 2121, bottom: 2284, left: 217, right: 435]; avg Y value: 0.763; valid range: 0.408 ~ 0.742
10-31 16:09:47.379 ERROR Exception occurred in test_lens_shading_and_color_uniformity.
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/mobly/base_test.py", line 756, in exec_one_test
test_method()
File "/home/its/CODE12.0_R5---CTSV/Verifier/android-cts-verifier-12_r5-linux_x86-arm/android-cts-verifier/CameraITS/tests/scene5/test_lens_shading_and_color_uniformity.py", line 293, in test_lens_shading_and_color_uniformity
_assert_results(ls_test_failed, cu_test_failed, center_luma, ls_thresh_h)
File "/home/its/CODE12.0_R5---CTSV/Verifier/android-cts-verifier-12_r5-linux_x86-arm/android-cts-verifier/CameraITS/tests/scene5/test_lens_shading_and_color_uniformity.py", line 102, in _assert_results
raise AssertionError('Lens shading test failed.')
AssertionError: Lens shading test failed.
综上本题可通过调整tuning效果参数来优化亮度均匀性问题。