用Python测量不规则形状沿中轴线的厚度

2024-04-25 04:50:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一组不规则形状的二值掩模图像,我要测量沿中轴的最大厚度,也要知道这个最大厚度的确切位置。在

我尝试了scikit图像中的中轴线功能,并成功地得到了我想要的中轴线。在

from skimage.morphology import medial_axis
from scipy import ndimage

index = random.randint(0,pred_imgs.shape[0])
print(index)
mask = pred_imgs[index]
mask=ndimage.binary_fill_holes(mask).astype(int)

skel, distance = medial_axis(mask, return_distance=True)
dist_on_skel = distance * skel

fig, axes = plt.subplots(1, 2, figsize=(8, 8), sharex=True, sharey=True, dpi = 200)
ax = axes.ravel()

ax[0].imshow(mask, cmap=plt.cm.gray, interpolation='nearest')
ax[0].set_title('original')


ax[1].imshow(dist_on_skel, cmap='magma', interpolation='nearest')
ax[1].contour(mask, [0.5], colors='w')
ax[1].set_title('medial_axis')

example

下一步,我想沿着轴测量厚度,就像下图中的红色和蓝色线(这些线垂直于轴)。另外,我想知道最大厚度的位置,也就是红线的起点和终点。在

Imgur

如何在python中实现这一点?非常感谢你!在


Tags: from图像importtrueindexmaskaxdistance