Skip to main content

AiSegmentation

AI Segmentation

High-level interface for AI-powered image segmentation. The AiSegmentation class enables users to perform automated segmentation of images using AI models, including TotalSegmentator, nnU-Net, MONAI, and Cellpose-SAM. It provides methods to select and configure models, install required dependencies, check installation status, and run segmentation on one or more image volumes. Results are returned as new mask objects that can be used in further analysis or visualization. Key features: - Choose between multiple AI segmentation models (TotalSegmentator, nnU-Net, MONAI, Cellpose-SAM). - Install and verify model environments and dependencies with a single call. - Run segmentation on one or more volumes by name, with flexible parameter options. - Retrieve available tasks, default parameters, and model information for scripting convenience. - Integrate seamlessly into automated workflows and batch processing scripts.

Import​

import ScriptingApi as api

# Access the class
# AiSegmentation is available via api.AiSegmentation

Methods​

Properties​

set_model_type​

Sets the active AI segmentation model type.

Signature:

set_model_type(modelType: AiSegmentationModelType) -> None

Parameters:

ParameterTypeDescription
modelTypeAiSegmentationModelTypeThe AI model to use for subsequent operations. Use api.AiSegmentationModelType.TotalSegmentator, api.AiSegmentationModelType.NnUnet, api.AiSegmentationModelType.Monai, or api.AiSegmentationModelType.Cellpose.

get_model_type​

Gets the currently active AI segmentation model type.

Signature:

get_model_type() -> AiSegmentationModelType

Returns: AiSegmentationModelType — The currently selected AI model type as api.AiSegmentationModelType.


set_model_type_str​

Sets the active AI segmentation model type using a string representation. Allows setting the model type using a string like "TotalSegmentator", "NnUnet", "Monai", or "Cellpose". Matching is case-insensitive and accepts common aliases (e.g. "nnU-Net", "Cellpose-SAM").

Signature:

set_model_type_str(modelType: str) -> None

Parameters:

ParameterTypeDescription
modelTypestrA string specifying the model type to set. An unrecognized value raises an error.

get_model_type_str​

Gets the currently active AI segmentation model type as a string.

Signature:

get_model_type_str() -> str

Returns: str — The currently selected AI model type as a string.


get_model_name​

Gets the display name of the current AI model.

Signature:

get_model_name() -> str

Returns: str — A string representing the human-readable name of the current model.


get_installation_status​

Checks if the AI segmentation model is installed and ready for use.

Signature:

get_installation_status() -> bool

Returns: bool — True if the model is installed and ready, false otherwise.


set_models_dir_path​

Sets the models directory path for nnU-Net and MONAI models. This path should contain the trained model files and configurations.

Signature:

set_models_dir_path(modelsDirPath: str) -> None

Parameters:

ParameterTypeDescription
modelsDirPathstrThe absolute path to the trained models directory.

get_models_dir_path​

Gets the currently configured trained models directory path.

Signature:

get_models_dir_path() -> str

Returns: str — The path to the trained models directory, or empty string if not set.


get_available_tasks​

Gets the list of available tasks for the current model. For TotalSegmentator: returns available task names (e.g., "total", "body"). For nnU-Net: returns available dataset names. For MONAI: returns available bundle names. For Cellpose-SAM: returns the available segmentation modes ("2D", "3D").

Signature:

get_available_tasks() -> list

Returns: list — List of available task/dataset/bundle/mode names for the current model.


get_default_total_segmentator_params​

Gets the default parameters for the current model type.

Signature:

get_default_total_segmentator_params() -> TotalSegmentatorParams

Returns: TotalSegmentatorParams — The api.TotalSegmentatorParams structure with default settings.


get_default_nn_unet_params​

Gets the default parameters for nnU-Net.

Signature:

get_default_nn_unet_params() -> NnUnetParams

Returns: NnUnetParams — The api.NnUnetParams structure with default settings.


get_default_monai_params​

Gets the default parameters for MONAI.

Signature:

get_default_monai_params() -> MonaiParams

Returns: MonaiParams — The api.MonaiParams structure with default settings.


get_default_cellpose_params​

Gets the default parameters for Cellpose-SAM.

Signature:

get_default_cellpose_params() -> CellposeParams

Returns: CellposeParams — The api.CellposeParams structure with default settings.


General​

install_model​

Installs the current AI model and its dependencies. This operation may take several minutes as it downloads and configures the Python environment and required packages.

