如何使用python库在骨架图像中找到循环? [英] How can i find cycles in a skeleton image with python libraries?
问题描述
我有很多这样的镂空图像:
I have many skeletonized images like this:
我如何检测一个循环,一个骨架中的循环?
是否有特殊函数执行此操作或应将其实现为图形?
How can i detect a cycle, a loop in the skeleton? Are there "special" functions that do this or should I implement it as a graph?
如果只有图形选项,可以使用python图形库NetworkX可以帮助我?
In case there is only the graph option, can the python graph library NetworkX can help me?
推荐答案
您可以利用骨架的拓扑结构。一个循环没有漏洞,所以我们可以使用 scipy.ndimage
找到任何漏洞并进行比较。这不是最快的方法,但编码非常简单。
You can exploit the topology of the skeleton. A cycle will have no holes, so we can use scipy.ndimage
to find any holes and compare. This isn't the fastest method, but it's extremely easy to code.
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图片作为例子。前两个图像是原始图像和填充版本,它检测周期。在第二个版本中,我打破了循环并且没有任何内容被填充,因此两个图像是相同的。
I've used your "B" picture as an example. The first two images are the original and the filled version which detects a cycle. In the second version, I've broken the cycle and nothing gets filled, thus the two images are the same.
这篇关于如何使用python库在骨架图像中找到循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!