|
4 | 4 | from PIL import Image
|
5 | 5 | import matplotlib.pyplot as plt
|
6 | 6 |
|
7 |
| -def demo_densecrf3d(): |
| 7 | +def densecrf3d(I, P, param): |
| 8 | + """ |
| 9 | + input parameters: |
| 10 | + I: a numpy array of shape [D, H, W, C], where C is the channel number |
| 11 | + type of I should be np.uint8, and the values are in [0, 255] |
| 12 | + P: a probability map of shape [D, H, W, L], where L is the number of classes |
| 13 | + type of P should be np.float32 |
| 14 | + param: a tuple giving parameters of CRF. see the following two examples for details. |
| 15 | + """ |
| 16 | + return denseCRF3D.densecrf3d(I, P, param) |
| 17 | + |
| 18 | +def demo_densecrf3d_1(): |
| 19 | + I1Nii = nibabel.load('../data/2013_12_1_img.nii.gz') |
| 20 | + PNii = nibabel.load('../data/2013_12_1_init.nii.gz') |
| 21 | + I1 = I1Nii.get_data() |
| 22 | + P = PNii.get_data() |
| 23 | + |
| 24 | + # convert input to intenstiy range of [0, 255] |
| 25 | + I = np.asarray([I1], np.float32) |
| 26 | + I = np.transpose(I, [1, 2, 3, 0]) |
| 27 | + I = I / I.max()* 255 |
| 28 | + I = np.asarray(I, np.uint8) |
| 29 | + |
| 30 | + # probability map for each class |
| 31 | + P = 0.5 + (P - 0.5) * 0.8 |
| 32 | + P = np.asarray([1.0 - P, P], np.float32) |
| 33 | + P = np.transpose(P, [1, 2, 3, 0]) |
| 34 | + |
| 35 | + dense_crf_param = {} |
| 36 | + dense_crf_param['MaxIterations'] = 2.0 |
| 37 | + dense_crf_param['PosW'] = 2.0 |
| 38 | + dense_crf_param['PosRStd'] = 5 |
| 39 | + dense_crf_param['PosCStd'] = 5 |
| 40 | + dense_crf_param['PosZStd'] = 5 |
| 41 | + dense_crf_param['BilateralW'] = 3.0 |
| 42 | + dense_crf_param['BilateralRStd'] = 5.0 |
| 43 | + dense_crf_param['BilateralCStd'] = 5.0 |
| 44 | + dense_crf_param['BilateralZStd'] = 5.0 |
| 45 | + dense_crf_param['ModalityNum'] = 1 |
| 46 | + dense_crf_param['BilateralModsStds'] = (5.0,) |
| 47 | + |
| 48 | + lab = densecrf3d(I, P, dense_crf_param) |
| 49 | + labNii = nibabel.Nifti1Image(lab, np.eye(4)) |
| 50 | + nibabel.save(labNii, '../data/seg_densecrf.nii.gz') |
| 51 | + |
| 52 | +def demo_densecrf3d_2(): |
8 | 53 | data_root = '../dependency/densecrf3d/applicationAndExamples/example/'
|
9 | 54 | I1Nii = nibabel.load(data_root + 'Flair_normalized.nii.gz')
|
10 | 55 | I2Nii = nibabel.load(data_root + 'DWI_normalized.nii.gz')
|
@@ -36,14 +81,24 @@ def demo_densecrf3d():
|
36 | 81 | dense_crf_param['BilateralCStd'] = 5.0
|
37 | 82 | dense_crf_param['BilateralZStd'] = 5.0
|
38 | 83 | dense_crf_param['ModalityNum'] = 2
|
39 |
| - dense_crf_param['BilateralModsStds'] = (5.0,5.0) |
| 84 | + dense_crf_param['BilateralModsStds'] = (5.0, 5.0) |
40 | 85 |
|
41 |
| - lab = denseCRF3D.densecrf3d(I, P, dense_crf_param) |
| 86 | + lab = densecrf3d(I, P, dense_crf_param) |
42 | 87 | labNii = nibabel.Nifti1Image(lab, np.eye(4))
|
43 | 88 | nibabel.save(labNii, data_root + 'results/lesionSegMap.nii.gz')
|
44 | 89 |
|
45 | 90 | if __name__ == "__main__":
|
46 |
| - demo_densecrf3d() |
47 |
| - |
| 91 | + print("example list") |
| 92 | + print(" 0 -- 3D Dense CRF example for single-modal segmentation") |
| 93 | + print(" 1 -- 3D Dense CRF example for multi-modal segmentation") |
| 94 | + print("please enter the index of an example:") |
| 95 | + method = input() |
| 96 | + method = "{0:}".format(method) |
| 97 | + if(method == '0'): |
| 98 | + demo_densecrf3d_1() |
| 99 | + elif(method == '1'): |
| 100 | + demo_densecrf3d_2() |
| 101 | + else: |
| 102 | + print("invalid number : {0:}".format(method)) |
48 | 103 |
|
49 | 104 |
|
0 commit comments