如何使用Python识别已提取中轴(medial axis)的端点与关节点
中轴端点与关节点识别实现方法
核心判断逻辑基于骨架点的8邻域连通像素计数:
- 端点:8邻域内仅存在1个其他骨架像素
- 关节点(分叉点):8邻域内存在≥3个其他骨架像素
- 普通骨架点:8邻域内存在2个其他骨架像素
以下是可直接运行的实现代码,可直接承接你已有的骨架提取逻辑:
import numpy as np from PIL import Image from skimage.morphology import skeletonize # 你已有的骨架提取代码 image = Image.open('path/to/img.png') image = (np.array(image) / 255).astype(np.uint8) medial = skeletonize(image) # 定义8邻域的坐标偏移量 neighbors_8 = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)] endpoints = [] junctions = [] h, w = medial.shape # 遍历所有骨架点做分类 for y in range(h): for x in range(w): if medial[y, x] == 1: neighbor_cnt = 0 for dy, dx in neighbors_8: ny, nx = y + dy, x + dx # 边界合法性判断 if 0 <= ny < h and 0 <= nx < w and medial[ny, nx] == 1: neighbor_cnt += 1 if neighbor_cnt == 1: endpoints.append((x, y)) elif neighbor_cnt >= 3: junctions.append((x, y)) print(f"端点数量:{len(endpoints)},坐标:{endpoints}") print(f"关节点数量:{len(junctions)},坐标:{junctions}")
可选优化:如果提取的骨架存在细小毛刺、噪点,可先调用
skimage.morphology.opening对骨架做一次小核的形态学开运算,过滤干扰后再做邻域计数,能大幅降低误判概率。
内容的提问来源于stack exchange,提问作者Qimin Chen
相关产品推荐
相关产品推荐

