python - 计算 3D 球面掩膜的直径线

标签 python numpy multidimensional-array 3d geometry

背景

对于我正在研究的算法,我目前使用一个 3D 球体作为二元掩模,一个 NxNxN 数组在半径为 N//2 的球体中包含体素> 作为 True。进一步处理为每个体素设置为 True

事实证明,随着 N 变大 = O(N^3),它对我的​​特定任务来说是计算密集型的,所以我现在想将我的二进制掩码减少为从阵列中心在半径范围内辐射的线的子样本.

目标

我想要图像中灰色线条的 3D 二值掩模。

Half-lines

为了稍微控制体素的数量,我将有一个参数(例如 l)调节每个 2D 圆中采样的线数,也许还有第二个参数( >k ?) 为 z 旋转的次数。

我尝试了什么

我正在使用 numpy 和 scipy,我想我可以使用 scipy.ndimage.interpolation.rotate方法在平面上旋转单条线,然后使用完整的 2D 蒙版绕 z 轴旋转。 这被证明是困难的,因为插值使用了一些关于样条的深层魔法,这些样条在旋转时丢弃了我的 True 值。

我想我可以通过遵循一些线性方程从数学上计算出哪个体素应该设置为 True,但我找不到它们。

知道怎么去吗?

更新:解决方案!

感谢 jkalden 帮助我思考这个问题并提供了代码示例,我有这个:

rmax 是球体的半径,n_thetan_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 samplespherical_to_cartesian .

这给了我这个(rmax = 50n_theta = n_phi = 8):

Irised ! (中心区域根据选择调出了我的功能)

最佳答案

我建议将坐标系改为spherical coordinates .因此,您将通过方位角选择 2D 圆,然后通过另外选择极角来定义一条线。沿线的变量就是半径,您可以使用“numpy.linspace”对其进行离散化。这样做还可以节省计算时间。

您可以随时使用实现的双射关系切换坐标系,例如herehere .

关于python - 计算 3D 球面掩膜的直径线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33343042/

相关文章:

python - Python 中的循环问题

python - 将 numpy 数组的负值变为正值

python - 复制 2D 数组以使其成为 3D

python - 多维数组的一维向量点积

c - 存储许多 for 循环的结果

c++ - 将静态二维数组定义为私有(private)

python - 为什么BeautifulSoup找不到HTML类?

python - 为什么这段 python 代码运行了两次

python - 在 rdf/n3 文件中按名称搜索

c++ - 二维数组重新分配