在3D numpy网格中绘制/采样球体 [英] Drawing/sampling a sphere in a 3D numpy grid
问题描述
我想对基于体素的球体进行测量,用一个numpy数组表示。由于采样,这些球体被表示为一组立方体(因为它们在数组中被采样)。我想通过这个网格限制来模拟引入的错误。有没有什么办法在网格中绘制3D球体来运行我的模拟? (所以基本上,一个单位长度为1的球体将会是数组中的一个点)。
或者还有另一种计算采样引入的误差的方法吗?
在2-D中看起来很简单...
最直接的方法是创建一个边界框数组,在每个点处保持距离该球体:
>>> radius = 3
>>> r2 = np.arange(-radius,radius + 1)** 2
>>> dist2 = r2 [:,无,无] + r2 [:,无] + r2
>>> volume = np.sum(dist2 <=半径** 2)
>>>数量
123
二维情况更易于直观化:
>>> dist2 = r2 [:,无] + r2
>>> (dist2 <=半径** 2).astype(np.int)
数组([[0,0,0,1,0,0,0],
[0,1,1 ,1,1,1,0],
[0,1,1,1,1,1,0],
[1,1,1,1,1,1,1],
[0,1,1,1,1,1,0],
[0,1,1,1,1,0,0],
[0,0,0 ,1,0,0,0]])
>>> np.sum(dist2 <=半径** 2)
29
I want to do a voxel-based measurement of spherical objects, represented in a numpy array. Because of the sampling, these spheres are represented as a group of cubes (because they are sampled in the array). I want to do a simulation of the introduced error by this grid-restriction. Is there any way to paint a 3D sphere in a numpy grid to run my simulations on? (So basically, a sphere of unit length one, would be one point in the array)
Or is there another way of calculating the error introduced by sampling?
In 2-D that seems to be easy...
The most direct approach is to create a bounding box array, holding at each point the distance to the center of the sphere:
>>> radius = 3
>>> r2 = np.arange(-radius, radius+1)**2
>>> dist2 = r2[:, None, None] + r2[:, None] + r2
>>> volume = np.sum(dist2 <= radius**2)
>>> volume
123
The 2D case is easier to visualize:
>>> dist2 = r2[:, None] + r2
>>> (dist2 <= radius**2).astype(np.int)
array([[0, 0, 0, 1, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 0],
[1, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 1, 0, 0, 0]])
>>> np.sum(dist2 <= radius**2)
29
这篇关于在3D numpy网格中绘制/采样球体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!