Exemple #1
0
def plot_contours(roi_array: np.ndarray,
                  template: np.ndarray,
                  fig: plt.figure = None,
                  ax: plt.axis = None):
    """Plot contours of roi.

    Args:
        roi_array (numpy.ndarray): Array of roi masks.
        template (numpy.ndarray): Template image.
        fig (matplotlib.pyplot.figure, optional): Defaults to None. Figure to plot to.
        ax (matplotlib.pyplot.axis, optional): Defaults to None. Axis to plot to.
    """

    if fig is None and ax is None:
        fig = plt.figure()
        ax = fig.add_subplot(111)
    elif ax is None and fig is not None:
        ax = fig.add_subplot(111)
    z, _, _ = roi_array.shape
    np.random.seed(128)
    colors = np.random.rand(3, z)

    for idx, img_slice in enumerate(roi_array):
        clean_border = segmentation.clear_border(img_slice).astype(np.int)
        template = segmentation.mark_boundaries(template,
                                                clean_border,
                                                color=colors[:, idx],
                                                mode='thick')
    ax.imshow(template)
def display_overlay(rgb_image: np.ndarray, binary_mask: np.ndarray, ax: plt.axis, check_mask=False):
    """
    Display version of RGB_image where `False` values in binary_mask are darkened.
    :param rgb_image:
    :param binary_mask:
    :param ax:
    :param check_mask:
    :return:
    """
    if check_mask and binary_mask.dtype is not bool:
        warn("`binary_mask` is non-boolean, attempting conversion.")
        binary_mask = binary_mask.astype(bool)

    overlay = rgb_image.copy()
    overlay[~binary_mask] = overlay[~binary_mask] // 2  # Darken the color of the original image where mask is False
    ax.imshow(overlay)
Exemple #3
0
def draw_model_prediction(fig: plt.figure,
                          ax: plt.axis,
                          model: nn.Module,
                          gw: int = 0,
                          min_point: (float, float) = (0, 0),
                          max_point: (float, float) = (500, 500),
                          resolution: float = 1,
                          apply_ploss: bool = True,
                          colorbar: bool = True,
                          detection_flag: bool = False,
                          detection_threshold: float = -140,
                          device='cpu',
                          *args,
                          **kwargs):
    x_mesh, y_mesh = np.mgrid[min_point[0]:max_point[0]:resolution,
                              min_point[1]:max_point[1]:resolution]
    x = x_mesh.ravel()
    y = y_mesh.ravel()
    data_x = np.stack([x, y], axis=1).astype(np.float32)
    if detection_flag is False:
        z = model(torch.from_numpy(data_x).to(device),
                  apply_ploss).detach()[:, gw, 0].cpu().numpy()
    else:
        #z = model(torch.from_numpy(data_x).to(device), apply_ploss).detach()[:, gw, 0].cpu().numpy()
        z = model(torch.from_numpy(data_x).to(device),
                  apply_ploss).detach()[:, gw, :].cpu().numpy()
        sub_z1 = z[:, 1]
        sub_z = z[:, 0]
        sub_z[sub_z1 < 0.9] = detection_threshold
        z = z[:, 0]
    z = z.reshape((int(max_point[0] - min_point[0]),
                   int(max_point[1] - min_point[1]))).transpose()
    z[z < detection_threshold] = detection_threshold
    im = ax.imshow(z, norm=colors.PowerNorm(gamma=2))
    if colorbar:
        divider = make_axes_locatable(ax)
        cax = divider.append_axes('right', size='5%', pad=0.05)
        fig.colorbar(im, cax=cax, orientation='vertical', label='dBm')
    return im