Skip to content

Commit 594ae03

Browse files
committed
DOC: Add notebook example for SegmentWithGeodesicActiveContourLevelSet
Also update the Python code style to be more Pythonic
1 parent 660ce50 commit 594ae03

File tree

4 files changed

+271
-145
lines changed

4 files changed

+271
-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: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "7c8b935b",
6+
"metadata": {},
7+
"source": [
8+
"# Segment with geodesic active contour level set"
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": null,
14+
"id": "196c6071",
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"import sys\n",
19+
"import os\n",
20+
"from urllib.request import urlretrieve\n",
21+
"\n",
22+
"import itk\n",
23+
"\n",
24+
"from itkwidgets import view"
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": null,
30+
"id": "d6f35e01",
31+
"metadata": {},
32+
"outputs": [],
33+
"source": [
34+
"input_filename = 'BrainProtonDensitySlice.png'\n",
35+
"if not os.path.exists(input_filename):\n",
36+
" url = 'https://data.kitware.com/api/v1/file/57b5d8028d777f10f2694bbf/download'\n",
37+
" urlretrieve(url, input_filename)"
38+
]
39+
},
40+
{
41+
"cell_type": "code",
42+
"execution_count": null,
43+
"id": "52104b31",
44+
"metadata": {},
45+
"outputs": [],
46+
"source": [
47+
"InputPixelType = itk.ctype('float')\n",
48+
"\n",
49+
"input_image = itk.imread(input_filename, InputPixelType)\n",
50+
"\n",
51+
"view(input_image)"
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": null,
57+
"id": "4297e2ac",
58+
"metadata": {},
59+
"outputs": [],
60+
"source": [
61+
"smoothed = itk.curvature_anisotropic_diffusion_image_filter(input_image,\n",
62+
" time_step=0.125,\n",
63+
" number_of_iterations=5,\n",
64+
" conductance_parameter=9.0)\n",
65+
"view(smoothed)"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": null,
71+
"id": "99186583",
72+
"metadata": {},
73+
"outputs": [],
74+
"source": [
75+
"sigma = 1.0\n",
76+
"\n",
77+
"gradient_magnitude = itk.gradient_magnitude_recursive_gaussian_image_filter(smoothed,\n",
78+
" sigma=sigma)\n",
79+
"view(gradient_magnitude)"
80+
]
81+
},
82+
{
83+
"cell_type": "code",
84+
"execution_count": null,
85+
"id": "8a219f34",
86+
"metadata": {},
87+
"outputs": [],
88+
"source": [
89+
"alpha = -0.5\n",
90+
"beta = 3.0\n",
91+
"\n",
92+
"sigmoid = itk.sigmoid_image_filter(gradient_magnitude,\n",
93+
" output_minimum=0.0,\n",
94+
" output_maximum=1.0,\n",
95+
" alpha=alpha,\n",
96+
" beta=beta)\n",
97+
"\n",
98+
"view(sigmoid)"
99+
]
100+
},
101+
{
102+
"cell_type": "code",
103+
"execution_count": null,
104+
"id": "499443a8",
105+
"metadata": {},
106+
"outputs": [],
107+
"source": [
108+
"Dimension = input_image.GetImageDimension()\n",
109+
"seeds = itk.VectorContainer[itk.UI, itk.LevelSetNode[InputPixelType, Dimension]].New()\n",
110+
"seeds.Initialize()\n",
111+
"\n",
112+
"seed_position = itk.Index[Dimension]()\n",
113+
"seed_position[0] = 81\n",
114+
"seed_position[1] = 114\n",
115+
"node = itk.LevelSetNode[InputPixelType, Dimension]()\n",
116+
"node.SetValue(-5.0)\n",
117+
"node.SetIndex(seed_position)\n",
118+
"seeds.InsertElement(0, node)\n",
119+
"\n",
120+
"fast_marching = itk.fast_marching_image_filter(trial_points=seeds,\n",
121+
" speed_constant=1.0,\n",
122+
" output_size=input_image.GetBufferedRegion().GetSize())"
123+
]
124+
},
125+
{
126+
"cell_type": "code",
127+
"execution_count": null,
128+
"id": "d2c5e205",
129+
"metadata": {},
130+
"outputs": [],
131+
"source": [
132+
"propagation_scaling = 2.0\n",
133+
"number_of_iterations = 800\n",
134+
"\n",
135+
"geodesic_active_contour = \\\n",
136+
" itk.geodesic_active_contour_level_set_image_filter(fast_marching,\n",
137+
" propagation_scaling=propagation_scaling,\n",
138+
" curvature_scaling=1.0,\n",
139+
" advection_scaling=1.0,\n",
140+
" maximum_r_m_s_error=0.02,\n",
141+
" number_of_iterations=number_of_iterations,\n",
142+
" feature_image=sigmoid)\n",
143+
"\n",
144+
"view(geodesic_active_contour)"
145+
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": null,
150+
"id": "1269f1b4",
151+
"metadata": {},
152+
"outputs": [],
153+
"source": [
154+
"OutputPixelType = itk.ctype('unsigned char')\n",
155+
"thresholded = itk.binary_threshold_image_filter(geodesic_active_contour,\n",
156+
" lower_threshold=-1000.0,\n",
157+
" upper_threshold=0.0,\n",
158+
" outside_value=itk.NumericTraits[OutputPixelType].min(),\n",
159+
" inside_value=itk.NumericTraits[OutputPixelType].max(),\n",
160+
" ttype=[type(geodesic_active_contour), itk.Image[OutputPixelType,Dimension]])"
161+
]
162+
},
163+
{
164+
"cell_type": "code",
165+
"execution_count": null,
166+
"id": "58bdf582",
167+
"metadata": {},
168+
"outputs": [],
169+
"source": [
170+
"view(thresholded)"
171+
]
172+
}
173+
],
174+
"metadata": {
175+
"kernelspec": {
176+
"display_name": "Python 3",
177+
"language": "python",
178+
"name": "python3"
179+
},
180+
"language_info": {
181+
"codemirror_mode": {
182+
"name": "ipython",
183+
"version": 3
184+
},
185+
"file_extension": ".py",
186+
"mimetype": "text/x-python",
187+
"name": "python",
188+
"nbconvert_exporter": "python",
189+
"pygments_lexer": "ipython3",
190+
"version": "3.8.6"
191+
}
192+
},
193+
"nbformat": 4,
194+
"nbformat_minor": 5
195+
}

0 commit comments

Comments
 (0)