gpt4 book ai didi

python - 我如何使用 python 库在骨架图像中找到循环?

转载 作者:太空狗 更新时间:2023-10-29 22:19:38 26 4
gpt4 key购买 nike

我有很多像这样的骨架化图像:

enter image description here enter image description here

我如何检测循环,骨架中的循环?是否有执行此操作的“特殊”功能,或者我应该将其实现为图形?

如果只有图形选项,python图形库NetworkX可以帮助我吗?

最佳答案

您可以利用骨架的拓扑结构。一个循环不会有空洞,所以我们可以使用 scipy.ndimage 找到任何空洞并进行比较。这不是最快的方法,但它非常容易编码。

import scipy.misc, scipy.ndimage

# Read the image
img = scipy.misc.imread("Skel.png")

# Retain only the skeleton
img[img!=255] = 0
img = img.astype(bool)

# Fill the holes
img2 = scipy.ndimage.binary_fill_holes(img)

# Compare the two, an image without cycles will have no holes
print "Cycles in image: ", ~(img == img2).all()

# As a test break the cycles
img3 = img.copy()
img3[0:200, 0:200] = 0
img4 = scipy.ndimage.binary_fill_holes(img3)

# Compare the two, an image without cycles will have no holes
print "Cycles in image: ", ~(img3 == img4).all()

我以您的“B”图片为例。前两个图像是原始图像和检测循环的填充版本。在第二个版本中,我打破了循环,没有填充任何东西,因此两个图像是相同的。

enter image description here

关于python - 我如何使用 python 库在骨架图像中找到循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15914684/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com