Data Subpackage

cv2_group.data.data_ingestion.load_image(image_path: str) ndarray

Loads an image from the given file path and converts it to grayscale.

Parameters:

image_path (str) – The path to the image file to be loaded.

Returns:

The grayscale image as a 2D NumPy array.

Return type:

np.ndarray

Raises:

FileNotFoundError – If the image could not be loaded from the specified path.

cv2_group.data.data_ingestion.rename_pairs(data: Dict[str, Tuple[UploadFile, UploadFile]]) Dict[str, Tuple[str, str]]

Generate new filenames for image and mask pairs using UTC timestamp.

Naming convention: - Images: YMDHS_image_x.png - Masks: YMDHS_image_x_root_mask.tif

Parameters:

data (Dict[str, Tuple[UploadFile, UploadFile]]) – Original image/mask UploadFile pairs.

Returns:

Mapping from original keys to (new_image_name, new_mask_name).

Return type:

Dict[str, Tuple[str, str]]

cv2_group.data.data_ingestion.upload_data(images: List[UploadFile], masks: List[UploadFile]) Dict[str, Tuple[UploadFile, UploadFile]]

Pair uploaded image and mask files based on matching base filenames.

Parameters:
  • images (List[UploadFile]) – List of uploaded image files (FastAPI UploadFile objects).

  • masks (List[UploadFile]) – List of uploaded mask files (FastAPI UploadFile objects).

Returns:

Dictionary mapping base filename to (image, mask) tuple.

Return type:

Dict[str, Tuple[UploadFile, UploadFile]]

cv2_group.data.data_ingestion.upload_to_azure_datastore(data: Dict[str, Tuple[UploadFile, UploadFile]], renamed: Dict[str, Tuple[str, str]], datastore: Datastore, image_folder: str = 'raw_img_final', mask_folder: str = 'raw_mask_final') Dict[str, Tuple[str, str]]

Save uploaded image and mask files locally with new names and upload them to AzureML Datastore under specified folder structure.

Parameters:
  • data (Dict[str, Tuple[UploadFile, UploadFile]]) – Original image/mask UploadFile pairs keyed by some identifier.

  • renamed (Dict[str, Tuple[str, str]]) – Mapping of original keys to new (image_name, mask_name).

  • datastore (azureml.core.Datastore) – Azure ML datastore to upload files to.

  • image_folder (str, optional) – Folder path in datastore for images (default: “new_v1/raw_img/”).

  • mask_folder (str, optional) – Folder path in datastore for masks (default: “new_v1/raw_masks/”).

Returns:

Mapping from original keys to full datastore paths for image and mask.

Return type:

Dict[str, Tuple[str, str]]

cv2_group.data.data_processing.create_patches(image: ndarray, patch_size: int) Tuple[ndarray, int, int, ndarray]

Splits a grayscale image into non-overlapping square RGB patches of the given size. The grayscale image is converted to 3-channel RGB before patching, as expected by the model input.

Parameters:
  • image (np.ndarray) – Grayscale image as a 2D NumPy array.

  • patch_size (int) – The height and width of each square patch.

Returns:

  • np.ndarray – 4D array of shape (num_patches, patch_size, patch_size, 3), containing RGB patches.

  • int – Number of patch rows (i).

  • int – Number of patch columns (j).

  • np.ndarray – RGB version of the padded input image.

cv2_group.data.data_processing.create_patches_alternative(image: ndarray, patch_size: int) Tuple[ndarray, int, int]

Split the image into non-overlapping patches of a given size.

This function divides a 2D image into square patches of size patch_size x patch_size, ignoring any remainder areas that do not form a complete patch.

Parameters:
  • image (np.ndarray) – Grayscale input image as a 2D NumPy array.

  • patch_size (int) – Size of each square patch.

Returns:

  • patches (np.ndarray) – Array of image patches with shape (num_patches, patch_size, patch_size).

  • num_rows (int) – Number of patches along the image height.

  • num_cols (int) – Number of patches along the image width.

cv2_group.data.data_processing.crop_image(image: ndarray) Tuple[ndarray, Tuple[int, int, int, int], Tuple[int, int]]

Crops the largest connected component from the image and centers it in a white square of size equal to the largest dimension. Returns the original bounding box and the offsets used for centering.

Parameters:

image (np.ndarray) – Grayscale image as a 2D NumPy array.

Returns:

  • Cropped and centered square image containing the largest component.

  • A tuple (x, y, width, height) representing the bounding box of the largest component in the original input image.

  • A tuple (x_offset, y_offset) representing the top-left corner where the cropped component was placed within the square_image.

Return type:

Tuple[np.ndarray, Tuple[int, int, int, int], Tuple[int, int]]

cv2_group.data.data_processing.crop_image_with_bbox(image: ndarray) Tuple[ndarray, Tuple[int, int, int, int]]

Crop the input image to its largest connected component and make it square.

