Skip to content

Commit 048f16b

Browse files
committed
DOC: Add notebook example for SegmentWithGeodesicActiveContourLevelSet
Also update the Python code style to be more Pythonic
1 parent c09befc commit 048f16b

File tree

4 files changed

+263
-145
lines changed

4 files changed

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

0 commit comments

Comments
 (0)