ModelToMask#
- class medicalimageanalysis.utils.roi.discrete_converters.ModelToMask(models, origin=None, spacing=None, dims=None, slice_locations=None, matrix=None, empty_array=True, convert=True)[source]
Bases:
objectConvert one or more surface models into a volumetric mask.
The grid is sized automatically from the combined model bounds plus a 5-voxel buffer on every side. The mask is either empty (default) or filled from axial cuts of the models.
- Parameters:
models (list[pv.PolyData]) – Surface models in mm.
origin (list[float] | None) – Grid origin (x, y, z). Recomputed from the bounds.
spacing (list[float] | None) – Voxel spacing (x, y, z). Set automatically to [1, 1, 3] or [1, 1, 5] for models that fit, otherwise must be given.
dims (list[int] | None) – Mask shape (z, y, x). Recomputed from the bounds.
slice_locations (list[float] | None) – z positions of the mask slices. Recomputed from the bounds.
matrix (np.ndarray | None) – Transformation matrix for the models. Identity if None.
empty_array (bool) – If True, only an empty mask of the right shape is made. If False, the mask is filled with the model cross-sections.
convert (bool) – If True, compute bounds, contours and mask during construction.
Examples
Empty mask sized to fit a model:
>>> import pyvista as pv >>> from medicalimageanalysis.utils.roi.converters import ModelToMask >>> helper = ModelToMask(models=[pv.Sphere(radius=20.0)]) >>> helper.spacing [1, 1, 3] >>> int(helper.mask.max()) 0
Filled mask from two models:
>>> models = [pv.Sphere(radius=10.0), pv.Sphere(radius=10.0, center=(30, 0, 0))] >>> helper = ModelToMask(models=models, empty_array=False) >>> helper.mask.ndim 3
- compute_bounds()[source]
Compute the grid bounds, origin, slice locations and mask dimensions.
The combined bounding box of all models is padded by 5 voxels on each axis. If the models fit in 512 x 512 mm in-plane, the spacing is set to [1, 1, 3] (< 450 mm tall) or [1, 1, 5] (< 750 mm tall).
- Return type:
None
Examples
>>> import pyvista as pv >>> helper = ModelToMask(models=[pv.Sphere(radius=20.0)], convert=False) >>> helper.compute_bounds() >>> helper.spacing [1, 1, 3] >>> helper.bounds[4], helper.bounds[5] # z: -20 - 15, 20 + 15 (-35, 35)
- compute_contours()[source]
Cut each model with axial planes at slice_locations.
Points are stored in pixel units relative to the grid. Slices outside a model get an empty list, so self.contours[model][slice] always lines up with the mask slices.
- Return type:
None
Examples
>>> import pyvista as pv >>> helper = ModelToMask(models=[pv.Sphere(radius=20.0)], convert=False) >>> helper.compute_bounds() >>> helper.compute_contours() >>> len(helper.contours[0]) == len(helper.slice_locations) True
- compute_mask()[source]
Create the int8 mask, filling it from the contours if empty_array is False.
Overlapping models are summed, so a voxel inside two models has value 2.
- Return type:
None
Examples
>>> import pyvista as pv >>> helper = ModelToMask(models=[pv.Sphere(radius=20.0)], empty_array=False, convert=False) >>> helper.compute_bounds() >>> helper.compute_contours() >>> helper.compute_mask() >>> helper.mask.dtype dtype('int8')
- save_image(export_path)[source]
Save the mask with SimpleITK, using the grid spacing and origin.
- Parameters:
export_path (str) – Output file path. The extension picks the format (.nii.gz, .mhd, .nrrd, …).
- Return type:
None
Examples
>>> import pyvista as pv >>> helper = ModelToMask(models=[pv.Sphere(radius=20.0)], empty_array=False) >>> helper.save_image('C:/Data/sphere_mask.nii.gz')
Save as MetaImage:
>>> helper.save_image('C:/Data/sphere_mask.mhd')