5
5
from PIL import Image
6
6
import matplotlib .pyplot as plt
7
7
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
+
8
84
def demo_maxflow ():
9
85
I = Image .open ('../data/brain.png' )
10
86
Iq = np .asarray (I .convert ('L' ), np .float32 )
@@ -13,10 +89,12 @@ def demo_maxflow():
13
89
14
90
fP = 0.5 + (P - 0.5 ) * 0.8
15
91
bP = 1.0 - fP
92
+ Prob = np .asarray ([bP , fP ])
93
+ Prob = np .transpose (Prob , [1 , 2 , 0 ])
16
94
lamda = 20.0
17
95
sigma = 10.0
18
96
param = (lamda , sigma )
19
- lab = maxflow . maxflow2d (Iq , fP , bP , param )
97
+ lab = maxflow2d (Iq , Prob , param )
20
98
21
99
plt .subplot (1 ,3 ,1 ); plt .axis ('off' ); plt .imshow (I ); plt .title ('input image' )
22
100
plt .subplot (1 ,3 ,2 ); plt .axis ('off' ); plt .imshow (fP ); plt .title ('initial \n segmentation' )
@@ -31,16 +109,17 @@ def demo_interactive_maxflow():
31
109
32
110
fP = 0.5 + (P - 0.5 ) * 0.8
33
111
bP = 1.0 - fP
112
+ Prob = np .asarray ([bP , fP ])
113
+ Prob = np .transpose (Prob , [1 , 2 , 0 ])
34
114
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 ])
39
118
40
119
lamda = 30.0
41
120
sigma = 8.0
42
121
param = (lamda , sigma )
43
- lab = maxflow . interactive_maxflow2d (Iq , fP , bP , scrb , param )
122
+ lab = interactive_maxflow2d (Iq , Prob , Seed , param )
44
123
45
124
plt .subplot (1 ,3 ,1 ); plt .axis ('off' ); plt .imshow (I ); plt .title ('input image' )
46
125
plt .subplot (1 ,3 ,2 ); plt .axis ('off' ); plt .imshow (fP ); plt .title ('initial \n segmentation' )
@@ -59,12 +138,15 @@ def demo_maxflow3d():
59
138
prob_data = np .asarray (prob_data , np .float32 )
60
139
61
140
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 ])
63
144
64
145
lamda = 10.0
65
146
sigma = 15.0
66
147
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 )
68
150
lab_obj = sitk .GetImageFromArray (lab )
69
151
lab_obj .CopyInformation (img_obj )
70
152
sitk .WriteImage (lab_obj , save_name )
@@ -83,18 +165,19 @@ def test_interactive_max_flow3d():
83
165
prob_data = np .asarray (prob_data , np .float32 )
84
166
85
167
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 ])
87
171
88
172
seed_obj = sitk .ReadImage (seed_name )
89
173
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 ])
93
176
94
177
lamda = 10.0
95
178
sigma = 15.0
96
179
param = (lamda , sigma )
97
- lab = maxflow . interactive_maxflow3d (img_data , fP , bP , seed_data , param )
180
+ lab = interactive_maxflow3d (img_data , Prob , Seed , param )
98
181
lab_obj = sitk .GetImageFromArray (lab )
99
182
lab_obj .CopyInformation (img_obj )
100
183
sitk .WriteImage (lab_obj , save_name )
0 commit comments