예제 #1
0
def densecrf3d(image_path, pred_path, save_path):
    I1NII = nib.load(image_path)
    I1 = I1NII.get_data()
    PNII = nib.load(pred_path)
    P = PNII.get_data()

    # convert input to intenstiy range of [0, 255]
    I = np.asarray([I1], np.float32)
    I = np.transpose(I, [1, 2, 3, 0])
    I = I / I.max() * 255
    I = np.asarray(I, np.uint8)

    # probability map for each class
    P = 0.5 + (P - 0.5) * 1.0
    P = np.asarray([1.0 - P, P], np.float32)
    P = np.transpose(P, [1, 2, 3, 0])

    dense_crf_param = {}
    dense_crf_param['MaxIterations'] = 2.0
    dense_crf_param['PosW'] = 2.0
    dense_crf_param['PosRStd'] = 5
    dense_crf_param['PosCStd'] = 5
    dense_crf_param['PosZStd'] = 5
    dense_crf_param['BilateralW'] = 3.0
    dense_crf_param['BilateralRStd'] = 5.0
    dense_crf_param['BilateralCStd'] = 5.0
    dense_crf_param['BilateralZStd'] = 5.0
    dense_crf_param['ModalityNum'] = 1
    dense_crf_param['BilateralModsStds'] = (5.0,)

    lab = denseCRF3D.densecrf3d(I, P, dense_crf_param)
    # lab = denseCRF3D.densecrf3d(I[0:1], P[0:1], dense_crf_param)  for test one slice time.

    labNii = nib.Nifti1Image(lab, np.eye(4))
    nib.save(labNii, save_path)
예제 #2
0
def densecrf3d(I, P, param):
    """
    input parameters:
        I: a numpy array of shape [D, H, W, C], where C is the channel number
           type of I should be np.uint8, and the values are in [0, 255]
        P: a probability map of shape [D, H, W, L], where L is the number of classes
           type of P should be np.float32
        param: a tuple giving parameters of CRF. see the following two examples for details.
    """
    return denseCRF3D.densecrf3d(I, P, param)
예제 #3
0
        dense_crf_param['PosCStd'] = 10
        dense_crf_param['PosZStd'] = 10
        dense_crf_param['BilateralW'] = 30.0
        dense_crf_param['BilateralRStd'] = 100.0
        dense_crf_param['BilateralCStd'] = 100.0
        dense_crf_param['BilateralZStd'] = 100.0
        dense_crf_param['ModalityNum'] = 1
        dense_crf_param['BilateralModsStds'] = (50.0,)

        # run crf and get hard segmentation:
        P = np.repeat(seg_pred_np[:, :, :, np.newaxis], 2, axis=3)
        P[:, :, :, 0] = 1 - P[:, :, :, 1]

        vol_crf = np.uint8((vol_np - vol_np.min()) / (vol_np.max() - vol_np.min()) * 255)
        vol_crf = vol_crf[:,:,:,np.newaxis]
        seg_pred_crf_np = denseCRF3D.densecrf3d(vol_crf, P, dense_crf_param)

        # get raw hard segmentation
        seg_pred_np[seg_pred_np <= 0.5] = 0
        seg_pred_np[seg_pred_np > 0.5] = 1

        # save the image and segmentation visualizations
        s1 = plot_slides(vol_np)
        cv2.imwrite(os.path.join(save_dir, 'c{}_{}_vol.png'.format(i, j)), s1)
        s1 = plot_slides(seg_np)
        cv2.imwrite(os.path.join(save_dir, 'c{}_{}_seg.png'.format(i, j)), s1)
        s1 = plot_slides(seg_pred_np)
        cv2.imwrite(os.path.join(save_dir, 'c{}_{}_seg_pred.png'.format(i, j)), s1)
        s1 = plot_slides(seg_pred_crf_np)
        cv2.imwrite(os.path.join(save_dir, 'c{}_{}_seg_pred_crf.png'.format(i, j)), s1)