Signature:

install_model(showMessages: bool = True) -> bool

Parameters:

ParameterTypeDescription
showMessagesboolIf true, shows installation progress messages while the model environment is being installed. The default is True.

Returns: bool — True if the installation succeeded. On failure an error is raised.


run_total_segmentator​

Runs AI segmentation on specified volume objects using TotalSegmentator. The volumes must exist in the current project and be accessible by name.

Signature:

run_total_segmentator(volumeNames: list, params: TotalSegmentatorParams = TotalSegmentatorParams(), multilabelOutput: bool = True, transferAllLabels: bool = True, selectedLabels: list = [], makeImageOrientationCompatible: bool = True) -> list

Parameters:

ParameterTypeDescription
volumeNameslistList of volume object names to segment.
paramsTotalSegmentatorParamsTotalSegmentator-specific configuration parameters. Create using params = api.TotalSegmentatorParams().
multilabelOutputboolIf true, generates multilabel masks (single mask with multiple label values); otherwise, single-label normal masks.
transferAllLabelsboolIf true, transfers all available labels to output; otherwise, only selected labels.
selectedLabelslistList of specific label names to include when transferAllLabels is false.
makeImageOrientationCompatibleboolWhen enabled (recommended for DICOM imports), Volvicon includes spatial orientation information in the file sent to the AI framework. This ensures TotalSegmentator correctly interprets the image geometry and produce accurate results. Disable this if the volume is already in RAS orientation or if the imported volume is natively in RAS.

Returns: list — List of generated mask object names on success, empty list on failure.


run_nn_unet​

Runs AI segmentation on specified volume objects using nnU-Net. The volumes must exist in the current project and be accessible by name.

Signature:

run_nn_unet(volumeNames: list, params: NnUnetParams, multilabelOutput: bool = True, transferAllLabels: bool = True, selectedLabels: list = [], makeImageOrientationCompatible: bool = True) -> list

Parameters:

ParameterTypeDescription
volumeNameslistList of volume object names to segment.
paramsNnUnetParamsnnU-Net-specific configuration parameters. Create using params = api.NnUnetParams().
multilabelOutputboolIf true, generates multilabel masks (single mask with multiple label values); otherwise, single-label normal masks.
transferAllLabelsboolIf true, transfers all available labels to output; otherwise, only selected labels.
selectedLabelslistList of specific label names to include when transferAllLabels is false.
makeImageOrientationCompatibleboolWhen enabled (recommended for DICOM imports), Volvicon includes spatial orientation information in the file sent to the AI framework. This ensures nnU-Net correctly interprets the image geometry and produce accurate results. Disable this if the volume is already in RAS orientation or if the imported volume is natively in RAS.

Returns: list — List of generated mask object names on success, empty list on failure.


run_monai​

Runs AI segmentation on specified volume objects using MONAI. The volumes must exist in the current project and be accessible by name.

Signature:

run_monai(volumeNames: list, params: MonaiParams, multilabelOutput: bool = True, transferAllLabels: bool = True, selectedLabels: list = []) -> list

Parameters:

ParameterTypeDescription
volumeNameslistList of volume object names to segment.
paramsMonaiParamsMONAI-specific configuration parameters. Create using params = api.MonaiParams().
multilabelOutputboolIf true, generates multilabel masks (single mask with multiple label values); otherwise, single-label normal masks.
transferAllLabelsboolIf true, transfers all available labels to output; otherwise, only selected labels.
selectedLabelslistList of specific label names to include when transferAllLabels is false.

Returns: list — List of generated mask object names on success, empty list on failure.


run_cellpose​

Runs AI segmentation on specified volume objects using Cellpose-SAM. Cellpose-SAM provides general-purpose 2D and 3D cell/object segmentation. In 2D mode, it segments the currently active slice and merges the result into the active mask. In 3D mode, it segments the entire volume.

Signature:

run_cellpose(volumeNames: list, params: CellposeParams = CellposeParams(), multilabelOutput: bool = True) -> list

Parameters:

ParameterTypeDescription
volumeNameslistList of volume object names to segment.
paramsCellposeParamsCellpose-SAM-specific configuration parameters. Create using params = api.CellposeParams().
multilabelOutputboolIf true, all detected instances are packed into a single multilabel mask (each object gets a unique integer label); if false, each detected instance is returned as a separate single-label mask object.

Returns: list — List of generated mask object names on success, empty list on failure.


See Also​