Crop Labeled ImageData

Crop Labeled ImageData#

Use crop() to crop labeled data such as segmented medical images.

from __future__ import annotations

import pyvista as pv
from pyvista import examples

Load a dataset with a CT image and corresponding segmentation labels. Here we load download_whole_body_ct_male().

dataset = examples.download_whole_body_ct_male()

Get the ImageData for the CT data and one of the segmentation masks. For this example we choose a mask of the skull.

ct = dataset['ct']
skull = dataset['segmentations']['skull']

Crop the CT image using the segmentation mask. Use padding to include additional data points around the masked region.

cropped_ct = ct.crop(mask=skull, padding=10)

Use points_to_cells() to plot the cropped image as VOXEL cells.

cpos = [(687.5, 763.6, 471.3), (231.8, 296.3, 677.0), (0.107, 0.311, 0.944)]

cropped_ct_voxels = cropped_ct.points_to_cells()
cropped_ct_voxels.plot(volume=True, cpos=cpos)
crop labeled

Include a surface contour of the mask with the plot.

crop labeled

After cropping, the CT image’s dimensions are smaller than the mask’s.

False

To keep dimension the same, either

  1. crop the mask itself; the meshes will have smaller dimensions relative to the input

  2. pad the CT image as part of the initial crop; the meshes will have the same dimensions as the input

To crop the mask itself, you can perform a similar crop as before using mask=True.

(71, 84, 100)

However, computationally it is more efficient to crop using extent directly.

(71, 84, 100)

Alternatively, use keep_dimensions and fill_value when initially cropping the image so that the output dimensions match the input. A value of -1000 is used, which may represent air in the scan.

cropped_ct = ct.crop(mask=skull, keep_dimensions=True, fill_value=-1000)
cropped_ct.dimensions
(160, 160, 273)

Total running time of the script: (0 minutes 5.498 seconds)

Gallery generated by Sphinx-Gallery