ContourToDiscreteMesh#
- class medicalimageanalysis.utils.roi.discrete_converters.ContourToDiscreteMesh(contour_position=None, contour_pixel=None, spacing=None, origin=None, dimensions=None, matrix=None, plane='Axial', mask=None)[source]
Bases:
objectConvert contours into a binary mask and then a discrete or smoothed surface mesh.
The mask is built during construction (unless one is passed in); compute_mesh turns it into a mesh. Axial, coronal and sagittal contours are supported.
- Parameters:
contour_position (list[np.ndarray] | None) – Contours as (n, 3) world points in mm.
contour_pixel (list[np.ndarray] | None) – Contours as (n, 3) voxel indices (x, y, z). Used directly if given.
spacing (tuple[float, float, float] | None) – Voxel spacing in mm (x-y-z).
origin (tuple[float, float, float] | None) – World (x, y, z) of voxel (0, 0, 0).
dimensions (tuple[int, int, int] | None) – Mask shape in z-y-x order.
matrix (np.ndarray | None) – 3x3 direction matrix. Identity if None.
plane (str) – ‘Axial’, ‘Coronal’ or ‘Sagittal’.
mask (np.ndarray | None) – Existing mask. If given, contour conversion and filling are skipped.
Examples
Mesh four stacked square contours (pixel space):
>>> import numpy as np >>> from medicalimageanalysis.utils.roi.converters import ContourToDiscreteMesh >>> square = np.array([[4, 4], [12, 4], [12, 12], [4, 12]], dtype=float) >>> contours = [np.c_[square, np.full(4, z)] for z in range(2, 6)] >>> helper = ContourToDiscreteMesh( ... contour_pixel=contours, ... spacing=(1, 1, 2), ... origin=(0, 0, 0), ... dimensions=(8, 16, 16), ... matrix=np.eye(3), ... ) >>> mesh = helper.compute_mesh()
- convert_to_pixel_spacing()[source]
Convert contour_position (world, mm) to contour_pixel (voxel indices).
Uses a 4x4 world-to-voxel matrix built from matrix, spacing and origin. Called automatically by __init__ when only world contours are given.
- Return type:
None
Examples
>>> import numpy as np >>> mask = np.zeros((8, 16, 16), dtype=np.uint8) >>> helper = ContourToDiscreteMesh(mask=mask, spacing=(1, 1, 2), origin=(0, 0, 0)) >>> helper.contour_position = [np.array([[4, 4, 6], [12, 4, 6], [12, 12, 6]], dtype=float)] >>> helper.convert_to_pixel_spacing() >>> helper.contour_pixel[0][0] # z = 6 mm -> slice 3 array([4., 4., 3.])
- compute_mask()[source]
Fill contour_pixel into a binary mask, slice by slice.
Contours on the same slice are XOR-combined, so an inner ring cuts a hole. The result is stored in self.mask as uint8 0/1.
- Return type:
None
Examples
A ring with a hole on slice 2:
>>> import numpy as np >>> outer = np.array([[2, 2, 2], [13, 2, 2], [13, 13, 2], [2, 13, 2]], dtype=float) >>> inner = np.array([[6, 6, 2], [9, 6, 2], [9, 9, 2], [6, 9, 2]], dtype=float) >>> helper = ContourToDiscreteMesh( ... contour_pixel=[outer, inner], dimensions=(8, 16, 16), ... ) >>> int(helper.mask[2, 3, 3]), int(helper.mask[2, 7, 7]) (1, 0)
- compute_mesh(discrete=False, smoothing_iterations=20, smoothing_relaxation=0.5, smoothing_distance=1)[source]
Convert the mask into a surface mesh in world coordinates.
The mask is padded by one voxel so the surface closes at the image edge.
- Parameters:
discrete (bool) – If True, use discrete marching cubes (blocky, voxel-exact). If False, use smoothed VTK surface nets.
smoothing_iterations (int) – Surface nets smoothing iterations (discrete=False only).
smoothing_relaxation (float) – Surface nets relaxation factor (discrete=False only).
smoothing_distance (float) – Maximum distance a point may move while smoothing (discrete=False only).
- Returns:
Closed surface mesh.
- Return type:
pv.PolyData
Examples
>>> import numpy as np >>> mask = np.zeros((8, 16, 16), dtype=np.uint8) >>> mask[2:6, 4:12, 4:12] = 1 >>> helper = ContourToDiscreteMesh( ... mask=mask, spacing=(1, 1, 2), origin=(0, 0, 0), dimensions=(8, 16, 16), ... )
Smoothed surface (default):
>>> smooth = helper.compute_mesh()
Voxel-exact surface:
>>> blocky = helper.compute_mesh(discrete=True)
Lighter smoothing that stays closer to the voxels:
>>> light = helper.compute_mesh(smoothing_iterations=5, smoothing_distance=0.5)