import cv2
# 连通组件标记
num_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(binary, connectivity=8)
# 提取连续的白色区域
white_regions = []
for i in range(1, num_labels):
# 过滤掉面积小于阈值的区域
if stats[i, cv2.CC_STAT_AREA] >= min_area:
# 获取区域的边界框坐标
x, y, w, h = stats[i, cv2.CC_STAT_LEFT], stats[i, cv2.CC_STAT_TOP], stats[i, cv2.CC_STAT_WIDTH], stats[i, cv2.CC_STAT_HEIGHT]
white_regions.append((x, y, w, h))
# 在原图上绘制提取的连续白色区域
image_with_regions = image.copy()
for region in white_regions:
x, y, w, h = region
cv2.rectangle(image_with_regions, (x, y), (x+w, y+h), (0, 255, 0), 2)
# 保存带有连续白色区域的图像
cv2.imwrite(output_path, image_with_regions)