
def find_bbox(mask):
_, labels, stats, centroids = cv2.connectedComponentsWithStats(mask.astype(np.uint8))
stats = stats[stats[:,4].argsort()]
return stats[:-1]
mask = label[4]
ax = plt.axes()
plt.imshow(mask,cmap='bone')
bboxs = find_bbox(mask)
for j in bboxs:
rect = patches.Rectangle((j[0],j[1]),j[2],j[3],linewidth=1,edgecolor='r',facecolor='none')
ax.add_patch(rect)
plt.show()
本文介绍了一种基于连通组件分析的目标检测方法,通过OpenCV库实现图像中多个目标的定位与绘制边界框,展示了如何使用Python进行图像处理和目标识别。
1659





