def mi_img(y_true, y_pred, mask=None, metric_type="MattesMutualInformation"): """Compute the mutual information (MI) between two images. Parameters ---------- y_true : np.array Image 1. Either (h, w) of (N, h, w). If (N, h, w), the decorator `multiple_images_decorator` takes care of the sample dimension. y_pred : np.array Image 2. Either (h, w) of (N, h, w). If (N, h, w), the decorator `multiple_images_decorator` takes care of the sample dimension. mask: np.array, optional Optional, can be specified to have the computation carried out on a precise area. metric_type: str, {'MattesMutualInformation', 'JointHistogramMutualInformation'} Type of mutual information computation. Returns ------- mi : float The mutual information (MI) metric. Similarity metric, the higher the more similar the images are. """ y_true_ants = ants.image_clone(ants.from_numpy(y_true), pixeltype="float") y_pred_ants = ants.image_clone(ants.from_numpy(y_pred), pixeltype="float") if mask is None: mi = ants.image_similarity(y_true_ants, y_pred_ants, metric_type=metric_type) else: mask_ants = ants.image_clone(ants.from_numpy(mask.astype(float)), pixeltype="float") mi = ants.image_similarity( y_true_ants, y_pred_ants, fixed_mask=mask_ants, moving_mask=mask_ants, metric_type=metric_type, ) return -mi
def cross_correlation_img(y_true, y_pred, mask=None): """Compute the cross correlation metric between two images. Parameters ---------- y_true : np.array Image 1. Either (h, w) of (N, h, w). If (N, h, w), the decorator `multiple_images_decorator` takes care of the sample dimension. y_pred : np.array Image 2. Either (h, w) of (N, h, w). If (N, h, w), the decorator `multiple_images_decorator` takes care of the sample dimension. mask: np.array, optional Optional, can be specified to have the computation carried out on a precise area. Returns ------- cc : float The Cross-Correlation value. Similarity metric, the higher the more similar the images are. """ y_true_ants = ants.image_clone(ants.from_numpy(y_true), pixeltype="float") y_pred_ants = ants.image_clone(ants.from_numpy(y_pred), pixeltype="float") if mask is None: cc = ants.image_similarity(y_true_ants, y_pred_ants, metric_type="Correlation") else: mask_ants = ants.image_clone(ants.from_numpy(mask.astype(float)), pixeltype="float") cc = ants.image_similarity( y_true_ants, y_pred_ants, fixed_mask=mask_ants, moving_mask=mask_ants, metric_type="Correlation", ) return -cc
def test_image_similarity_example(self): x = ants.image_read(ants.get_ants_data('r16')) y = ants.image_read(ants.get_ants_data('r30')) metric = ants.image_similarity(x, y, metric_type='MeanSquares')
def test_image_similarity_example(self): x = ants.image_read(ants.get_ants_data("r16")) y = ants.image_read(ants.get_ants_data("r30")) metric = ants.image_similarity(x, y, metric_type="MeanSquares")