Skip to content

geodesic notebook #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
cmake_minimum_required(VERSION 3.10.2)

project( SegmentWithGeodesicActiveContourLevelSet )
project(SegmentWithGeodesicActiveContourLevelSet)

find_package( ITK REQUIRED )
include( ${ITK_USE_FILE} )
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})

add_executable( SegmentWithGeodesicActiveContourLevelSet Code.cxx )
target_link_libraries( SegmentWithGeodesicActiveContourLevelSet ${ITK_LIBRARIES} )
add_executable(SegmentWithGeodesicActiveContourLevelSet Code.cxx)
target_link_libraries(SegmentWithGeodesicActiveContourLevelSet ${ITK_LIBRARIES})

install( TARGETS SegmentWithGeodesicActiveContourLevelSet
install(TARGETS SegmentWithGeodesicActiveContourLevelSet
DESTINATION bin/ITKSphinxExamples/Segmentation/LevelSets
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
)

enable_testing()

add_test(NAME GeodesicActiveContourLeftVentricleTest
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/SegmentWithGeodesicActiveContourLevelSet
${CMAKE_CURRENT_BINARY_DIR}/BrainProtonDensitySlice.png
Output1.png
81 114 5.0 1.0 -0.5 3.0 2.0 800
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/SegmentWithGeodesicActiveContourLevelSet
${CMAKE_CURRENT_BINARY_DIR}/BrainProtonDensitySlice.png
Output1.png
81 114 5.0 1.0 -0.5 3.0 2.0 800
)

add_test(NAME GeodesicActiveContourRightVentricleTest
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/SegmentWithGeodesicActiveContourLevelSet
${CMAKE_CURRENT_BINARY_DIR}/BrainProtonDensitySlice.png
Output2.png
99 114 5.0 1.0 -0.5 3.0 2.0 800
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/SegmentWithGeodesicActiveContourLevelSet
${CMAKE_CURRENT_BINARY_DIR}/BrainProtonDensitySlice.png
Output2.png
99 114 5.0 1.0 -0.5 3.0 2.0 800
)

add_test(NAME GeodesicActiveContourWhiteMatterTest
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/SegmentWithGeodesicActiveContourLevelSet
${CMAKE_CURRENT_BINARY_DIR}/BrainProtonDensitySlice.png
Output3.png
56 92 5.0 1.0 -0.3 2.0 10.0 800
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/SegmentWithGeodesicActiveContourLevelSet
${CMAKE_CURRENT_BINARY_DIR}/BrainProtonDensitySlice.png
Output3.png
56 92 5.0 1.0 -0.3 2.0 10.0 800
)

add_test(NAME GeodesicActiveContourGrayMatterTest
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/SegmentWithGeodesicActiveContourLevelSet
${CMAKE_CURRENT_BINARY_DIR}/BrainProtonDensitySlice.png
Output4.png
40 90 5.0 .5 -0.3 2.0 10.0 800
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/SegmentWithGeodesicActiveContourLevelSet
${CMAKE_CURRENT_BINARY_DIR}/BrainProtonDensitySlice.png
Output4.png
40 90 5.0 .5 -0.3 2.0 10.0 800
)

if( ITK_WRAP_PYTHON )
if(ITK_WRAP_PYTHON)
add_test(NAME GeodesicActiveContourLeftVentricleTestPython
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Code.py
${CMAKE_CURRENT_BINARY_DIR}/BrainProtonDensitySlice.png
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Point picker for itkwidgets would come in handy here for interactive experimentation.


Reply via ReviewNB

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, on the agenda :-).

"cells": [
{
"cell_type": "markdown",
"id": "7c8b935b",
"metadata": {},
"source": [
"# Segment with geodesic active contour level set"
]
},
{
"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