三维立体图像中椭球体的绘制与分析。
pyellipsoid的Python项目详细描述
py椭球是用于绘制和分析三维体积图像(3d数组)中椭球的软件包。
安装
安装最新版本的最简单方法是使用pip:
$ pip install pyellipsoid
您还可以使用git克隆存储库并手动安装:
$ git clone https://github.com/ashkarin/pyellipsoid.git $ cd pyellipsoid $ python setup.py install
如何使用
绘制椭球体:
importnumpyasnpfrompyellipsoidimportdrawing# Define an image shape, axis order is: Z, Y, Ximage_shape=(128,128,128)# Define an ellipsoid, axis order is: X, Y, Zell_center=(64,64,64)ell_radii=(5,50,30)ell_angles=np.deg2rad([80,30,20])# Order of rotations is X, Y, Z# Draw a 3D binary image containing the ellipsoidimage=drawing.make_ellipsoid_image(image_shape,ell_center,ell_radii,ell_angles)
计算给定椭球图像的惯性椭球:
importnumpyasnpfrompyellipsoidimportdrawing,analysis# Draw a 3D binary image containing an ellipsoidimage_shape=(128,128,128)ell_center=(64,64,64)ell_radii=(5,50,30)ell_angles=np.deg2rad([80,30,20])image=drawing.make_ellipsoid_image(image_shape,ell_center,ell_radii,ell_angles)# Compute inertia ellipsoidpoints=analysis.sample_random_points(image)inertia_ellipsoid=analysis.compute_inertia_ellipsoid(points)
要旋转轴并查找原始轴和旋转轴之间的相对旋转:
importnumpyasnpfrompyellipsoidimportgeometryoriginal_axes=np.array([[1,0,0],[0,1,0],[0,0,1]])rot_angles=np.deg2rad([0,0,45])# Rotate around Z-axis by 45 deg# Build rotation matrix and rotate the axesrotm=geometry.build_rotation_matrix(*rot_angles)rotated_axes=np.dot(rotm,np.transpose(original_axes)).T# Find relative rotationrel_rotm=geometry.find_relative_axes_rotation(original_axes,rotated_axes)# Validate relative rotation matrixrel_rotated_axes=np.dot(rel_rotm,np.transpose(original_axes)).Tassert(np.all(rotated_axes==rel_rotated_axes))# Compute rotation angles# Please note, that these angles might differ from rot_angles!rel_rot_angles=geometry.rotation_matrix_to_angles(rel_rotm)print(np.rad2deg(rel_rot_angles))