背景
我正在开发一个算法,目前使用一个3D球作为二进制掩模,使用NxNxN
数组来表示半径为N // 2
的球体内的体素为True
。进一步处理会对每个设置为True
的体素进行计算。
随着N
的增大,它在我的特定任务中变得计算密集,时间复杂度为O(N^3),因此我现在想将我的二进制掩模减少到从阵列中心向外辐射的线的子采样。
目标
我想要一种灰色图像中的线条的3D二进制掩模。
为了在一定程度上控制体素的数量,我会有一个参数(例如l
)来调节每个2D圆中采样的线数,以及可能是第二个参数(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
使用来自此代码示例的spherical_to_cartesian
方法。
当rmax = 50
和n_theta = n_phi = 8
时,得到以下结果: