コード例 #1
0
def seg_target_lipiodol(P, thresholds=[75, 160, 215], num_tumors=1):
    raise ValueError("Not ready")
    ct_img, D = hf.nii_load(P["ct24"]["img"])
    mask, _ = masks.get_mask(seg, D, ct_img.shape)

    img = (mask > 0) * ct_img
    #save_folder =
    info[dirname(seg)] = (ct_img, D, mask)

    if P["ct24"]["tumor"] is None:
        tumor_mask = img > thresholds[1]
        tumor_mask = binary_closing(tumor_mask)
    else:
        tumor_mask = masks.get_mask(P["ct24"]["tumor"], D, ct_img.shape)

    mask1 = copy.deepcopy(img)
    mask1 = mask1 > thresholds[1]
    B2 = ball(2)
    B2 = B2[:, :, [0, 2, 4]]
    mask1 = binary_dilation(mask1, np.ones((3, 3, 1)))

    if num_tumors == 1:
        tumor_labels, num_labels = label(mask1, return_num=True)
        label_sizes = [
            np.sum(tumor_labels == label_id)
            for label_id in range(1, num_labels + 1)
        ]
        biggest_label = label_sizes.index(max(label_sizes)) + 1
        mask1[tumor_labels != biggest_label] = 0

    mask1 = binary_opening(binary_closing(mask1, structure=B2, iterations=2),
                           structure=B2,
                           iterations=2)

    if num_tumors == 1:
        tumor_labels, num_labels = label(mask1, return_num=True)
        label_sizes = [
            np.sum(tumor_labels == label_id)
            for label_id in range(1, num_labels + 1)
        ]
        biggest_label = label_sizes.index(max(label_sizes)) + 1
        mask1[tumor_labels != biggest_label] = 0

    target_mask = mask1 * tumor_mask
    nontarget_mask = (1 - mask1) * tumor_mask

    masks.save_mask(target_mask,
                    join(P["mask"], "target_lip"),
                    D,
                    save_mesh=True)
    masks.save_mask(nontarget_mask,
                    join(P["mask"], "nontarget_lip"),
                    D,
                    save_mesh=True)
コード例 #2
0
def seg_liver_mri(mri_img, save_path, mri_dims, model, tumor_mask_path=None):
    """Use a UNet to segment liver on MRI"""

    C = config.Config()
    #correct bias field!

    orig_shape = mri_img.shape

    x = mri_img
    x -= np.amin(x)
    x /= np.std(x)

    crops = list(map(int,[.05 * x.shape[0], .95 * x.shape[0]] + \
        [.05 * x.shape[1], .95 * x.shape[1]] + \
        [.05 * x.shape[2], .95 * x.shape[2]]))

    x = x[crops[0]:crops[1], crops[2]:crops[3], crops[4]:crops[5]]
    scale_shape = x.shape
    x = tr.rescale_img(x, C.dims)

    y = model.predict(np.expand_dims(x, 0))[0]
    liver_mask = (y[:, :, :, 1] > y[:, :, :, 0]).astype(float)
    liver_mask = tr.rescale_img(liver_mask, scale_shape)  #orig_shape)
    liver_mask = np.pad(liver_mask, ((crops[0], orig_shape[0] - crops[1]),
                                     (crops[2], orig_shape[1] - crops[3]),
                                     (crops[4], orig_shape[2] - crops[5])),
                        'constant')
    liver_mask = liver_mask > .5

    B3 = ball(3)
    B3 = B3[:, :, [0, 2, 3, 4, 6]]
    #B3 = ball(4)
    #B3 = B3[:,:,[1,3,5,6,8]]
    liver_mask = binary_opening(binary_closing(liver_mask, B3, 1), B3, 1)

    labels, num_labels = label(liver_mask, return_num=True)
    label_sizes = [
        np.sum(labels == label_id) for label_id in range(1, num_labels + 1)
    ]
    biggest_label = label_sizes.index(max(label_sizes)) + 1
    liver_mask[labels != biggest_label] = 0

    if tumor_mask_path is not None:
        tumor_mask, _ = masks.get_mask(tumor_mask_path, mri_dims,
                                       mri_img.shape)
        liver_mask[tumor_mask > tumor_mask.max() / 2] = liver_mask.max()

    masks.save_mask(liver_mask, save_path, mri_dims, save_mesh=True)
