def draw_mrseq_with_mask(lesion_id, target_dir, save_dir, mod='mrbl'): importlib.reload(masks) P = lm.get_paths_dict(lesion_id, target_dir) out_img = [] art, C = masks.crop_img_to_mask_vicinity(P[mod]['art'], P[mod]['tumor'], .5, return_crops=True) pre = masks.crop_img_to_mask_vicinity(P[mod]['pre'], P[mod]['tumor'], .5) equ = masks.crop_img_to_mask_vicinity(P[mod]['equ'], P[mod]['tumor'], .5) sub = art - pre sl = art.shape[-1] // 2 I, D = hf.nii_load(P[mod]['art']) if exists(P[mod]['enh'] + ".off"): mask = masks.get_mask(P[mod]['enh'], D, I.shape) mask = hf.crop_nonzero(mask, C)[0] else: mask = np.zeros(art.shape) tumor_mask = masks.get_mask(P[mod]['tumor'], D, I.shape) tumor_mask = hf.crop_nonzero(tumor_mask, C)[0] sub_w_mask = vis.create_contour_img(sub, [tumor_mask, mask]) vis.display_sequence([ pre[..., sl], art[..., sl], equ[..., sl], sub[..., sl], sub_w_mask, mask[..., sl] ], 2, 3, join(save_dir, "%s_%s.png" % (lesion_id, mod)))
def spherize(lesion_id, target_dir, R=1.): importlib.reload(reg) def ball_ct_batch(): reg.transform_region(P['ct24']['img'], xform_path, crops, pads, [R] * 3, P['ball']['ct24']['img'], intermed_shape=ball_shape) #if exists(P['ct24']['midlip']+".off"): # reg.transform_mask(P['ct24']['midlip'], P['ct24']['img'], xform_path, # crops, pads, [R]*3, P['ball']['ct24']['midlip'], intermed_shape=ball_shape) # if exists(P['ct24']['highlip']+".off"): # reg.transform_mask(P['ct24']['highlip'], P['ct24']['img'], xform_path, # crops, pads, [R]*3, P['ball']['ct24']['highlip'], intermed_shape=ball_shape) def ball_mr_batch(mod): reg.transform_region(P[mod]['art'], xform_path, crops, pads, [R] * 3, P['ball'][mod]['art'], intermed_shape=ball_shape) if exists(P['ball'][mod]['enh'] + ".off"): reg.transform_mask(P[mod]['enh'], P[mod]['art'], xform_path, crops, pads, [R] * 3, P['ball'][mod]['enh'], intermed_shape=ball_shape) P = get_paths_dict(lesion_id, target_dir) ctmask, ctd = masks.get_mask(P['ct24']['tumor'], img_path=P['ct24']['img']) mrmask, mrd = masks.get_mask(P['mrbl']['tumor'], img_path=P['mrbl']['art']) ctmask = hf.crop_nonzero(ctmask)[0] mrmask = hf.crop_nonzero(mrmask)[0] CT = np.max([ctmask.shape[i] * ctd[i] for i in range(3)]) MRBL = np.max([mrmask.shape[i] * mrd[i] for i in range(3)]) mrmask, mrd = masks.get_mask(P['mr30']['tumor'], img_path=P['mr30']['art']) mrmask = hf.crop_nonzero(mrmask)[0] MR30 = np.max([mrmask.shape[i] * mrd[i] for i in range(3)]) if CT > MRBL and CT > MR30: xform_path, crops, pads = reg.get_mask_Tx_shape( P['ct24']['img'], P['ct24']['tumor'], mask_path=P['ball']['mask']) ball_shape = masks.get_mask(P['ball']['mask'])[0].shape ball_ct_batch() xform_path, crops, pads = reg.get_mask_Tx_shape( P['mrbl']['art'], P['mrbl']['tumor'], ball_mask_path=P['ball']['mask']) ball_mr_batch('mrbl') xform_path, crops, pads = reg.get_mask_Tx_shape( P['mr30']['art'], P['mr30']['tumor'], ball_mask_path=P['ball']['mask']) ball_mr_batch('mr30') elif MRBL > MR30: xform_path, crops, pads = reg.get_mask_Tx_shape( P['mrbl']['art'], P['mrbl']['tumor'], mask_path=P['ball']['mask']) ball_shape = masks.get_mask(P['ball']['mask'])[0].shape ball_mr_batch('mrbl') xform_path, crops, pads = reg.get_mask_Tx_shape( P['ct24']['img'], P['ct24']['tumor'], ball_mask_path=P['ball']['mask']) ball_ct_batch() xform_path, crops, pads = reg.get_mask_Tx_shape( P['mr30']['art'], P['mr30']['tumor'], ball_mask_path=P['ball']['mask']) ball_mr_batch('mr30') else: xform_path, crops, pads = reg.get_mask_Tx_shape( P['mr30']['art'], P['mr30']['tumor'], mask_path=P['ball']['mask']) ball_shape = masks.get_mask(P['ball']['mask'])[0].shape ball_mr_batch('mr30') xform_path, crops, pads = reg.get_mask_Tx_shape( P['mrbl']['art'], P['mrbl']['tumor'], ball_mask_path=P['ball']['mask']) ball_mr_batch('mrbl') xform_path, crops, pads = reg.get_mask_Tx_shape( P['ct24']['img'], P['ct24']['tumor'], ball_mask_path=P['ball']['mask']) ball_ct_batch()
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)