def profile_score(contour, binary):
"""
Calculate a score based on the "profile" of the target, basically how closely its geometry matches with the expected
geometry of the goal
:param contour:
:param binary:
:return:
"""
bounding = cv2.boundingRect(contour)
pixels = np.zeros((binary.shape[0], binary.shape[1]))
cv2.drawContours(pixels, [contour], -1, 255, -1)
col_averages = np.mean(pixels, axis=0)[bounding[0]:bounding[0] + bounding[2]]
row_averages = np.mean(pixels, axis=1)[bounding[1]:bounding[1] + bounding[3]]
# normalize to between 0 and 1
col_averages *= 1.0 / col_averages.max()
row_averages *= 1.0 / row_averages.max()
col_diff = np.subtract(col_averages, col_profile(col_averages.shape[0], bounding[2]))
row_diff = np.subtract(row_averages, row_profile(row_averages.shape[0], bounding[3]))
# average difference should be close to 0
avg_diff = np.mean([np.mean(col_diff), np.mean(row_diff)])
return 100 - (avg_diff * 50)
评论列表
文章目录