mymesh.converter.mesh2im#
- mymesh.converter.mesh2im(NodeCoords, NodeConn, voxelsize, fill=True, sdf=False, Type=None, indexing='zyx')[source]#
Convert a 3D mesh to a binarized image.
- Parameters:
NodeCoords (array_like) – Coordinates of the nodes in the mesh
NodeConn (array_like, list) – Elements in the mesh, defined by the connectivity of nodes
voxelsize (float) – Voxel size of the image, i.e. image resolution in units/voxel
fill (bool, optional) – Option to fill in the volume of the mesh, by default True. If False, only the boundary will be present in the binarized image. The image will only be able to be filled if the surface mesh is closed to the resolution of the image (i.e. if defects/holes in the mesh are sufficiently smaller than the voxelsize such that they aren’t resolved by the voxelization, then the image will still be able to be filled).
sdf (bool, optional) – Option to make the image a signed distance field using a Euclidean distance transform (
scipy.ndimage.distance_transform_edt()) on the binarized image. The returned image will have values less than zero inside the surface and greater than zero outside the surface. The magnitude of the values is equal to the distance from the surface. This is intended for use with fill=True.Type (str, NoneType, optional) – Mesh Type (‘surf’, ‘vol’), by default None. If not provided, the Type will be inferred using
identify_type().indexing (str, optional) –
Specify how to handle coordinates during the conversion, by default ‘zyx’.
’zyx’: The z coordinate will correspond to the first dimension of the image and the x coordinate will become the last dimension of the image. This is consistent with how meshes/images are handled throughout mymesh and follows a common convention (default)
’xyz’: The x coordinate will correspond to the first dimension of the image and the z coordinate will become the last dimension of the image.
- Returns:
img – Three dimensional binarized (0,1) image of the mesh
- Return type:
np.ndarray
Examples
To convert a surface mesh into a binary image:
import matplotlib.pyplot as plt M = primitives.Torus([0,0,0], 1, .5) img = converter.mesh2im(M.NodeCoords, M.NodeConn, 0.05) plt.imshow(img[10], cmap='gray')
To voxelize only the surface of the mesh, the fill=False option can be used:
import matplotlib.pyplot as plt M = primitives.Torus([0,0,0], 1, .5) img = converter.mesh2im(M.NodeCoords, M.NodeConn, 0.05, fill=False) plt.imshow(img[10], cmap='gray')
The binarized image can be converted to a signed distance field (values are the distance to the surface, sign is negative inside, positive outside) using the sdf=True option:
import matplotlib.pyplot as plt M = primitives.Torus([0,0,0], 1, .5) img = converter.mesh2im(M.NodeCoords, M.NodeConn, 0.05, sdf=True) plt.imshow(img[10], cmap='gray')