ContourToMask#
- class medicalimageanalysis.utils.roi.discrete_converters.ContourToMask(contour_position=None, contour_pixel=None, spacing=None, origin=None, dimensions=None, matrix=None, plane='Axial')[source]
Bases:
objectConvert contours into a binary volumetric mask.
Construction only stores the inputs; call create_mask to fill self.mask. 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) – Direction matrix (at least 3x3). Required when converting contour_position.
plane (str) – ‘Axial’, ‘Coronal’ or ‘Sagittal’.
Examples
Fill one axial square on slice 2:
>>> import numpy as np >>> from medicalimageanalysis.utils.roi.converters import ContourToMask >>> square = np.array([[4, 4, 2], [12, 4, 2], [12, 12, 2], [4, 12, 2]], dtype=float) >>> helper = ContourToMask(contour_pixel=[square], dimensions=(8, 16, 16)) >>> helper.create_mask() >>> int(helper.mask[2].sum()) # 9 x 9 pixels, edges included 81
- create_mask()[source]
Convert contours to pixel space if needed, then fill self.mask.
- Return type:
None
Examples
Axial contours:
>>> import numpy as np >>> square = np.array([[4, 4, 2], [12, 4, 2], [12, 12, 2], [4, 12, 2]], dtype=float) >>> helper = ContourToMask(contour_pixel=[square], dimensions=(8, 16, 16)) >>> helper.create_mask() >>> helper.mask.shape, helper.mask.dtype ((8, 16, 16), dtype('uint8'))
Sagittal contour on x = 8 (filled in the z-y plane):
>>> sagittal = np.array([[8, 4, 1], [8, 12, 1], [8, 12, 6], [8, 4, 6]], dtype=float) >>> helper = ContourToMask(contour_pixel=[sagittal], dimensions=(8, 16, 16), plane='Sagittal') >>> helper.create_mask() >>> int(helper.mask[3, 8, 8]) 1
- convert_to_pixel_spacing()[source]
Convert contour_position (world, mm) to voxel indices using SimpleITK.
- Return type:
None
Examples
>>> import numpy as np >>> world = [np.array([[4, 4, 4], [12, 4, 4], [12, 12, 4], [4, 12, 4]], dtype=float)] >>> helper = ContourToMask( ... contour_position=world, ... spacing=(1, 1, 2), ... origin=(0, 0, 0), ... dimensions=(16, 16, 8), ... matrix=np.eye(3), ... ) >>> helper.convert_to_pixel_spacing() >>> helper.contour_pixel[0][0] # z = 4 mm -> slice 2 (4.0, 4.0, 2.0)
- 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
Two separate squares on different slices:
>>> import numpy as np >>> a = np.array([[2, 2, 1], [6, 2, 1], [6, 6, 1], [2, 6, 1]], dtype=float) >>> b = np.array([[8, 8, 4], [14, 8, 4], [14, 14, 4], [8, 14, 4]], dtype=float) >>> helper = ContourToMask(contour_pixel=[a, b], dimensions=(8, 16, 16)) >>> helper.compute_mask() >>> [int(helper.mask[k].any()) for k in range(8)] [0, 1, 0, 0, 1, 0, 0, 0]