HypVINN.utils.img_processing_utils

HypVINN.utils.img_processing_utils.get_clean_labels(segmentation)[source]

Get clean labels by removing non-connected components from a dilated mask and any connected component with size less than 3.

Parameters:
segmentationnp.ndarray

The segmentation mask.

Returns:
clean_seg: np.ndarray

The cleaned segmentation mask.

labels_cc: np.ndarray

The labels of the connected components in the segmentation mask.

HypVINN.utils.img_processing_utils.get_clean_mask(segmentation, optic=False)[source]

Get a clean mask by removing non-connected components from a dilated mask.

This function takes a segmentation mask and an optional boolean flag indicating whether to consider optic labels. It removes not connected components from the segmentation mask and returns the cleaned segmentation mask, the labels of the connected components, and a flag indicating whether to save the mask.

Parameters:
segmentationnp.ndarray

The input segmentation mask.

opticbool, default=False

A flag indicating whether to consider optic labels. Default is False.

Returns:
clean_segnp.ndarray

The cleaned segmentation mask.

labels_ccnp.ndarray

The labels of the connected components in the segmentation mask.

savemaskbool

A flag indicating whether to save the mask.

HypVINN.utils.img_processing_utils.img2axcodes(img)[source]

Convert the affine matrix of an image to axis codes.

This function takes an image as input and returns the axis codes corresponding to the affine matrix of the image.

Parameters:
imgnibabel image object

The input image.

Returns:
tuple

The axis codes corresponding to the affine matrix of the image.

HypVINN.utils.img_processing_utils.save_logits(logits, orig_path, ras_affine, ras_header, save_dir, mode)[source]

Save the logits (raw model outputs) as a NIfTI image.

This function takes the logits, reorients the image to match the original image’s orientation, and saves the results.

Parameters:
logitsnpt.NDArray[float]

The raw model outputs.

orig_pathPath

The path to the original image.

ras_affinenpt.NDArray[float]

The affine transformation of the RAS orientation.

ras_headernib.nifti1.Nifti1Header

The header of the RAS orientation.

save_dirPath

The directory where the logits will be saved.

modestr

The mode of operation.

Returns:
save_as: Path

The path where the logits were saved.

HypVINN.utils.img_processing_utils.save_segmentation(prediction, orig_path, ras_affine, ras_header, subject_dir, seg_file, mask_file, save_mask=False)[source]

Save the segmentation results.

This function takes the prediction results, cleans the labels, maps them to FreeSurfer Hypvinn Labels, and saves the results. It also reorients the mask and prediction images to match the original image’s orientation.

Parameters:
predictionnp.ndarray

The prediction results.

orig_pathPath

The path to the original image.

ras_affinenpt.NDArray[float]

The affine transformation of the RAS orientation.

ras_headernibabel header object

The header of the RAS orientation.

subject_dirPath

The directory where the subject’s data is stored.

seg_filePath

The file where the segmentation will be saved (relative to subject_dir/mri).

mask_filestr

The file where the mask will be saved (relative to subject_dir/mri).

save_maskbool, default=False

Whether to save the mask or not. Default is False.

Returns:
float

The time taken to save the segmentation.