Example #1
0
def pre_processing(dict_images):
    MR = dict_images['MR']
    MR = np.clip(MR / 2048, a_min=0, a_max=1)
    Mask = dict_images['Mask']
    _, D, H, W = MR.shape

    heatmap_generator = HeatmapGenerator(image_size=(D, H, W),
                                         sigma=2.,
                                         scale_factor=1.,
                                         normalize=True,
                                         size_sigma_factor=8,
                                         sigma_scale_factor=2,
                                         dtype=np.float32)
    list_landmarks = dict_images['list_landmarks']

    index = random.randint(10, 18)
    while True in np.isnan(list_landmarks[index]):
        index = random.randint(10, 18)

    heatmap = heatmap_generator.generate_heatmap(landmark=list_landmarks[index])[np.newaxis, :, :, :]
    Mask = np.where(Mask == index + 1, 1, 0)  # just segment one IVD

    if D > 12:
        start = random.choice([i for i in range(D - 12 + 1)])
        MR = crop(MR, start=start, end=start + 12, axis='z')
        heatmap = crop(heatmap, start=start, end=start + 12, axis='z')
        Mask = crop(Mask, start=start, end=start + 12, axis='z')

    MR = crop_to_center(MR, list_landmarks[index], dsize=(12, 64, 96))
    heatmap = crop_to_center(heatmap, list_landmarks[index], dsize=(12, 64, 96))
    Mask = crop_to_center(Mask, list_landmarks[index], dsize=(12, 64, 96))

    return [np.concatenate((MR, heatmap)), Mask]
Example #2
0
def crop_to_center(img, landmark=(0, 0, 0), dsize=(12, 128, 128)):
    """
    :param img 4D image with shape (C, D, H, W)
    """
    _, D, H, W = img.shape
    # bz = max(landmark[0] - dsize[0] // 2, 0)
    # ez = min(bz + dsize[0], D)
    pad_h_1, pad_h_2, pad_w_1, pad_w_2 = 0, 0, 0, 0

    bh = landmark[1] - dsize[1] // 2
    eh = bh + dsize[1]
    if bh < 0:
        pad_h_1 = abs(bh)
        bh = 0
    if eh > H:
        pad_h_2 = eh - H
        eh = H

    bw = landmark[2] - dsize[2] // 2
    ew = bw + dsize[2]
    if bw < 0:
        pad_w_1 = abs(bw)
        bw = 0
    if ew > W:
        pad_w_2 = ew - W
        ew = W
    # img = crop(img, bz, ez, axis='z')
    img = crop(img, bh, eh, axis='y')
    img = crop(img, bw, ew, axis='x')
    img = np.pad(img,
                 ((0, 0), (0, 0), (pad_h_1, pad_h_2), (pad_w_1, pad_w_2)),
                 mode='constant',
                 constant_values=0)

    return img
Example #3
0
def pre_processing(dict_images):
    MR = dict_images['MR']
    MR = np.clip(MR / 2048, a_max=1, a_min=0)
    _, D, H, W = MR.shape

    heatmap_generator = HeatmapGenerator(image_size=(D, H, W),
                                         sigma=2.,
                                         spine_heatmap_sigma=20,
                                         scale_factor=3.,
                                         spine_heatmap_scale_factor=10.,
                                         normalize=True,
                                         size_sigma_factor=8,
                                         sigma_scale_factor=2,
                                         dtype=np.float32)
    spine_heatmap = heatmap_generator.generate_spine_heatmap(
        list_landmarks=dict_images['list_landmarks'])

    if D > 12:
        start = random.choice([i for i in range(D - 12 + 1)])
        MR = crop(MR, start=start, end=start + 12, axis='z')
        spine_heatmap = crop(spine_heatmap,
                             start=start,
                             end=start + 12,
                             axis='z')

    return [MR, spine_heatmap]
Example #4
0
def pre_processing(dict_images):

    MR = dict_images['MR']
    _, D, H, W = MR.shape
    MR = crop(MR, start=int(W / 4), end=-int(W / 4), axis='W')
    MR = normalize(MR)
    Mask = dict_images['Mask']
    Mask = crop(Mask, start=int(W / 4), end=-int(W / 4), axis='W')

    # raw_Mask = dict_images['raw_Mask']

    list_images = [MR, Mask]  # (C, D, H, W) or (C, z, y, x)

    return list_images
Example #5
0
def pre_processing(dict_images):

    MR = dict_images['MR']
    # MR = np.clip(MR, a_min=-1024)
    _, D, H, W = MR.shape
    MR = crop(MR, start=int(W / 4), end=-int(W / 4), axis='W')
    MR = normalize(MR)  # normalization
    # Mask = dict_images['Mask']

    list_images = [MR]

    return list_images
Example #6
0
def pre_processing(dict_images):
    MR = dict_images['MR']
    MR = np.clip(MR / 2048, a_max=1, a_min=0)
    _, D, H, W = MR.shape

    heatmap_generator = HeatmapGenerator(image_size=(D, H, W),
                                         sigma=2.,
                                         spine_heatmap_sigma=20,
                                         scale_factor=1.,
                                         normalize=True,
                                         size_sigma_factor=6,
                                         sigma_scale_factor=1,
                                         dtype=np.float32)
    spine_heatmap = heatmap_generator.generate_spine_heatmap(
        list_landmarks=dict_images['list_landmarks'])
    heatmaps = heatmap_generator.generate_heatmaps(
        list_landmarks=dict_images['list_landmarks'])
    centroid_coordinate = [
        round(i) for i in ndimage.center_of_mass(spine_heatmap)
    ]  # (0, z, y, x)

    start_x = centroid_coordinate[-1] - W // 4
    end_x = centroid_coordinate[-1] + W // 4
    MR = crop(MR, start=start_x, end=end_x, axis='x')
    spine_heatmap = crop(spine_heatmap, start=start_x, end=end_x, axis='x')
    heatmaps = crop(heatmaps, start_x, end=end_x, axis='x')

    if D > 12:
        start_z = random.choice([i for i in range(D - 12 + 1)])
        MR = crop(MR, start=start_z, end=start_z + 12, axis='z')
        spine_heatmap = crop(spine_heatmap,
                             start=start_z,
                             end=start_z + 12,
                             axis='z')
        heatmaps = crop(heatmaps, start_z, end=start_z + 12, axis='z')

    # FIXME crop patches
    start_y = random.choice((0, H // 4, H // 2))
    end_y = start_y + H // 2
    MR = crop(MR, start=start_y, end=end_y, axis='y')
    spine_heatmap = crop(spine_heatmap, start=start_y, end=end_y, axis='y')
    heatmaps = crop(heatmaps, start_y, end=end_y, axis='y')

    return [MR, spine_heatmap, heatmaps
            ]  # (1, 12, 256, 256), (1, 12, 256, 256), (19, 12, 256, 256)