获取最大轮廓对应的掩码图
示例代码
def get_max_contour_mask(mask):
# Find connected components
# mask = cv2.inRange(mask, 100, 255)
num_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(
mask, connectivity=8
)
# Initialize variables to keep track of the largest contour
max_area = 0
max_label = 0
# Loop over each component to find the largest one
for i in range(1, num_labels):
area = stats[i, cv2.CC_STAT_AREA]
if area > max_area:
max_area = area
max_label = i
# Create a mask for the largest contour
mask = np.zeros_like(labels, dtype=np.uint8)
mask[labels == max_label] = 255
return mask
标签:area,max,labels,mask,获取,cv2,掩码,轮廓,contour
From: https://blog.csdn.net/familytaijun/article/details/144118224