def solidity(contour, hull=None, area_min=0):
"""
Calculate the solidity of a contour, which is the ratio of its area to the
area of its convex hull.
:param contour: the target contour
:param hull: the convex hull of the contour, see cv2.convexHull(contour)
:param area_min: a contour with an area below the minimum has a solidity of 0
"""
if hull is None:
hull = cv2.convexHull(contour)
try:
contour_area = cv2.contourArea(contour)
hull_area = cv2.contourArea(hull)
return contour_area / hull_area if hull_area > area_min else 0
except ArithmeticError:
return 0
评论列表
文章目录