Exemplo n.º 1
0
def surface_distance(target, estimated, spacing=list((1, 1, 3))):
    a = as_logical(target)
    b = as_logical(estimated)
    a_bound = np.stack(np.where(np.logical_and(a, np.logical_not(imerode(a)))),
                       axis=1) * spacing
    b_bound = np.stack(np.where(np.logical_and(b, np.logical_not(imerode(b)))),
                       axis=1) * spacing
    return eucl_distance(a_bound, b_bound)
Exemplo n.º 2
0
def get_slices_boundary(masks, patch_size, rois=None, rate=0.1):
    patch_half = map(lambda p_length: p_length // 2, patch_size)
    boundaries = map(
        lambda m: map(lambda l: log_and(m == l, log_not(imerode(m == l))),
                      range(1,
                            m.max() + 1)), masks)

    max_shape = masks[0].shape
    mesh = get_mesh(max_shape)
    legal_mask = reduce(
        np.logical_and,
        map(
            lambda (m_j, p_ij, max_j): np.logical_and(m_j >= p_ij, m_j <= max_j
                                                      - p_ij),
            zip(mesh, patch_half, max_shape)))

    boundaries = map(
        lambda b: map(lambda b_i: np.logical_and(b_i, legal_mask), b),
        boundaries)

    centers = map(
        lambda b: np.concatenate(filter(
            lambda arr: arr.size > 0,
            map(
                lambda b_i: np.random.permutation(zip(*np.where(b_i)))[:int(
                    np.sum(b_i) * rate)], b)),
                                 axis=0), boundaries)

    patch_slices = map(lambda c: centers_to_slice(c, patch_half), centers)

    return patch_slices
def remove_boundary_regions(img_vol, mask_vol, boundary=2):
    """
    Function to remove regions that are inside the boundary of mask. The
    boundary's thickness can also be defined (default is 2 voxels).
    :param img_vol: Mask volume to process. It should be a numpy array of type
     bool.
    :param mask_vol: Mask from where the boundary area is defined.
    :param boundary: Boundary thickness (default = 2).
    :return: New mask without boundary regions.
    """
    # White matter lesions, should not be on the boundaries of a brain. That
    # region is where the cortex is located which can sometimes present
    # hyperintense artifacts. A way of removing some is to remove all lesions
    # that are on the boundaries (given a certain thickness).

    # First, we'll create a boundary mask by eroding the brain mask and taking
    # only the region of the mask that is not part of the erosion.
    new_mask = np.copy(img_vol)
    im_lab = bwlabeln(img_vol)
    mask_bin = mask_vol.astype(np.bool)
    inner = imerode(mask_bin, iterations=boundary)
    boundary = np.logical_and(mask_bin, np.logical_not(inner))

    # Then it's just a matter of removing any lesion that overlaps with that
    # boundary mask.
    boundary_labs = np.unique(im_lab[boundary])
    boundary_labs = boundary_labs[boundary_labs > 0]
    if len(boundary_labs) > 0:
        new_mask[np.isin(im_lab, boundary_labs)] = 0

    return new_mask
Exemplo n.º 4
0
def surface_distance(gt, mask, spacing=list((1, 1, 1))):
    """
    Compute the surface distance between the input mask and a
    ground truth mask

    Inputs:
    - gt: 3D np.ndarray, reference image (ground truth)
    - mask: 3D np.ndarray, input MRI mask
    - spacing: sets the input resolution (def: (1, 1, 1))

    Output:
    - (float) Euclidian distance between gt and mask

    """
    a = as_logical(gt)
    b = as_logical(mask)
    a_bound = np.stack(np.where(np.logical_and(a, np.logical_not(imerode(a)))),
                       axis=1) * spacing
    b_bound = np.stack(np.where(np.logical_and(b, np.logical_not(imerode(b)))),
                       axis=1) * spacing
    return eucl_distance(a_bound, b_bound)
Exemplo n.º 5
0
def get_mask(mask_name, dilate=0, dtype=np.uint8):
    # Lesion mask
    mask_image = load_nii(mask_name).get_data().astype(dtype)
    if dilate > 0:
        mask_d = imdilate(
            mask_image,
            iterations=dilate
        )
        mask_e = imerode(
            mask_image,
            iterations=dilate
        )
        mask_image = np.logical_and(mask_d, np.logical_not(mask_e)).astype(dtype)

    return mask_image
Exemplo n.º 6
0
def get_mask(mask_name, dilate=0, dtype=np.uint8):
    """
    Function to load a mask image
    :param mask_name: Path to the mask image file
    :param dilate: Dilation radius
    :param dtype: Data type for the final mask
    :return:
    """
    # Lesion mask
    mask_image = (load_nii(mask_name).get_fdata() > 0.5).astype(dtype)
    if dilate > 0:
        mask_d = imdilate(mask_image, iterations=dilate)
        mask_e = imerode(mask_image, iterations=dilate)
        mask_image = np.logical_and(mask_d,
                                    np.logical_not(mask_e)).astype(dtype)

    return mask_image
Exemplo n.º 7
0
def apply_blur_effect_on_single_frame(orig_frame, mask):
    """
    applying the blur effect to the given frame and mask
    :param orig_frame:
    :param mask:
    :return:
    """
    after_effect = orig_frame.copy()
    eroded_mask = imerode(mask.copy(), structure=np.ones((20, 20)))
    after_effect[:, :, 0] = eroded_mask * after_effect[:, :, 0] + (
        1 - eroded_mask) * __blur_spatial(after_effect[:, :, 0], 7)
    after_effect[:, :, 1] = eroded_mask * after_effect[:, :, 1] + (
        1 - eroded_mask) * __blur_spatial(after_effect[:, :, 1], 7)
    after_effect[:, :, 2] = eroded_mask * after_effect[:, :, 2] + (
        1 - eroded_mask) * __blur_spatial(after_effect[:, :, 2], 7)
    plt.figure()
    plt.imshow(after_effect)
    plt.show()
    return after_effect
Exemplo n.º 8
0
def surface_distance(target, estimated, spacing=list((1, 1, 3))):
    a = as_logical(target)
    b = as_logical(estimated)
    a_bound = np.stack(np.where(np.logical_and(a, np.logical_not(imerode(a)))), axis=1) * spacing
    b_bound = np.stack(np.where(np.logical_and(b, np.logical_not(imerode(b)))), axis=1) * spacing
    return eucl_distance(a_bound, b_bound)