Skip to content

Commit 972312b

Browse files
committed
DOC: Add notebook example for SegmentWithGeodesicActiveContourLevelSet
Also update the Python code style to be more Pythonic
1 parent 635f1a0 commit 972312b

File tree

4 files changed

+341
-145
lines changed

4 files changed

+341
-145
lines changed

src/Segmentation/LevelSets/SegmentWithGeodesicActiveContourLevelSet/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ install(TARGETS SegmentWithGeodesicActiveContourLevelSet
1313
COMPONENT Runtime
1414
)
1515

16-
install(FILES Code.cxx CMakeLists.txt
16+
install(FILES Code.cxx CMakeLists.txt Code.py
1717
DESTINATION share/ITKSphinxExamples/Code/Segmentation/LevelSets/SegmentWithGeodesicActiveContourLevelSet
1818
COMPONENT Code
1919
)
Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "196c6071",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"import sys\n",
11+
"\n",
12+
"import itk\n",
13+
"\n",
14+
"from itkwidgets import view"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 2,
20+
"id": "52104b31",
21+
"metadata": {},
22+
"outputs": [
23+
{
24+
"data": {
25+
"application/vnd.jupyter.widget-view+json": {
26+
"model_id": "f93f02adae4d47c7add2409a5f97b075",
27+
"version_major": 2,
28+
"version_minor": 0
29+
},
30+
"text/plain": [
31+
"Viewer(geometries=[], gradient_opacity=0.22, point_sets=[], rendered_image=<itk.itkImagePython.itkImageF2; pro…"
32+
]
33+
},
34+
"metadata": {},
35+
"output_type": "display_data"
36+
}
37+
],
38+
"source": [
39+
"InputPixelType = itk.ctype('float')\n",
40+
"\n",
41+
"input_image = itk.imread('./BrainProtonDensitySlice.png', InputPixelType)\n",
42+
"\n",
43+
"view(input_image)"
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": 3,
49+
"id": "4297e2ac",
50+
"metadata": {},
51+
"outputs": [
52+
{
53+
"data": {
54+
"application/vnd.jupyter.widget-view+json": {
55+
"model_id": "ec1c09bacc97419f8da1825563aa36bc",
56+
"version_major": 2,
57+
"version_minor": 0
58+
},
59+
"text/plain": [
60+
"Viewer(geometries=[], gradient_opacity=0.22, point_sets=[], rendered_image=<itk.itkImagePython.itkImageF2; pro…"
61+
]
62+
},
63+
"metadata": {},
64+
"output_type": "display_data"
65+
}
66+
],
67+
"source": [
68+
"smoothed = itk.curvature_anisotropic_diffusion_image_filter(input_image,\n",
69+
" time_step=0.125,\n",
70+
" number_of_iterations=5,\n",
71+
" conductance_parameter=9.0)\n",
72+
"view(smoothed)"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": 4,
78+
"id": "99186583",
79+
"metadata": {},
80+
"outputs": [
81+
{
82+
"data": {
83+
"application/vnd.jupyter.widget-view+json": {
84+
"model_id": "dd0a1ba394ed4cab87b0c79e7b9a137d",
85+
"version_major": 2,
86+
"version_minor": 0
87+
},
88+
"text/plain": [
89+
"Viewer(geometries=[], gradient_opacity=0.22, point_sets=[], rendered_image=<itk.itkImagePython.itkImageF2; pro…"
90+
]
91+
},
92+
"metadata": {},
93+
"output_type": "display_data"
94+
}
95+
],
96+
"source": [
97+
"sigma = 1.0\n",
98+
"\n",
99+
"gradient_magnitude = itk.gradient_magnitude_recursive_gaussian_image_filter(smoothed,\n",
100+
" sigma=sigma)\n",
101+
"view(gradient_magnitude)"
102+
]
103+
},
104+
{
105+
"cell_type": "code",
106+
"execution_count": 5,
107+
"id": "8a219f34",
108+
"metadata": {},
109+
"outputs": [
110+
{
111+
"data": {
112+
"application/vnd.jupyter.widget-view+json": {
113+
"model_id": "4839685882a04ed3a697f3e947458368",
114+
"version_major": 2,
115+
"version_minor": 0
116+
},
117+
"text/plain": [
118+
"Viewer(geometries=[], gradient_opacity=0.22, point_sets=[], rendered_image=<itk.itkImagePython.itkImageF2; pro…"
119+
]
120+
},
121+
"metadata": {},
122+
"output_type": "display_data"
123+
}
124+
],
125+
"source": [
126+
"alpha = -0.5\n",
127+
"beta = 3.0\n",
128+
"\n",
129+
"sigmoid = itk.sigmoid_image_filter(gradient_magnitude,\n",
130+
" output_minimum=0.0,\n",
131+
" output_maximum=1.0,\n",
132+
" alpha=alpha,\n",
133+
" beta=beta)\n",
134+
"\n",
135+
"view(sigmoid)"
136+
]
137+
},
138+
{
139+
"cell_type": "code",
140+
"execution_count": 6,
141+
"id": "499443a8",
142+
"metadata": {},
143+
"outputs": [],
144+
"source": [
145+
"Dimension = input_image.GetImageDimension()\n",
146+
"seeds = itk.VectorContainer[itk.UI, itk.LevelSetNode[InputPixelType, Dimension]].New()\n",
147+
"seeds.Initialize()\n",
148+
"\n",
149+
"seed_position = itk.Index[Dimension]()\n",
150+
"seed_position[0] = 81\n",
151+
"seed_position[1] = 114\n",
152+
"node = itk.LevelSetNode[InputPixelType, Dimension]()\n",
153+
"node.SetValue(-5.0)\n",
154+
"node.SetIndex(seed_position)\n",
155+
"seeds.InsertElement(0, node)\n",
156+
"\n",
157+
"fast_marching = itk.fast_marching_image_filter(trial_points=seeds,\n",
158+
" speed_constant=1.0,\n",
159+
" output_size=input_image.GetBufferedRegion().GetSize())"
160+
]
161+
},
162+
{
163+
"cell_type": "code",
164+
"execution_count": 7,
165+
"id": "d2c5e205",
166+
"metadata": {},
167+
"outputs": [
168+
{
169+
"data": {
170+
"application/vnd.jupyter.widget-view+json": {
171+
"model_id": "8d762859e81a48698841081217638423",
172+
"version_major": 2,
173+
"version_minor": 0
174+
},
175+
"text/plain": [
176+
"Viewer(geometries=[], gradient_opacity=0.22, point_sets=[], rendered_image=<itk.itkImagePython.itkImageF2; pro…"
177+
]
178+
},
179+
"metadata": {},
180+
"output_type": "display_data"
181+
}
182+
],
183+
"source": [
184+
"propagation_scaling = 2.0\n",
185+
"number_of_iterations = 800\n",
186+
"\n",
187+
"geodesic_active_contour = \\\n",
188+
" itk.geodesic_active_contour_level_set_image_filter(fast_marching,\n",
189+
" propagation_scaling=propagation_scaling,\n",
190+
" curvature_scaling=1.0,\n",
191+
" advection_scaling=1.0,\n",
192+
" maximum_r_m_s_error=0.02,\n",
193+
" number_of_iterations=number_of_iterations,\n",
194+
" feature_image=sigmoid)\n",
195+
"\n",
196+
"view(geodesic_active_contour)"
197+
]
198+
},
199+
{
200+
"cell_type": "code",
201+
"execution_count": 8,
202+
"id": "1269f1b4",
203+
"metadata": {},
204+
"outputs": [],
205+
"source": [
206+
"OutputPixelType = itk.ctype('unsigned char')\n",
207+
"thresholded = itk.binary_threshold_image_filter(geodesic_active_contour,\n",
208+
" lower_threshold=-1000.0,\n",
209+
" upper_threshold=0.0,\n",
210+
" outside_value=itk.NumericTraits[OutputPixelType].min(),\n",
211+
" inside_value=itk.NumericTraits[OutputPixelType].max(),\n",
212+
" ttype=[type(geodesic_active_contour), itk.Image[OutputPixelType,Dimension]])"
213+
]
214+
},
215+
{
216+
"cell_type": "code",
217+
"execution_count": 9,
218+
"id": "58bdf582",
219+
"metadata": {},
220+
"outputs": [
221+
{
222+
"data": {
223+
"application/vnd.jupyter.widget-view+json": {
224+
"model_id": "9c693a5687134f269fd009babaae2e29",
225+
"version_major": 2,
226+
"version_minor": 0
227+
},
228+
"text/plain": [
229+
"Viewer(geometries=[], gradient_opacity=0.22, point_sets=[], rendered_image=<itk.itkImagePython.itkImageUC2; pr…"
230+
]
231+
},
232+
"metadata": {},
233+
"output_type": "display_data"
234+
}
235+
],
236+
"source": [
237+
"view(thresholded)"
238+
]
239+
}
240+
],
241+
"metadata": {
242+
"kernelspec": {
243+
"display_name": "Python 3",
244+
"language": "python",
245+
"name": "python3"
246+
},
247+
"language_info": {
248+
"codemirror_mode": {
249+
"name": "ipython",
250+
"version": 3
251+
},
252+
"file_extension": ".py",
253+
"mimetype": "text/x-python",
254+
"name": "python",
255+
"nbconvert_exporter": "python",
256+
"pygments_lexer": "ipython3",
257+
"version": "3.8.6"
258+
}
259+
},
260+
"nbformat": 4,
261+
"nbformat_minor": 5
262+
}

0 commit comments

Comments
 (0)