【发布时间】:2015-10-26 10:07:12
【问题描述】:
背景
对于我正在研究的算法,我目前使用 3D 球体作为二进制掩码,NxNxN 数组在半径为 N//2 的球体中具有体素为True。进一步的处理对每个体素集进行计算True。
事实证明,由于 N 变大 = O(N^3),这对我的特定任务来说是计算密集型的,所以我现在想将我的二进制掩码减少为从半径内的阵列中心辐射的线的子样本。
目标
我想要图像中灰色线条的 3D 二进制蒙版。
为了对体素的数量进行一些控制,我将有一个参数(比如l)来调节每个二维圆中采样的线数,也许还有第二个参数(k?)用于z 轴旋转次数。
我尝试了什么
我正在使用 numpy 和 scipy,我认为我可以使用 scipy.ndimage.interpolation.rotate 方法在平面上旋转一条线,然后使用该完整的 2D 蒙版围绕 z 轴旋转。 这被证明是困难的,因为 interpolate 使用了一些关于样条曲线的深层魔法,它在旋转时丢弃了我的 True 值。
我想我可以通过遵循一些线方程以数学方式计算哪个体素应该设置为 True,但我找不到它们。
知道怎么去吗?
更新:解决方案!
感谢 jkalden 帮助我思考这个问题并提供了代码示例,我有这个:
rmax 是球体半径,n_theta 和 n_phi 是要使用的极线和方位线的数量。
out_mask = np.zeros((rmax*2,) * 3, dtype=bool)
# for each phi = one circle in azimutal circles
for phi in np.linspace(0, np.deg2rad(360), n_phi,endpoint=False):
# for all lines in polar circle of this azimutal circle
for theta in np.linspace(0, np.deg2rad(360), n_theta,endpoint=False):
# for all distances (0-rmax) in these lines
for r in range(rmax):
coords = spherical_to_cartesian([r, theta, phi]) + rmax
out_mask[tuple(coords)] = True
使用来自this code sample 的spherical_to_cartesian。
这给了我这个(rmax = 50 和 n_theta = n_phi = 8):
【问题讨论】:
标签: python numpy multidimensional-array 3d geometry