Utilities

core.utils.util.apply_affine_transformation(fixed_img: numpy.ndarray, moving_img: numpy.ndarray, transform_initializer: numpy.ndarray) numpy.ndarray[source]

Apply affine transformation using OpenCV.

Parameters:
Returns:

A transformed image.

Return type:

numpy.ndarray

Examples

>>> moving_image = apply_affine_transformation(
...     fixed_image, moving_image, transform_initializer
... )
core.utils.util.apply_deformation_to_points(points, deformation_field)[source]

Apply a deformation field to a set of 2D points.

Parameters:
  • points – np.array of shape (N, 2), points as (x, y) coordinates.

  • deformation_field – np.array of shape (2, H, W), displacement vectors. deformation_field[0] is displacement in x, deformation_field[1] is displacement in y.

Returns:

np.array of shape (N, 2), warped point coordinates.

Return type:

warped_points

core.utils.util.apply_displacement_field(image, displacement_field)[source]

Apply displacement field to warp an image.

Parameters:
  • image – Input image

  • displacement_field – Displacement field [2, H, W]

Returns:

Warped image

Return type:

warped_image

core.utils.util.calculate_resample_size(source, target, output_max_size)[source]
core.utils.util.combine_deformation(u_x, u_y, v_x, v_y)[source]
core.utils.util.compute_center_of_mass(array)[source]
core.utils.util.convert_pytorch_checkpoint(net_state_dict)[source]
core.utils.util.create_deform(source_prep, final_transform, displacement_field, output_path='')[source]

Computes the full deformation field and writes it to an MHA file.

Parameters:
  • source_prep (array-like) – Input source data for matrix_df.

  • final_transform (array-like) – Final transform data for matrix_df.

  • displacement_field (array-like) – Raw displacement field (expected shape: [2, H, W] or similar).

  • output_path (str) – Output MHA file path.

core.utils.util.create_deformation_field(shape_transform, source_prep, u_x, u_y, util, output_path='./533_finerigid.mha')[source]

Creates a deformation field by scaling a 3x3 transformation matrix and composing it with given vector fields.

Parameters:
  • shape_transform (np.ndarray) – A 3x3 transformation matrix (affine or rigid).

  • source_prep (np.ndarray) – Source coordinate grid or image array used for transformation.

  • u_x (np.ndarray) – Components of an existing deformation field (x and y displacement maps).

  • u_y (np.ndarray) – Components of an existing deformation field (x and y displacement maps).

  • util (module) –

    A module or object providing two functions:
    • matrix_df(source, transform): applies rigid transformation.

    • combine_deformation(u_x, u_y, t_x, t_y): composes deformation fields.

  • output_path (str, optional) – Path to save the resulting deformation field as an .mha file.

Returns:

The resulting deformation field as a SimpleITK image.

Return type:

sitk.Image

core.utils.util.create_displacement_field_for_wsi(transform_matrix, source_thumbnail, target_thumbnail)[source]
core.utils.util.create_nonrigid_mha(moving_subsample, nonrigid_transformed_coords, r_x, w_x, w_y, target_prep, create_displacement_field, RegistrationParams, output_path='')[source]

Builds a non-rigid displacement + deformation field and saves it as an .mha file (using fr_x, fr_y as final field).

core.utils.util.create_pyramid(array: numpy.ndarray, num_levels: int, mode: str = 'bilinear')[source]

Creates a resolution pyramid for a NumPy array (assumes uniform downsampling by a factor of 2).

Parameters:
  • array (np.ndarray) – The input array (image or multi-dimensional data).

  • num_levels (int) – The number of pyramid levels.

  • mode (str) – The interpolation mode (“bilinear” or “nearest”).

Returns:

pyramid – The created resolution pyramid.

Return type:

list of np.ndarray

core.utils.util.deform_conversion(displacement_field_tc: torch.Tensor)[source]
core.utils.util.dice(fixed, moving)[source]
core.utils.util.find_scale(images)[source]
core.utils.util.gamma_corrections(img, gamma)[source]
core.utils.util.gaussian_filter(image, sigma)[source]
core.utils.util.load_image(path)[source]

Load an image and return it as RGB (not converting to grayscale).

core.utils.util.load_image_as_grayscale(path)[source]

Load an image and convert it to grayscale.

core.utils.util.load_landmarks(path)[source]

Load landmarks from a CSV file.

core.utils.util.load_landmarks_no_header(path)[source]

Load the first two columns of landmark data from a CSV file (no header).

core.utils.util.matchpoints(points)[source]

Match point between fixed and moving point sets.

Parameters:

points (np.array) – point sets

Returns:

Rotation matrix and translation vector

Return type:

np.array

core.utils.util.matrix_df(image, matrix)[source]
core.utils.util.matrix_mha(image, matrix)[source]
core.utils.util.mse(fixed, moving)[source]

Calculate MSE value.

Parameters:
  • fixed (np.array) – Fixed point set

  • moving (np.array) – Moving point set

Returns:

Mean Square error.

Return type:

float

core.utils.util.normalize(image)[source]
core.utils.util.pad_landmarks(landmarks, x, y)[source]
core.utils.util.phase_correlation(fixed, moving)[source]
core.utils.util.plot_landmarks(landmarks, marker_type, colors=None)[source]
core.utils.util.print_rtre(source_landmarks, target_landmarks, x_size, y_size)[source]
core.utils.util.resample(image, output_x_size, output_y_size)[source]
core.utils.util.resample_both(source, target, resample_ratio)[source]
core.utils.util.resample_displacement_field(u_x, u_y, output_x_size, output_y_size)[source]
core.utils.util.rotate_point(x, y, cx, cy, angle)[source]

