| 1 |
""" |
| 2 |
.. _crop_labeled_example: |
| 3 |
|
| 4 |
Crop Labeled ImageData |
| 5 |
~~~~~~~~~~~~~~~~~~~~~~ |
| 6 |
|
| 7 |
Use :meth:`~pyvista.ImageDataFilters.crop` to crop labeled data such as segmented medical |
| 8 |
images. |
| 9 |
|
| 10 |
""" |
| 11 |
|
| 12 |
# sphinx_gallery_thumbnail_number = 2 |
| 13 |
|
| 14 |
import pyvista as pv |
| 15 |
from pyvista import examples |
| 16 |
|
| 17 |
# %% |
| 18 |
# Load a dataset with a CT image and corresponding segmentation labels. Here we load |
| 19 |
# :func:`~pyvista.examples.downloads.download_whole_body_ct_male`. |
| 20 |
|
| 21 |
dataset = examples.download_whole_body_ct_male() |
| 22 |
|
| 23 |
# %% |
| 24 |
# Get the :class:`~pyvista.ImageData` for the CT data and one of the segmentation masks. |
| 25 |
# For this example we choose a mask of the skull. |
| 26 |
|
| 27 |
ct = dataset['ct'] |
| 28 |
skull = dataset['segmentations']['skull'] |
| 29 |
|
| 30 |
# %% |
| 31 |
# Crop the CT image using the segmentation mask. Use ``padding`` to include additional |
| 32 |
# data points around the masked region. |
| 33 |
|
| 34 |
cropped_ct = ct.crop(mask=skull, padding=10) |
| 35 |
|
| 36 |
# %% |
| 37 |
# Use :meth:`~pyvista.ImageDataFilters.points_to_cells` to plot the cropped image |
| 38 |
# as :attr:`~pyvista.CellType.VOXEL` cells. |
| 39 |
|
| 40 |
cpos = pv.CameraPosition( |
| 41 |
position=(687.5, 763.6, 471.3), |
| 42 |
focal_point=(231.8, 296.3, 677.0), |
| 43 |
viewup=(0.107, 0.311, 0.944), |
| 44 |
) |
| 45 |
|
| 46 |
cropped_ct_voxels = cropped_ct.points_to_cells() |
| 47 |
cropped_ct_voxels.plot(volume=True, cpos=cpos) |
| 48 |
|
| 49 |
# %% |
| 50 |
# Include a surface contour of the mask with the plot. |
| 51 |
|
| 52 |
skull_surface = skull.contour_labels() |
| 53 |
|
| 54 |
pl = pv.Plotter() |
| 55 |
pl.add_mesh(skull_surface, color='white') |
| 56 |
pl.add_volume(cropped_ct_voxels) |
| 57 |
pl.camera_position = cpos |
| 58 |
pl.show() |
| 59 |
|
| 60 |
# %% |
| 61 |
# After cropping, the CT image's dimensions are smaller than the mask's. |
| 62 |
|
| 63 |
cropped_ct.dimensions == skull.dimensions |
| 64 |
|
| 65 |
# %% |
| 66 |
# To keep dimension the same, either |
| 67 |
# |
| 68 |
# #. crop the mask itself; the meshes will have smaller dimensions relative to the input |
| 69 |
# #. pad the CT image as part of the initial crop; the meshes will have the same |
| 70 |
# dimensions as the input |
| 71 |
# |
| 72 |
# To crop the mask itself, you can perform a similar crop as before using ``mask=True``. |
| 73 |
|
| 74 |
cropped_skull = skull.crop(mask=True, padding=10) |
| 75 |
cropped_skull.dimensions |
| 76 |
|
| 77 |
# %% |
| 78 |
# However, computationally it is more efficient to crop using ``extent`` directly. |
| 79 |
|
| 80 |
cropped_skull = skull.crop(extent=cropped_ct.extent) |
| 81 |
cropped_skull.dimensions |
| 82 |
|
| 83 |
# %% |
| 84 |
# Alternatively, use ``keep_dimensions`` and ``fill_value`` when initially cropping the |
| 85 |
# image so that the output dimensions match the input. A value of ``-1000`` is used, which |
| 86 |
# may represent air in the scan. |
| 87 |
|
| 88 |
cropped_ct = ct.crop(mask=skull, keep_dimensions=True, fill_value=-1000) |
| 89 |
cropped_ct.dimensions |