Skip to content

Commit

Permalink
DOC: Add notebook example for SegmentWithGeodesicActiveContourLevelSet
Browse files Browse the repository at this point in the history
Also update the Python code style to be more Pythonic
  • Loading branch information
thewtex committed May 18, 2021
1 parent c09befc commit 048f16b
Show file tree
Hide file tree
Showing 4 changed files with 263 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ install(TARGETS SegmentWithGeodesicActiveContourLevelSet
COMPONENT Runtime
)

install(FILES Code.cxx CMakeLists.txt
install(FILES Code.cxx CMakeLists.txt Code.py
DESTINATION share/ITKSphinxExamples/Code/Segmentation/LevelSets/SegmentWithGeodesicActiveContourLevelSet
COMPONENT Code
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "196c6071",
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"import os\n",
"from urllib.request import urlretrieve\n",
"\n",
"import itk\n",
"\n",
"from itkwidgets import view"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d6f35e01",
"metadata": {},
"outputs": [],
"source": [
"input_filename = 'BrainProtonDensitySlice.png'\n",
"if not os.path.exists(input_filename):\n",
" url = 'https://data.kitware.com/api/v1/file/57b5d8028d777f10f2694bbf/download'\n",
" urlretrieve(url, input_filename)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "52104b31",
"metadata": {},
"outputs": [],
"source": [
"InputPixelType = itk.ctype('float')\n",
"\n",
"input_image = itk.imread(input_filename, InputPixelType)\n",
"\n",
"view(input_image)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4297e2ac",
"metadata": {},
"outputs": [],
"source": [
"smoothed = itk.curvature_anisotropic_diffusion_image_filter(input_image,\n",
" time_step=0.125,\n",
" number_of_iterations=5,\n",
" conductance_parameter=9.0)\n",
"view(smoothed)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "99186583",
"metadata": {},
"outputs": [],
"source": [
"sigma = 1.0\n",
"\n",
"gradient_magnitude = itk.gradient_magnitude_recursive_gaussian_image_filter(smoothed,\n",
" sigma=sigma)\n",
"view(gradient_magnitude)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8a219f34",
"metadata": {},
"outputs": [],
"source": [
"alpha = -0.5\n",
"beta = 3.0\n",
"\n",
"sigmoid = itk.sigmoid_image_filter(gradient_magnitude,\n",
" output_minimum=0.0,\n",
" output_maximum=1.0,\n",
" alpha=alpha,\n",
" beta=beta)\n",
"\n",
"view(sigmoid)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "499443a8",
"metadata": {},
"outputs": [],
"source": [
"Dimension = input_image.GetImageDimension()\n",
"seeds = itk.VectorContainer[itk.UI, itk.LevelSetNode[InputPixelType, Dimension]].New()\n",
"seeds.Initialize()\n",
"\n",
"seed_position = itk.Index[Dimension]()\n",
"seed_position[0] = 81\n",
"seed_position[1] = 114\n",
"node = itk.LevelSetNode[InputPixelType, Dimension]()\n",
"node.SetValue(-5.0)\n",
"node.SetIndex(seed_position)\n",
"seeds.InsertElement(0, node)\n",
"\n",
"fast_marching = itk.fast_marching_image_filter(trial_points=seeds,\n",
" speed_constant=1.0,\n",
" output_size=input_image.GetBufferedRegion().GetSize())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d2c5e205",
"metadata": {},
"outputs": [],
"source": [
"propagation_scaling = 2.0\n",
"number_of_iterations = 800\n",
"\n",
"geodesic_active_contour = \\\n",
" itk.geodesic_active_contour_level_set_image_filter(fast_marching,\n",
" propagation_scaling=propagation_scaling,\n",
" curvature_scaling=1.0,\n",
" advection_scaling=1.0,\n",
" maximum_r_m_s_error=0.02,\n",
" number_of_iterations=number_of_iterations,\n",
" feature_image=sigmoid)\n",
"\n",
"view(geodesic_active_contour)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1269f1b4",
"metadata": {},
"outputs": [],
"source": [
"OutputPixelType = itk.ctype('unsigned char')\n",
"thresholded = itk.binary_threshold_image_filter(geodesic_active_contour,\n",
" lower_threshold=-1000.0,\n",
" upper_threshold=0.0,\n",
" outside_value=itk.NumericTraits[OutputPixelType].min(),\n",
" inside_value=itk.NumericTraits[OutputPixelType].max(),\n",
" ttype=[type(geodesic_active_contour), itk.Image[OutputPixelType,Dimension]])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "58bdf582",
"metadata": {},
"outputs": [],
"source": [
"view(thresholded)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit 048f16b

Please sign in to comment.