This function performs the following steps: 1. Applies a median blur to reduce noise. 2. Thresholds the image to create a binary mask. 3. Identifies connected components and selects the largest one. 4. Crops the image to the bounding box of the largest component. 5. Pads the crop to make it a square image centered around the object.

Parameters:

image (np.ndarray) – Grayscale input image as a 2D NumPy array.

Returns:

  • square (np.ndarray) – Cropped and square-padded image as a 2D NumPy array.

  • bbox (Tuple[int, int, int, int]) – Bounding box (x, y, width, height) of the largest connected component.

cv2_group.data.data_processing.pad_image(image: ndarray, patch_size: int) Tuple[ndarray, Tuple[int, int, int, int]]

Pads the image so its dimensions are divisible by patch_size by adding borders symmetrically.

Parameters:
  • image (np.ndarray) – Grayscale image as a 2D NumPy array.

  • patch_size (int) – Size of the patches the image will be divided into.

Returns:

Zero-padded image with height and width divisible by patch_size. A tuple containing padding sizes (top, bottom, left, right).

Return type:

Tuple[np.ndarray, Tuple[int, int, int, int]]

cv2_group.data.data_processing.pad_image_alternative(image: ndarray, patch_size: int) Tuple[ndarray, Tuple[int, int]]

Pad the image so its dimensions are divisible by patch_size.

This function pads the bottom and right sides of a grayscale image using a constant value (255) so that its height and width are evenly divisible by the specified patch size.

Parameters:
  • image (np.ndarray) – Grayscale input image as a 2D NumPy array.

  • patch_size (int) – Desired size of patches to divide the image into.

Returns:

  • padded (np.ndarray) – Padded image with dimensions divisible by patch_size.

  • padding (Tuple[int, int]) – Tuple of padding applied to height and width respectively: (pad_h, pad_w).

cv2_group.data.data_processing.remove_padding(image: ndarray, top: int, bottom: int, left: int, right: int) ndarray

Removes padding from an image based on the given top, bottom, left, and right values.

Parameters:
  • image (np.ndarray) – The padded image.

  • top (int) – Number of pixels to remove from the top.

  • bottom (int) – Number of pixels to remove from the bottom.

  • left (int) – Number of pixels to remove from the left.

  • right (int) – Number of pixels to remove from the right.

Returns:

The cropped image without padding.

Return type:

np.ndarray

cv2_group.data.data_processing.uncrop_mask(cropped_mask: ndarray, original_image_shape: Tuple[int, int], original_bbox: Tuple[int, int, int, int], square_offsets: Tuple[int, int]) ndarray

Reconstructs a mask from its cropped and centered version back to the original image’s dimensions.

Parameters:
  • cropped_mask (np.ndarray) – The mask that corresponds to the square_image produced by crop_image (after remove_padding). Its shape should be (size, size) where size is max(original_bbox_width, original_bbox_height).

  • original_image_shape (Tuple[int, int]) – A tuple (height, width) representing the dimensions of the image before any cropping by crop_image.

  • original_bbox (Tuple[int, int, int, int]) – A tuple (x, y, width, height) representing the bounding box of the largest component in the original input image, as returned by crop_image.

  • square_offsets (Tuple[int, int]) – A tuple (x_offset, y_offset) representing the top-left corner where the cropped component was placed within the square_image, as returned by crop_image.

Returns:

The mask resized and placed back onto a canvas of the original image’s dimensions.

Return type:

np.ndarray

cv2_group.data.data_processing.unpatch_image(preds: ndarray, i: int, j: int, padded_image_shape: Tuple[int, int]) ndarray

Reconstructs the original grayscale prediction image from patches.

Parameters:
  • preds (np.ndarray) – Raw prediction output from the model. Expected shape can be (num_patches, patch_height, patch_width, num_channels) or (num_patches, patch_height, patch_width).

  • i (int) – Patch grid dimensions, representing the number of patches along the height and width.

  • j (int) – Patch grid dimensions, representing the number of patches along the height and width.

  • padded_image_shape (Tuple[int, int]) – The (height, width) of the padded image from which patches were created. Used to determine the full output shape for unpatchify.

Returns:

The reconstructed 2D grayscale prediction image.

Return type:

np.ndarray

cv2_group.data.data_validation.check_size_match(input_image: ndarray, predicted_mask: ndarray) bool

Check if the input image and output mask have the same shape.

REQUIREMENT: Check if the size of the predicted mask matches the input image.

Parameters:
  • input_image (np.ndarray) – The input image array.

  • predicted_mask (np.ndarray) – The predicted_mask array.

Returns:

True if both input and output arrays have the same shape, False otherwise.

Return type:

bool

cv2_group.data.data_validation.is_binary_mask(predicted_mask: ndarray) bool

Check if a given mask is binary (contains only 0 and 255 values).

REQUIREMENT: Ensure the mask is binary.

Parameters:

mask (np.ndarray) – A NumPy array representing the mask to be validated.

Returns:

True if the mask contains only binary values (0 and 255), False otherwise.

Return type:

bool