gal*_*ica 2 python scipy convex-hull computational-geometry scikit-image
我想知道是否有任何基于 numpy 的工具可以:
一种可能性是使用skimage.morphology.convex_hull_image(),但这仅支持 2D 图像,因此我必须逐个切片(在 z 轴上)调用此函数,这很慢。[编辑:见下面的注释。]
我绝对更喜欢更有效的方式。例如, scipy.spatial.ConvexHull() 可以获取 N 维空间中的点列表,并返回一个凸包对象,该对象似乎不支持查找其凸包图像/体积。
points = np.transpose(np.where(image))
hull = scipy.spatial.ConvexHull(points)
# but now wonder an efficient way of finding the list of 3D voxels that are inside this convex hull
Run Code Online (Sandbox Code Playgroud)
任何想法如何?请注意效率对我的申请很重要。谢谢!
更新:同时,convex_hull_image()已经扩展到支持 ND 图像,但对于中等大小的数据来说速度很慢。下面接受的答案要快得多。
你应该能够做到这一点:
def flood_fill_hull(image):
points = np.transpose(np.where(image))
hull = scipy.spatial.ConvexHull(points)
deln = scipy.spatial.Delaunay(points[hull.vertices])
idx = np.stack(np.indices(image.shape), axis = -1)
out_idx = np.nonzero(deln.find_simplex(idx) + 1)
out_img = np.zeros(image.shape)
out_img[out_idx] = 1
return out_img, hull
Run Code Online (Sandbox Code Playgroud)
它可能不是最快的,但缺少它应该工作的现成功能。
测试:
points = tuple(np.rint(10 * np.random.randn(3,100)).astype(int) + 50)
image = np.zeros((100,)*3)
image[points] = 1
%timeit flood_fill_hull(image)
10 loops, best of 3: 96.8 ms per loop
out, h = flood_fill_hull(image)
plot.imshow(out[50])
Run Code Online (Sandbox Code Playgroud)
无法上传图片,但似乎可以解决问题。