SimpleAutoImageCropper ========================================================= .. py:class:: petpal.preproc.image_operations_4d.SimpleAutoImageCropper(input_image_path: str, out_image_path: str, thresh_val: float = 0.01, verbose: bool = True, copy_metadata: bool = True) Bases: :py:obj:`object` Class for automatically cropping 3D or 4D medical images based on pixel intensity thresholds. This class provides functionality to load a medical image, determine the meaningful regions by thresholding, and crop the image to remove regions outside these boundaries. It also supports copying metadata from the original image. :ivar input_image_path: The file path to the input image. :vartype input_image_path: str :ivar out_image_path: The file path to save the cropped image. :vartype out_image_path: str :ivar thresh: The threshold value used to determine the boundaries. :vartype thresh: float :ivar verbose: If True, prints information about image shapes. :vartype verbose: bool :ivar input_img_obj: The loaded input image object. :vartype input_img_obj: nibabel.Nifti1Image :ivar crop_img_obj: The cropped image object. :vartype crop_img_obj: nibabel.Nifti1Image .. rubric:: Example .. code-block:: python from petpal.preproc.image_operations_4d import SimpleAutoImageCropper cropper = SimpleAutoImageCropper( input_image_path='path/to/input_image_path.nii', out_image_path='path/to/output_image.nii', thresh_val=0.01, verbose=True, copy_metadata=True ) .. seealso:: - :meth:`get_cropped_image` - :meth:`get_index_pairs_for_all_dims` - :meth:`get_left_and_right_boundary_indices_for_threshold` - :meth:`gen_line_profile` Initializes the SimpleAutoImageCropper with input image path, output image path, and other parameters. Loads the input image, generates the cropped image using the specified threshold, and saves it to the output path. :param input_image_path: The file path to the input image. :type input_image_path: str :param out_image_path: The file path to save the cropped image. :type out_image_path: str :param thresh_val: The threshold value used to determine the boundaries. Must be less than 0.5. Defaults to 1e-2. :type thresh_val: float, optional :param verbose: If True, prints information about image shapes. Defaults to True. :type verbose: bool, optional :param copy_metadata: If True, copies metadata from the original image to the cropped image. Defaults to True. :type copy_metadata: bool, optional :raises AssertionError: If the `thresh_val` is not less than 0.5. .. rubric:: Example .. code-block:: python from petpal.preproc.image_operations_4d import SimpleAutoImageCropper cropper = SimpleAutoImageCropper( input_image_path='path/to/input_image_path.nii', out_image_path='path/to/output_image.nii', thresh_val=0.01, verbose=True, copy_metadata=True ) .. py:method:: gen_line_profile(img_arr: numpy.ndarray, dim: str = 'x') :staticmethod: Generates a line profile by averaging the pixel intensities along specified dimensions. This function computes the mean pixel intensities along a specified dimension (x, y, or z) of a 3D or 4D image array. :param img_arr: The input image array. :type img_arr: np.ndarray :param dim: The dimension along which to compute the line profile. Must be one of 'x', 'y', or 'z'. Case-insensitive. Defaults to 'x'. :type dim: str, optional :returns: *np.ndarray* -- The computed line profile as a 1D array. :raises AssertionError: If `dim` is not one of 'x', 'y', or 'z'. .. rubric:: Example .. code-block:: python import numpy as np from petpal.preproc.image_operations_4d import SimpleAutoImageCropper img_arr = np.random.rand(100, 100, 100) # Example 3D array x_profile = SimpleAutoImageCropper.gen_line_profile(img_arr=img_arr, dim='x') print(x_profile) .. py:method:: get_left_and_right_boundary_indices_for_threshold(line_prof: numpy.ndarray, thresh: float = 0.01) :staticmethod: Determines the left and right boundary indices above a threshold in a line profile. This function identifies the indices where the normalized line profile crosses the specified threshold value, indicating the boundaries of the region of interest. :param line_prof: The input line profile as a 1D array. :type line_prof: np.ndarray :param thresh: The threshold value for determining boundaries. Must be less than 0.5. Defaults to 1e-2. :type thresh: float, optional :returns: *tuple* -- A tuple containing the left and right boundary indices (left_index, right_index). :raises AssertionError: If the `thresh` value is not less than 0.5. .. rubric:: Example .. code-block:: python import numpy as np from petpal.preproc.image_operations_4d import SimpleAutoImageCropper as Crop line_prof = np.random.rand(100) # Example normalized line profile boundaries = Crop.get_left_and_right_boundary_indices_for_threshold left_index, right_index = boundaries(line_prof=line_prof, thresh=0.01) print(left_index, right_index) .. py:method:: get_index_pairs_for_all_dims(img_obj: nibabel.Nifti1Image, thresh: float = 0.01) :staticmethod: Gets the boundary indices for each dimension of the input image based on a threshold value. This function computes the left and right boundary indices for all dimensions (x, y, z) by generating line profiles and applying a threshold to identify meaningful regions. :param img_obj: The input NIfTI image object. :type img_obj: nibabel.Nifti1Image :param thresh: The threshold value used to determine the boundaries. Must be less than 0.5. Defaults to 1e-2. :type thresh: float, optional :returns: *tuple* -- A tuple of boundary index pairs for each dimension, formatted as ((x_left, x_right), (y_left, y_right), (z_left, z_right)). :raises AssertionError: If the `thresh` value is not less than 0.5. .. seealso:: - :meth:`get_index_pairs_for_all_dims` .. rubric:: Example .. code-block:: python import nibabel as nib from petpal.preproc.image_operations_4d import SimpleAutoImageCropper input_image_path = 'path/to/input_image_path.nii' img_obj = nib.load(input_image_path) boundaries = SimpleAutoImageCropper.get_index_pairs_for_all_dims(img_obj=img_obj, thresh=0.01) print(boundaries) .. py:method:: get_cropped_image(img_obj: nibabel.Nifti1Image, thresh: float = 0.01) :staticmethod: Crops the input medical image based on a threshold value. This function determines the boundaries of the meaningful regions in the input image by thresholding and then crops the image to remove regions outside these boundaries. :param img_obj: The input NIfTI image object to be cropped. :type img_obj: nibabel.Nifti1Image :param thresh: The threshold value used to determine the boundaries. Must be less than 0.5. Defaults to 1e-2. :type thresh: float, optional :returns: *nibabel.Nifti1Image* -- The cropped NIfTI image object. :raises AssertionError: If the `thresh` value is not less than 0.5. .. seealso:: - :meth:`get_index_pairs_for_all_dims` - :meth:`get_left_and_right_boundary_indices_for_threshold` - :meth:`gen_line_profile` .. rubric:: Example .. code-block:: python import nibabel as nib from petpal.preproc.image_operations_4d import SimpleAutoImageCropper input_image_path = 'path/to/input_image_path.nii' img_obj = nib.load(input_image_path) cropped_img = SimpleAutoImageCropper.get_cropped_image(img_obj=img_obj, thresh=0.01) nib.save(cropped_img, 'path/to/output_image.nii')