gpt4 book ai didi

python - 如何计算 3d numpy 数组中的凸包图像/体积

转载 作者:太空宇宙 更新时间:2023-11-04 09:54:53 25 4
gpt4 key购买 nike

我想知道是否有任何基于 numpy 的工具可以:

  1. 给定一个二值输入的 3D numpy 图像,找到它的凸包;
  2. 并返回索引列表或此 3D 凸包内的体素(3D 像素)的类似物。

一种可能是使用skimage.morphology.convex_hull_image(),但是 这只支持二维图像,所以我必须逐个切片调用这个函数(在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

有什么想法吗?请注意效率对我的申请很重要。谢谢!


更新:与此同时,convex_hull_image()extended to support ND images , 但对于中等大小的数据来说速度很慢。 accepted answer below速度要快得多。

最佳答案

你应该能够做到这一点:

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

它可能不是最快的,但缺少一个现成的函数它应该可以工作。

测试:

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])

无法上传图片,但似乎可以解决问题。

关于python - 如何计算 3d numpy 数组中的凸包图像/体积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46310603/

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