resomapper.processing.masking ============================= .. py:module:: resomapper.processing.masking Functions --------- .. autoapisummary:: resomapper.processing.masking.no_mask resomapper.processing.masking.hist_strip_mask resomapper.processing.masking.manual_mask resomapper.processing.masking.check_mask_shape resomapper.processing.masking.prepare_vol resomapper.processing.masking.min_max_normalization resomapper.processing.masking.click resomapper.processing.masking.itera Module Contents --------------- .. py:function:: 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. 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. :param nifti_file_path: The file path to the input NIfTI file. :type nifti_file_path: str :param mask_nii_output_path: The file path where the output mask NIfTI file will be saved. :type mask_nii_output_path: str :returns: None :raises FileNotFoundError: If the input NIfTI file does not exist. :raises nib.filebasedimages.ImageFileError: If the input file is not a valid NIfTI file. .. py:function:: hist_strip_mask() .. py:function:: manual_mask(nifti_file_path, mask_nii_output_path, ask_if_repeat=False) 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. :param nifti_file_path: The file path to the input NIfTI file. :type nifti_file_path: str :param mask_nii_output_path: The file path where the output mask NIfTI file will be saved. :type mask_nii_output_path: str :param ask_if_repeat: If True, prompts the user to confirm the mask before saving. Defaults to False. :type ask_if_repeat: bool :returns: None :raises FileNotFoundError: If the input NIfTI file does not exist. :raises nib.filebasedimages.ImageFileError: If the input file is not a valid NIfTI file. .. py:function:: check_mask_shape(img, mask) 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. :param img: Input image array or path to the image file. :type img: numpy.ndarray or str :param mask: The mask array or path to be checked against the image. :type mask: numpy.ndarray or str :returns: True if mask and image match dimensions, False if not. :rtype: bool .. py:function:: prepare_vol(vol_3d) Some modifications on the volume: 270 degrees rotation and image flip. :param vol_3d: Input image. :type vol_3d: ndarray :returns: Transformed image ready for visualization. :rtype: list .. py:function:: min_max_normalization(img) 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). :param img: Input image array. :type img: numpy.ndarray :returns: Normalized image array. :rtype: numpy.ndarray .. py:function:: click(event, x, y, flags, param) Event handler function for mouse clicks. :param event: The type of mouse event (left button down, right button down, etc.). :param x: The x-coordinate of the mouse click position. :param y: The y-coordinate of the mouse click position. :param flags: Additional flags associated with the mouse event. :param 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. .. rubric:: Example mouse_params = [[] for _ in range(5)] # Create list to store click positions cv2.setMouseCallback("window", click, param=mouse_params) .. py:function:: itera(ima, refPT) Iteratively display slices for masking. Left click adds a line and right click closes the polygon. Next slice will be showed after right click. :param ima: Input image array. :type ima: numpy.ndarray :param refPT: List to store the masked vertices for each slice. :type refPT: list 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. :rtype: list .. rubric:: 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)