如何找到 bw 连接组件的端点?

How to find end points of bw connected components?

我正在使用以下代码来标记二进制图像中的连接组件:

def connected_component(img): 
output = cv2.connectedComponentsWithStats(img, 8)
num_labels = output[0]
labels = output[1]
return labels, num_labels

我主要是这样调用的:

labels, num_labels = connected_component(seg_f)

我希望找到每个连通分量的端点(假设连通分量是线)。 我尝试按如下方式进行操作,但输出错误:

cropped_max_y_1=[]
cropped_min_y_1=[]
cropped_max_x_1=[]
cropped_min_x_1=[]
seg_f, _ = ndimage.label(seg_f)
num_instances = np.max(np.max(seg_f))
for instance_id in range(1,num_instances+1):
im_inst = seg_f == instance_id
points = np.nonzero(im_inst)
cropped_min_x_1.append(np.min(points[0]))
cropped_min_y_1.append(np.min(points[1]))
cropped_max_x_1.append(np.max(points[0])+1)
cropped_max_y_1.append(np.max(points[1])+1)

请提出更改建议或替代方法。

这是一个示例输入:

预期的输出是关节的坐标

端点是唯一具有单个邻居的端点。您可以轻松检查每个像素的配置。

如果您还需要检测 "quasi adjoining" 个分叉,您可以在端点周围搜索其他像素,但忽略相同连通分量的像素。