コード例 #3
0
def seg_liver_ct(ct_path, save_path, model, tumor_mask_path=None):
    """Use a UNet to segment liver on CT"""

    C = config.Config()
    ct_img, ct_dims = hf.nii_load(ct_path)

    orig_shape = ct_img.shape

    x = tr.apply_window(ct_img)
    x -= np.amin(x)

    crops = list(map(int,[.05 * x.shape[0], .95 * x.shape[0]] + \
        [.05 * x.shape[1], .95 * x.shape[1]] + \
        [.05 * x.shape[2], .95 * x.shape[2]]))

    x = x[crops[0]:crops[1], crops[2]:crops[3], crops[4]:crops[5]]
    scale_shape = x.shape
    x = tr.rescale_img(x, C.dims)

    y = model.predict(np.expand_dims(x, 0))[0]
    liver_mask = (y[:, :, :, 1] > y[:, :, :, 0]).astype(float)
    liver_mask[x < 30] = 0
    liver_mask = tr.rescale_img(liver_mask, scale_shape)
    liver_mask = np.pad(liver_mask, ((crops[0], orig_shape[0] - crops[1]),
                                     (crops[2], orig_shape[1] - crops[3]),
                                     (crops[4], orig_shape[2] - crops[5])),
                        'constant')

    B3 = ball(3)
    B3 = B3[:, :, [0, 2, 3, 4, 6]]
    liver_mask = binary_opening(binary_closing(liver_mask, B3, 1), B3, 1)

    labels, num_labels = label(liver_mask, return_num=True)
    label_sizes = [
        np.sum(labels == label_id) for label_id in range(1, num_labels + 1)
    ]
    biggest_label = label_sizes.index(max(label_sizes)) + 1
    liver_mask[labels != biggest_label] = 0

    if tumor_mask_path is not None:
        tumor_mask, _ = masks.get_mask(tumor_mask_path, ct_dims, ct_img.shape)
        liver_mask[tumor_mask > tumor_mask.max() / 2] = liver_mask.max()

    masks.save_mask(liver_mask, save_path, ct_dims, save_mesh=True)
コード例 #4
0
def seg_lipiodol(P, thresholds=[75, 160, 215]):
    #P = get_paths_dict(lesion_id, target_dir)

    img, dims = hf.nii_load(P['ct24']['img'])

    low_mask = copy.deepcopy(img)
    low_mask = low_mask > thresholds[0]
    low_mask = binary_closing(binary_opening(low_mask,
                                             structure=np.ones((3, 3, 2))),
                              structure=np.ones((2, 2, 1)))  #2,2,1
    mid_mask = copy.deepcopy(img)
    mid_mask = mid_mask > thresholds[1]
    mid_mask = binary_closing(mid_mask)
    high_mask = copy.deepcopy(img)
    high_mask = high_mask > thresholds[2]

    mask, _ = masks.get_mask(P['ct24']['liver'], dims, img.shape)
    low_mask = low_mask * mask
    mid_mask = mid_mask * mask
    high_mask = high_mask * mask

    masks.save_mask(low_mask, P['ct24']['lowlip'], dims, save_mesh=True)
    masks.save_mask(mid_mask, P['ct24']['midlip'], dims, save_mesh=True)
    masks.save_mask(high_mask, P['ct24']['highlip'], dims, save_mesh=True)

    return low_mask, mid_mask, high_mask
