提问人:Eloi Schlegel 提问时间:5/7/2021 最后编辑:Eloi Schlegel 更新时间:5/14/2021 访问量:485
坐标为 skeleton_to_csgraph 的错误 (python)
Error with coordinates of skeleton_to_csgraph (python)
问:
我正在使用 skeleton-to-csgraph() 来分析我获得的骨架,这个函数应该会发回一些数据,其中,矩阵 [(N+1) x 2] 中骨架所有点的所有坐标,点数为 N。但是,有一种情况是,此函数无缘无故地给了我一个带有一些错误的坐标矩阵。 有人知道错误可能来自哪里吗?
有关函数和软件包的更多信息,请访问:https://jni.github.io/skan/
由 skeleton-to-csgraph 函数给出的坐标矩阵的线。在第 326 行处出现无法解释的错误,它应该是 int 而不是浮点值,因为它是一个像素索引
使用 Grid,则为 int32 [200,100] 的 NDarray,值为 1 或 0。此链接中的文件: https://drive.google.com/drive/folders/12xwZBv3RKQ4Q802jXPAhtYZXHrsMytMl?usp=sharing
skeleton, distance = medial_axis(grid, return_distance=True)
graph, coordinates, degrees = skeleton_to_csgraph(skeleton)
就像在图片上一样,我没有得到一个坐标的整数值
答:
按照以下步骤操作: 注意:我在 jupyter notebook 上运行了这个,所以你会看到!安装开始时
!pip install git+https://github.com/jni/skan # install latest version github
加载数据并对其进行处理。
import numpy as np
from skimage.morphology import medial_axis
from skan import skeleton_to_csgraph
from skan import _testdata
from skan import draw
from matplotlib import pyplot as plt
grid = np.load('grid.npy') # this is the file you shared through google drive
skeleton, distance = medial_axis(grid, return_distance=True)
graph, coordinates, degrees = skeleton_to_csgraph(skeleton)
fig, ax = plt.subplots()
draw.overlay_skeleton_networkx(graph,coordinates, image=skeleton,axis=ax)
fig, ax = plt.subplots()
draw.overlay_skeleton_2d(grid, skeleton, dilate=1, axes=ax);
评论
coordinates[325]=[115,11]
在某些特定情况下,它接缝了功能存在问题。但为了克服此错误,以下代码提供了您应该具有的结果:skeleton-to-csgraph()
a = np.where(skeleton == 1 )
b = np.array([a[0], a[1]]).transpose()
coordinates = np.vstack(([0,0],b))
使用 skeleton,带有骨架图像和坐标的 numpy 数组,您应该获得的结果包含骨架点的所有坐标。skeleton-to-csgraph()
您遇到的问题是因为 skan 需要将交汇点像素折叠到它们的质心中才能生成连贯的图形。有关详细信息,请参阅 https://doi.org/10.7717/peerj.4312/supp-2。很抱歉,这没有更好的记录。我提出了一个问题来改进它:
https://github.com/jni/skan/issues/121
这个问题也与此有关:coordinates 数组包含所有节点坐标,但也包含一些垃圾坐标:
https://github.com/jni/skan/issues/108
欢迎在 github 上继续讨论!🙏
评论
draw.overlay_skeleton_networkx(graph,coordinates, image=skeleton,axis=ax)