resomapper.processing.masking

Functions

no_mask(nifti_file_path, mask_nii_output_path)

Create a binary mask from a NIfTI file and save it to a specified output path.

hist_strip_mask()

manual_mask(nifti_file_path, mask_nii_output_path[, ...])

Create a manual mask for a NIfTI file and save it to a specified output path.

check_mask_shape(img, mask)

Verify that the shape of the mask matches the shape of the image.

prepare_vol(vol_3d)

Some modifications on the volume: 270 degrees rotation and image flip.

min_max_normalization(img)

Apply min-max normalization to the input image. Creates a copy of the input

click(event, x, y, flags, param)

Event handler function for mouse clicks.

itera(ima, refPT)

Iteratively display slices for masking. Left click adds a line and right

Module Contents

resomapper.processing.masking.no_mask(nifti_file_path, mask_nii_output_path)[source]

Create a binary mask from a NIfTI file and save it to a specified output path.

This function loads a NIfTI file, processes its data to create a mask of ones, and saves the resulting mask as a new NIfTI file. If the input data is 4D, only the first volume is used for the mask.

Parameters:
  • nifti_file_path (str) – The file path to the input NIfTI file.

  • mask_nii_output_path (str) – The file path where the output mask NIfTI file will be saved.

Returns:

None

Raises:
  • FileNotFoundError – If the input NIfTI file does not exist.

  • nib.filebasedimages.ImageFileError – If the input file is not a valid NIfTI file.

resomapper.processing.masking.hist_strip_mask()[source]
resomapper.processing.masking.manual_mask(nifti_file_path, mask_nii_output_path, ask_if_repeat=False)[source]

Create a manual mask for a NIfTI file and save it to a specified output path.

This function allows the user to interactively create a mask for each slice of a 3D or 4D NIfTI image. The user can draw the mask outline using mouse clicks, and the function will save the resulting mask as a new NIfTI file.

Parameters:
  • nifti_file_path (str) – The file path to the input NIfTI file.

  • mask_nii_output_path (str) – The file path where the output mask NIfTI file will be saved.

  • ask_if_repeat (bool) – If True, prompts the user to confirm the mask before saving. Defaults to False.

Returns:

None

Raises:
  • FileNotFoundError – If the input NIfTI file does not exist.

  • nib.filebasedimages.ImageFileError – If the input file is not a valid NIfTI file.

resomapper.processing.masking.check_mask_shape(img, mask)[source]

Verify that the shape of the mask matches the shape of the image.

This function checks if the dimensions of the provided mask are compatible with the dimensions of the input image. It raises an error if the shapes do not match, ensuring that the mask can be correctly applied to the image. If the input arguments are file paths, the ‘load_nifti’ function is used to load the respective NIfTI files.

Parameters:
  • img (numpy.ndarray or str) – Input image array or path to the image file.

  • mask (numpy.ndarray or str) – The mask array or path to be checked against the image.

Returns:

True if mask and image match dimensions, False if not.

Return type:

bool

resomapper.processing.masking.prepare_vol(vol_3d)[source]

Some modifications on the volume: 270 degrees rotation and image flip.

Parameters:

vol_3d (ndarray) – Input image.

Returns:

Transformed image ready for visualization.

Return type:

list

resomapper.processing.masking.min_max_normalization(img)[source]

Apply min-max normalization to the input image. Creates a copy of the input image and computes the minimum and maximum values. The image is normalized using the formula (img - min_val) / (max_val - min_val).

Parameters:

img (numpy.ndarray) – Input image array.

Returns:

Normalized image array.

Return type:

numpy.ndarray

resomapper.processing.masking.click(event, x, y, flags, param)[source]

Event handler function for mouse clicks.

Parameters:
  • event – The type of mouse event (left button down, right button down, etc.).

  • x – The x-coordinate of the mouse click position.

  • y – The y-coordinate of the mouse click position.

  • flags – Additional flags associated with the mouse event.

  • param – Additional parameters associated with the mouse event.

The function handles mouse click events and updates the global variables ‘status’ and ‘counter’. If the event is a left button down click, the function appends the coordinates of the click position to the list specified by ‘param[counter]’. If the event is a right button down click, the function performs the same action as the left click and also sets the ‘status’ variable to 0, indicating that the click operation is finished.

Note

  • The global variables ‘status’ and ‘counter’ are used and updated within this function.

  • The ‘param’ argument is expected to be a list or an array-like object.

Example

mouse_params = [[] for _ in range(5)] # Create list to store click positions cv2.setMouseCallback(“window”, click, param=mouse_params)

resomapper.processing.masking.itera(ima, refPT)[source]

Iteratively display slices for masking. Left click adds a line and right click closes the polygon. Next slice will be showed after right click.

Parameters:
  • ima (numpy.ndarray) – Input image array.

  • refPT (list) – List to store the masked vertices for each slice.

The ‘click’ event handler is used to handle mouse events and update the ‘refPT’ list with the coordinates of the drawn lines. The function continues to display and process slices until all slices have been processed or until the ‘c’ key or a right-click event is detected. At that point, the function returns the updated ‘refPT’ list.

Note

  • The global variables ‘counter’ and ‘status’ are used and updated within this function.

  • The ‘click’ event handler is set using ‘cv2.setMouseCallback’ with the ‘refPT’ argument.

Returns:

Updated ‘refPT’ list with the masked vertices for each slice.

Return type:

list

Example

image = np.zeros((256, 256, 3), dtype=np.uint8) # Create a blank image ref_points = [[] for _ in range(10)] # Create list to store masked vertices masker = Mask(study_path) masked_vertices = masker.itera(image, ref_points)