Skip to content

Commit c2600eb

Browse files
committed
update examples to supoort python 2
1 parent 3e52742 commit c2600eb

File tree

4 files changed

+94
-12
lines changed

4 files changed

+94
-12
lines changed

examples/demo_densecrf.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,26 @@
33
from PIL import Image
44
import matplotlib.pyplot as plt
55

6+
def densecrf(I, P, param):
7+
"""
8+
input parameters:
9+
I : a numpy array of shape [H, W, C], where C should be 3.
10+
type of I should be np.uint8, and the values are in [0, 255]
11+
P : a probability map of shape [H, W, L], where L is the number of classes
12+
type of P should be np.float32
13+
param: a tuple giving parameters of CRF (w1, alpha, beta, w2, gamma, it), where
14+
w1 : weight of bilateral term, e.g. 10.0
15+
alpha : spatial distance std, e.g., 80
16+
beta : rgb value std, e.g., 15
17+
w2 : weight of spatial term, e.g., 3.0
18+
gamma : spatial distance std for spatial term, e.g., 3
19+
it : iteration number, e.g., 5
20+
output parameters:
21+
out : a numpy array of shape [H, W], where pixel values represent class indices.
22+
"""
23+
out = denseCRF.densecrf(I, P, param)
24+
return out
25+
626
def convert_label_to_probability_map(label, color_list):
727
[H, W, _] = label.shape
828
C = len(color_list)
@@ -50,7 +70,7 @@ def demo_densecrf1():
5070
gamma = 3 # spatial std
5171
it = 5.0 # iteration
5272
param = (w1, alpha, beta, w2, gamma, it)
53-
lab = denseCRF.densecrf(Iq, prob, param)
73+
lab = densecrf(Iq, prob, param)
5474
lab = Image.fromarray(lab*255)
5575
plt.subplot(1,3,1); plt.axis('off'); plt.imshow(I); plt.title('input image')
5676
plt.subplot(1,3,2); plt.axis('off'); plt.imshow(L); plt.title('initial label')
@@ -76,7 +96,7 @@ def demo_densecrf2():
7696
gamma = 3 # spatial std
7797
it = 5.0 # iteration
7898
param = (w1, alpha, beta, w2, gamma, it)
79-
lab = denseCRF.densecrf(Iq, prob, param)
99+
lab = densecrf(Iq, prob, param)
80100
lab = colorize_label_map(lab, color_list)
81101
lab = Image.fromarray(lab)
82102
plt.subplot(1,3,1); plt.axis('off'); plt.imshow(I); plt.title('input image')
@@ -90,6 +110,7 @@ def demo_densecrf2():
90110
print(" 1 -- Dense CRF example for a multi-class segmentation")
91111
print("please enter the index of an example:")
92112
method = input()
113+
method = "{0:}".format(method)
93114
if(method == '0'):
94115
demo_densecrf1()
95116
elif(method == '1'):

examples/demo_densecrf3d.py

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,52 @@
44
from PIL import Image
55
import matplotlib.pyplot as plt
66

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():
853
data_root = '../dependency/densecrf3d/applicationAndExamples/example/'
954
I1Nii = nibabel.load(data_root + 'Flair_normalized.nii.gz')
1055
I2Nii = nibabel.load(data_root + 'DWI_normalized.nii.gz')
@@ -36,14 +81,24 @@ def demo_densecrf3d():
3681
dense_crf_param['BilateralCStd'] = 5.0
3782
dense_crf_param['BilateralZStd'] = 5.0
3883
dense_crf_param['ModalityNum'] = 2
39-
dense_crf_param['BilateralModsStds'] = (5.0,5.0)
84+
dense_crf_param['BilateralModsStds'] = (5.0, 5.0)
4085

41-
lab = denseCRF3D.densecrf3d(I, P, dense_crf_param)
86+
lab = densecrf3d(I, P, dense_crf_param)
4287
labNii = nibabel.Nifti1Image(lab, np.eye(4))
4388
nibabel.save(labNii, data_root + 'results/lesionSegMap.nii.gz')
4489

4590
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))
48103

49104

examples/demo_maxflow.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def test_interactive_max_flow3d():
106106
print(" 3 -- 3D max flow with interactions")
107107
print("please enter the index of an example:")
108108
method = input()
109+
method = "{0:}".format(method)
109110
if(method == '0'):
110111
demo_maxflow()
111112
elif(method == '1'):

setup.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,28 @@
6060
description = 'An open-source toolkit for conditional random field (CRF) and dense CRF'
6161

6262
# Get the long description
63-
with open('README.md', encoding='utf-8') as f:
64-
long_description = f.read()
63+
if(sys.version[0] == '2'):
64+
import io
65+
with io.open('README.md', 'r', encoding='utf-8') as f:
66+
long_description = f.read()
67+
else:
68+
with open('README.md', encoding='utf-8') as f:
69+
long_description = f.read()
6570

6671

6772
setup(name=package_name,
68-
version = "0.0.1",
73+
version = "0.0.5",
6974
author ='Guotai Wang',
7075
author_email = '[email protected]',
7176
description = description,
7277
long_description = long_description,
7378
long_description_content_type = 'text/markdown',
7479
url = 'https://github.com/taigw/SimpleCRF',
75-
license = 'MIT',
80+
license = 'BSD',
7681
packages = setuptools.find_packages(),
7782
ext_modules = [module1, module2, module3],
7883
classifiers=[
79-
'License :: OSI Approved :: MIT License',
84+
'License :: OSI Approved :: BSD License',
8085
'Programming Language :: Python',
8186
'Programming Language :: Python :: 2',
8287
'Programming Language :: Python :: 3',

0 commit comments

Comments
 (0)