Rotate point set around center with certain angle.

Parameters:
  • x (float) – x-coordinate

  • y (float) – y-coordinate

  • cx (float) – center x-coordinate

  • cy (float) – center y-coordinate

  • angle (float) – rotation angle

Returns:

rotated x coordinate float: rotated y coordinate

Return type:

float

core.utils.util.round_up_to_odd(value)[source]
core.utils.util.rtre(landmarks_1, landmarks_2, x_size, y_size)[source]
core.utils.util.save_landmarks(path, landmarks)[source]
core.utils.util.scale_coordinates(images)[source]
core.utils.util.scale_transformation_matrix(transform_matrix, input_res, output_res)[source]

Upscale the transformation matrix to the original image resolution.

Parameters:
  • transform_matrix (np.ndarray) – Transformation matrix.

  • resolution (int) – Original image resolution.

Returns:

Upscaled transformation matrix.

Return type:

np.ndarray

core.utils.util.skip_subsample(points, n_samples=1000)[source]
core.utils.util.sort_coordinates(set1, set2)[source]

Match points between fixed and moving point set.

Parameters:
  • set1 (np.array) – Fixed point set

  • set2 (np.array) – Moving point set

Returns:

New moving point set

Return type:

np.array

core.utils.util.tensor_to_rgb_numpy(tensor)[source]
core.utils.util.to_image(array)[source]
core.utils.util.transform_landmarks(landmarks, u_x, u_y)[source]
core.utils.util.tre(landmarks_1, landmarks_2)[source]
core.utils.util.warp_image(image, u_x, u_y)[source]

MHA Deformation Field Application for CORE

Apply deformation fields computed at low resolution (e.g., 0.625x) to whole slide images at high resolution (e.g., 40x).

Features: - Uses pyvips for memory-efficient large image handling - Scales deformation fields from low to high resolution - Properly scales both spatial dimensions and displacement magnitudes

core.utils.mha_wsi.apply_displacement_field(img: numpy.ndarray | pyvips.Image, displacement: numpy.ndarray, output_shape_rc: Tuple[int, int], source_shape_rc: Tuple[int, int], registered_shape_rc: Tuple[int, int], background: int | float | list | None = None, interpolation: str = 'bicubic') pyvips.Image[source]

Apply displacement field to warp an image.

Parameters:
  • img – Input image to warp

  • displacement – Backwards displacement field (2, H, W)

  • output_shape_rc – Output shape (rows, cols)

  • source_shape_rc – Shape where transform was computed

  • registered_shape_rc – Shape of registered image at transform resolution

  • background – Background color for areas outside the image

  • interpolation – Interpolation method

Returns:

Warped pyvips Image

core.utils.mha_wsi.array_to_vips(arr: numpy.ndarray) pyvips.Image[source]

Convert numpy array to pyvips Image.

core.utils.mha_wsi.get_image_shape(img: numpy.ndarray | pyvips.Image) Tuple[int, ...][source]

Get shape of image (row, col) or (row, col, channels).

core.utils.mha_wsi.load_mha_displacement(mha_path: str) Tuple[numpy.ndarray, dict][source]

Load MHA deformation field and convert to displacement array format.

Output format is (2, H, W) where: - displacement[0] = dx (column/x displacement) - displacement[1] = dy (row/y displacement)

Parameters:

mha_path – Path to MHA file

Returns:

Tuple of (displacement array, metadata dict)

core.utils.mha_wsi.register_wsi_with_mha(mha_path: str, wsi_path: str, output_path: str, source_magnification: float = 0.625, target_magnification: float = 40.0, interpolation: str = 'bicubic', background: int = 0)[source]

Apply MHA deformation field to WSI for registration.

Parameters:
  • mha_path – Path to deformation field MHA file

  • wsi_path – Path to WSI to be warped

  • output_path – Output path for registered image

  • source_magnification – Magnification at which deformation was computed

  • target_magnification – Target WSI magnification

  • interpolation – Interpolation method

  • background – Background color

core.utils.mha_wsi.resize_displacement_field(displacement: numpy.ndarray | Tuple[numpy.ndarray, numpy.ndarray], target_shape_rc: Tuple[int, int], source_shape_rc: Tuple[int, int], registered_shape_rc: Tuple[int, int]) numpy.ndarray[source]

Resize displacement field to match target resolution.

Steps: 1. Resize the displacement field spatially to match target dimensions 2. Scale the displacement values by the ratio of resolutions

Parameters:
  • displacement – Displacement field as (2, H, W) array or tuple of (dx, dy)

  • target_shape_rc – Target output shape (rows, cols)

  • source_shape_rc – Shape of image where transform was computed

  • registered_shape_rc – Shape of registered image at transform resolution

Returns:

Resized displacement field as (2, H, W) array

core.utils.mha_wsi.save_displacement_as_mha(displacement: numpy.ndarray, output_path: str, spacing: Tuple[float, float] = (1.0, 1.0), origin: Tuple[float, float] = (0.0, 0.0))[source]

Save displacement array as MHA file.

Parameters:
  • displacement – Displacement field (2, H, W)

  • output_path – Output path

  • spacing – Pixel spacing

  • origin – Image origin

core.utils.mha_wsi.vips_to_array(vips_img: pyvips.Image) numpy.ndarray[source]

Convert pyvips Image to numpy array.

Imports for CORE registration