SimpleAutoImageCropper#
- 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:
objectClass 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.
- Variables:
input_image_path (str) – The file path to the input image.
out_image_path (str) – The file path to save the cropped image.
thresh (float) – The threshold value used to determine the boundaries.
verbose (bool) – If True, prints information about image shapes.
input_img_obj (nibabel.Nifti1Image) – The loaded input image object.
crop_img_obj (nibabel.Nifti1Image) – The cropped image object.
Example
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 )
See also
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.
- Parameters:
input_image_path (str) – The file path to the input image.
out_image_path (str) – The file path to save the cropped image.
thresh_val (float, optional) – The threshold value used to determine the boundaries. Must be less than 0.5. Defaults to 1e-2.
verbose (bool, optional) – If True, prints information about image shapes. Defaults to True.
copy_metadata (bool, optional) – If True, copies metadata from the original image to the cropped image. Defaults to True.
- Raises:
AssertionError – If the thresh_val is not less than 0.5.
Example
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 )
- static gen_line_profile(img_arr: numpy.ndarray, dim: str = 'x')#
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.
- Parameters:
img_arr (np.ndarray) – The input image array.
dim (str, optional) – The dimension along which to compute the line profile. Must be one of ‘x’, ‘y’, or ‘z’. Case-insensitive. Defaults to ‘x’.
- Returns:
np.ndarray – The computed line profile as a 1D array.
- Raises:
AssertionError – If dim is not one of ‘x’, ‘y’, or ‘z’.
Example
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)
- static get_left_and_right_boundary_indices_for_threshold(line_prof: numpy.ndarray, thresh: float = 0.01)#
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.
- Parameters:
line_prof (np.ndarray) – The input line profile as a 1D array.
thresh (float, optional) – The threshold value for determining boundaries. Must be less than 0.5. Defaults to 1e-2.
- 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.
Example
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)
- static get_index_pairs_for_all_dims(img_obj: nibabel.Nifti1Image, thresh: float = 0.01)#
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.
- Parameters:
img_obj (nibabel.Nifti1Image) – The input NIfTI image object.
thresh (float, optional) – The threshold value used to determine the boundaries. Must be less than 0.5. Defaults to 1e-2.
- 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.
See also
Example
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)
- static get_cropped_image(img_obj: nibabel.Nifti1Image, thresh: float = 0.01)#
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.
- Parameters:
img_obj (nibabel.Nifti1Image) – The input NIfTI image object to be cropped.
thresh (float, optional) – The threshold value used to determine the boundaries. Must be less than 0.5. Defaults to 1e-2.
- Returns:
nibabel.Nifti1Image – The cropped NIfTI image object.
- Raises:
AssertionError – If the thresh value is not less than 0.5.
See also
Example
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')