コード例 #5
0
def seg_tumor_ct(P, threshold=150, num_tumors=1):
    ct_img, D = hf.nii_load(P["ct24"]["img"])
    mask, _ = masks.get_mask(P["ct24"]["liver"], D, ct_img.shape)

    tumor_mask = (mask > 0) * ct_img > threshold
    tumor_mask = binary_closing(tumor_mask)

    if num_tumors == 1:
        tumor_labels, num_labels = label(tumor_mask, return_num=True)
        label_sizes = [
            np.sum(tumor_labels == label_id)
            for label_id in range(1, num_labels + 1)
        ]
        biggest_label = label_sizes.index(max(label_sizes)) + 1
        tumor_mask[tumor_labels != biggest_label] = 0

    B3 = ball(3)
    B3 = B3[:, :, [0, 2, 3, 4, 6]]
    tumor_mask = binary_opening(binary_closing(mask1, B3), B3)

    masks.save_mask(tumor_mask, P["ct24"]["tumor"], D)

    return tumor_mask
コード例 #6
0
def reg_to_ct24(lesion_id, target_dir, D=[1., 1., 2.5], padding=.2):
    importlib.reload(reg)
    P = get_paths_dict(lesion_id, target_dir)

    ct24, ct24_dims = hf.nii_load(P['ct24']['img'])
    fmod = 'ct24'

    mod = 'mrbl'
    xform_path, crops, pad_m = reg.get_mask_Tx(P[fmod]['img'],
                                               P[fmod]['tumor'],
                                               P[mod]['art'],
                                               P[mod]['tumor'],
                                               padding=padding,
                                               D=D)

    crop_ct24 = hf.crop_nonzero(ct24, crops[1])[0]
    t_shape = crop_ct24.shape
    reg.transform_region(P[mod]['art'],
                         xform_path,
                         crops,
                         pad_m,
                         ct24_dims,
                         P['ct24Tx'][mod]['art'],
                         target_shape=t_shape,
                         D=D)
    reg.transform_region(P[mod]['sub'],
                         xform_path,
                         crops,
                         pad_m,
                         ct24_dims,
                         P['ct24Tx'][mod]['sub'],
                         target_shape=t_shape,
                         D=D)
    #reg.transform_region(P[mod]['equ'], xform_path, crops, pad_m, ct24_dims,
    #			P['ct24Tx'][mod]['equ'], target_shape=t_shape, D=D);
    if exists(P[mod]['enh'] + ".off"):
        reg.transform_mask(P[mod]['enh'],
                           P[mod]['art'],
                           xform_path,
                           crops,
                           pad_m,
                           ct24_dims,
                           P['ct24Tx'][mod]['enh'],
                           target_shape=t_shape,
                           D=D)

    hf.save_nii(crop_ct24, P['ct24Tx']['crop']['img'], ct24_dims)
    M = masks.get_mask(P['ct24']['tumor'], ct24_dims, ct24.shape)[0]
    M = hf.crop_nonzero(M, crops[1])[0]
    masks.save_mask(M, P['ct24Tx']['crop']['tumor'], ct24_dims)

    mod = 'mr30'
    xform_path, crops, pad_m = reg.get_mask_Tx(P[fmod]['img'],
                                               P[fmod]['tumor'],
                                               P[mod]['art'],
                                               P[mod]['tumor'],
                                               padding=padding,
                                               D=D)

    reg.transform_region(P[mod]['art'],
                         xform_path,
                         crops,
                         pad_m,
                         ct24_dims,
                         P['ct24Tx'][mod]['art'],
                         target_shape=t_shape,
                         D=D)
    if exists(P[mod]['enh'] + ".off"):
        reg.transform_mask(P[mod]['enh'],
                           P[mod]['art'],
                           xform_path,
                           crops,
                           pad_m,
                           ct24_dims,
                           P['ct24Tx'][mod]['enh'],
                           target_shape=t_shape,
                           D=D)