Utils
- MapImage(image: Union[numpy.ndarray, torch.Tensor], original_values: List[int]) Union[numpy.ndarray, torch.Tensor]
Maps the current values of a given input image to the values given by the tuple (current values, new values).
- Parameters
image (Union[np.ndarray, torch.Tensor]) – The input image to transform
original_values (List[int]) – List of original values to be mapped
- Raises
TypeError – If the input image is neither a numpy array or a torch tensor
- Returns
the transformed input after mapping.
- Return type
Union[np.ndarray, torch.Tensor]
- Example::
transformed_image = MapImage(image, [0, 85, 170]) The values will be mapped to [0, 1, 2] with the first value of the original values specified being the background index.
- contrast_img(img: numpy.ndarray) numpy.ndarray
Applies the Adaptive Equalization or histogram equalization contrast method. This method enhances the contrast of an image by adjusting the intensity values of pixels based on the distribution of pixel intensities in the image’s histogram.
- Parameters
img (np.ndarray) – input image
- Returns
contrasted image
- Return type
np.ndarray
- createBinaryAnnotation(img: Union[numpy.ndarray, torch.Tensor]) Union[numpy.ndarray, torch.Tensor]
Creates a binary mask out of the prediction result with root as foreground and the rest as background
- Parameters
img (Union[np.ndarray, torch.Tensor]) – Input image
- Raises
TypeError – if the input is neither a numpy array or a torch tensor
- Returns
binary mask
- Return type
Union[np.ndarray, torch.Tensor]
- elliptical_crop(img: numpy.ndarray, center_x: int, center_y: int, width: int, height: int) Tuple[numpy.ndarray, numpy.ndarray]
Crops out an elliptical shape out of the input image and sets the rest as background
- Parameters
img (np.ndarray) – Input image
center_x (int) – Center x coordinate of the ellipse
center_y (int) – Center y coordinate of the ellipse
width (int) – Width of the wanted ellipse
height (int) – Height of the wanted ellipse
- Returns
cropped output image
- Return type
Tuple[np.ndarray, np.ndarray]
- extract_largest_component_bbox_image(img: Union[numpy.ndarray, torch.Tensor], lab: Optional[Union[numpy.ndarray, torch.Tensor]] = None) Union[numpy.ndarray, Tuple[numpy.ndarray, numpy.ndarray]]
Extract the largest connected component (LCC) of the given image and get the bounding box associated with this LCC. Depending on the input parameters, it can either:
Crop the image to the bounding box.
Apply a binary mask to the image, maintaining the same shape as the input image, with everything but the LCC set as a black background.
This function can be applied to both the image and the annotation (label) if provided.
- Parameters
img (Union[numpy.ndarray, torch.Tensor]) – The input image. - Can be a NumPy array or PyTorch tensor with shape (C, H, W) or (B, C, H, W) if a batch dimension is included. - Must not be None; if None, the function raises a ValueError.
lab (Union[numpy.ndarray, torch.Tensor], optional) – The label or annotated image. Defaults to None.
- Returns
If lab is None: Returns the processed image as a NumPy array or PyTorch tensor.
- If lab is provided: Returns a tuple containing:
The processed image (NumPy array).
The processed label image (NumPy array).
- Return type
Union[numpy.ndarray, Tuple[numpy.ndarray, numpy.ndarray]]
- get_biomass(binary_img: numpy.ndarray) int
Calculate the biomass by counting the number of pixels equal to 1
- Parameters
binary_img (np.ndarray) – input image as binary mask
- Returns
integer value corresponding to the pixel count or root biomass.
- Return type
int
- get_image_paths(dir: str) List[str]
Goes through a folder directory and lists all filepaths
- Parameters
dir (str) – folder directory to extract from all the filepaths
- Returns
list of all filepaths
- Return type
List[str]
- get_weights(labels: torch.Tensor, classes: List[int], device: str, include_background=False) List[float]
Computes the weights of each class in the batch of labeled images
- Parameters
labels (torch.Tensor) – Batch of labeled imagse with each pixel of an image equal to a class value.
classes (List[int]) – List of the classes
device (str) – training device should be ‘cuda’ if training on GPU
include_background (bool, optional) – Boolean to include or not the background valued as 0 when calculating the weight of the classes in the images. Defaults to False.
- Returns
List of the class weights (floats).
- Return type
List[float]