Skip to content

Commit ca46f6b

Browse files
committed
enable multiple inpu channels
1 parent 037f429 commit ca46f6b

File tree

6 files changed

+417
-900
lines changed

6 files changed

+417
-900
lines changed

examples/demo_maxflow.py

+96-13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,82 @@
55
from PIL import Image
66
import matplotlib.pyplot as plt
77

8+
def maxflow2d(I, P, param):
9+
"""
10+
maxflow for 2D image segmentation. Only binary segmentation is supported.
11+
input parameters:
12+
I : a numpy array of shape [H, W], or [H, W, C] where C should be 3.
13+
type of I should be np.float32
14+
P : a probability map of shape [H, W, L], where L==2 is the number of classes
15+
type of P should be np.float32
16+
param: a tuple giving parameters of CRF (lambda, sigma), where
17+
lambda : weight of smooth term, e.g. 20.0
18+
sigma : std intensity value, e.g., 10
19+
20+
output parameters:
21+
lab : a numpy array of shape [H, W], where pixel values represent class indices.
22+
"""
23+
lab = maxflow.maxflow2d(I, P, param)
24+
return lab
25+
26+
def interactive_maxflow2d(I, P, S, param):
27+
"""
28+
maxflow for 2D interactive image segmentation. Only binary segmentation is supported.
29+
input parameters:
30+
I : a numpy array of shape [H, W], or [H, W, C] where C should be 3.
31+
type of I should be np.float32
32+
P : a probability map of shape [H, W, L], where L==2 is the number of classes
33+
type of P should be np.float32
34+
S : a numpy array showing user interactions, shape should be [H, W, L], where L == 2
35+
type of S should be np.uint8
36+
param: a tuple giving parameters of CRF (lambda, sigma), where
37+
lambda : weight of smooth term, e.g. 20.0
38+
sigma : std intensity value, e.g., 10
39+
40+
output parameters:
41+
lab : a numpy array of shape [H, W], where pixel values represent class indices.
42+
"""
43+
lab = maxflow.interactive_maxflow2d(I, P, S, param)
44+
return lab
45+
46+
def maxflow3d(I, P, param):
47+
"""
48+
maxflow for 3D image segmentation. Only binary segmentation is supported.
49+
input parameters:
50+
I : a numpy array of shape [D, H, W], or [D, H, W, C] where C should be 3.
51+
type of I should be np.float32
52+
P : a probability map of shape [D, H, W, L], where L==2 is the number of classes
53+
type of P should be np.float32
54+
param: a tuple giving parameters of CRF (lambda, sigma), where
55+
lambda : weight of smooth term, e.g. 20.0
56+
sigma : std intensity value, e.g., 10
57+
58+
output parameters:
59+
lab : a numpy array of shape [D, H, W], where pixel values represent class indices.
60+
"""
61+
lab = maxflow.maxflow3d(I, P, param)
62+
return lab
63+
64+
def interactive_maxflow3d(I, P, S, param):
65+
"""
66+
maxflow for 3D interactive image segmentation. Only binary segmentation is supported.
67+
input parameters:
68+
I : a numpy array of shape [D, H, W], or [D, H, W, C] where C should be 3.
69+
type of I should be np.float32
70+
P : a probability map of shape [D, H, W, L], where L==2 is the number of classes
71+
type of P should be np.float32
72+
S : a numpy array showing user interactions, shape should be [D, H, W, L], where L == 2
73+
type of S should be np.uint8
74+
param: a tuple giving parameters of CRF (lambda, sigma), where
75+
lambda : weight of smooth term, e.g. 20.0
76+
sigma : std intensity value, e.g., 10
77+
78+
output parameters:
79+
lab : a numpy array of shape [D, H, W], where pixel values represent class indices.
80+
"""
81+
lab = maxflow.interactive_maxflow3d(I, P, S, param)
82+
return lab
83+
884
def demo_maxflow():
985
I = Image.open('../data/brain.png')
1086
Iq = np.asarray(I.convert('L'), np.float32)
@@ -13,10 +89,12 @@ def demo_maxflow():
1389

1490
fP = 0.5 + (P - 0.5) * 0.8
1591
bP = 1.0 - fP
92+
Prob = np.asarray([bP, fP])
93+
Prob = np.transpose(Prob, [1, 2, 0])
1694
lamda = 20.0
1795
sigma = 10.0
1896
param = (lamda, sigma)
19-
lab = maxflow.maxflow2d(Iq, fP, bP, param)
97+
lab = maxflow2d(Iq, Prob, param)
2098

2199
plt.subplot(1,3,1); plt.axis('off'); plt.imshow(I); plt.title('input image')
22100
plt.subplot(1,3,2); plt.axis('off'); plt.imshow(fP); plt.title('initial \n segmentation')
@@ -31,16 +109,17 @@ def demo_interactive_maxflow():
31109

32110
fP = 0.5 + (P - 0.5) * 0.8
33111
bP = 1.0 - fP
112+
Prob = np.asarray([bP, fP])
113+
Prob = np.transpose(Prob, [1, 2, 0])
34114

35-
S = np.asarray(Image.open('../data/brain_scrb.png').convert('L'))
36-
scrb = np.zeros_like(S)
37-
scrb[S==170] = 127
38-
scrb[S==255] = 255
115+
S = np.asarray(Image.open('../data/brain_scrb.png').convert('L'))
116+
Seed = np.asarray([S == 255, S == 170], np.uint8)
117+
Seed = np.transpose(Seed, [1, 2, 0])
39118

40119
lamda = 30.0
41120
sigma = 8.0
42121
param = (lamda, sigma)
43-
lab = maxflow.interactive_maxflow2d(Iq, fP, bP, scrb, param)
122+
lab = interactive_maxflow2d(Iq, Prob, Seed, param)
44123

45124
plt.subplot(1,3,1); plt.axis('off'); plt.imshow(I); plt.title('input image')
46125
plt.subplot(1,3,2); plt.axis('off'); plt.imshow(fP); plt.title('initial \n segmentation')
@@ -59,12 +138,15 @@ def demo_maxflow3d():
59138
prob_data = np.asarray(prob_data, np.float32)
60139

61140
fP = 0.5 + (prob_data - 0.5) * 0.8
62-
bP = 1.0-fP
141+
bP = 1.0 - fP
142+
Prob = np.asarray([bP, fP])
143+
Prob = np.transpose(Prob, [1, 2, 3, 0])
63144

64145
lamda = 10.0
65146
sigma = 15.0
66147
param = (lamda, sigma)
67-
lab = maxflow.maxflow3d(img_data, fP, bP, param)
148+
print('image data shape', img_data.shape)
149+
lab = maxflow3d(img_data, Prob, param)
68150
lab_obj = sitk.GetImageFromArray(lab)
69151
lab_obj.CopyInformation(img_obj)
70152
sitk.WriteImage(lab_obj, save_name)
@@ -83,18 +165,19 @@ def test_interactive_max_flow3d():
83165
prob_data = np.asarray(prob_data, np.float32)
84166

85167
fP = 0.5 + (prob_data - 0.5) * 0.8
86-
bP = 1.0-fP
168+
bP = 1.0 - fP
169+
Prob = np.asarray([bP, fP])
170+
Prob = np.transpose(Prob, [1, 2, 3, 0])
87171

88172
seed_obj = sitk.ReadImage(seed_name)
89173
seed_data = sitk.GetArrayFromImage(seed_obj)
90-
seed_data[seed_data == 1] = 127
91-
seed_data[seed_data == 2] = 255
92-
seed_data = np.asarray(seed_data, np.uint8)
174+
Seed = np.asarray([seed_data == 2, seed_data == 3], np.uint8)
175+
Seed = np.transpose(Seed, [1, 2, 3, 0])
93176

94177
lamda = 10.0
95178
sigma = 15.0
96179
param = (lamda, sigma)
97-
lab = maxflow.interactive_maxflow3d(img_data, fP, bP, seed_data, param)
180+
lab = interactive_maxflow3d(img_data, Prob, Seed, param)
98181
lab_obj = sitk.GetImageFromArray(lab)
99182
lab_obj.CopyInformation(img_obj)
100183
sitk.WriteImage(lab_obj, save_name)

0 commit comments

Comments
 (0)