Skip to content

Commit

Permalink
Debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
dzenanz committed Nov 30, 2023
1 parent f1b09e5 commit 325971e
Showing 1 changed file with 42 additions and 95 deletions.
137 changes: 42 additions & 95 deletions examples/ITK_Example04_InitialTransformAndMultiThreading.ipynb
Original file line number Diff line number Diff line change
@@ -1,126 +1,73 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4. Image Registration with initial transform and/or multiple threads"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this notebook 2 other options of the elastix algorithm are shown: initial transformation and multithreading.\n",
"They're shown together just to reduce the number of example notebooks and \n",
"thus can be used independently as well as in combination with whichever other functionality\n",
"of the elastix algorithm. \n",
"\n",
"Initial transforms are transformations that are done on the moving image before the registration is started.\n",
"\n",
"Multithreading spreaks for itself and can be used in similar fashion in the transformix algorithm.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Registration"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"import itk"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"import itk\n",
"\n",
"# Import Images\n",
"fixed_image = itk.imread('data/CT_2D_head_fixed.mha', itk.F)\n",
"moving_image = itk.imread('data/CT_2D_head_moving.mha', itk.F)\n",
"fixed_image = itk.imread('temp/F95_Neck.nrrd', itk.SS)\n",
"moving_image = itk.imread('temp/F05_Neck.nrrd', itk.SS)\n",
"fixed_image_n4 = itk.imread('temp/F95_Neck-bias-corrected.nrrd', itk.SS)\n",
"moving_image_n4 = itk.imread('temp/F05_Neck-bias-corrected.nrrd', itk.SS)\n",
"\n",
"# Import Default Parameter Map\n",
"parameter_object = itk.ParameterObject.New()\n",
"parameter_map_rigid = parameter_object.GetDefaultParameterMap('rigid')\n",
"parameter_map_rigid['Registration'] = ['MultiMetricMultiResolutionRegistration']\n",
"parameter_map_rigid['Transform'] = ['SimilarityTransform']\n",
"parameter_map_rigid['Metric'] = [\n",
" 'AdvancedMattesMutualInformation', 'CorrespondingPointsEuclideanDistanceMetric']\n",
"parameter_map_rigid['Metric0Weight'] = ['0.995'] # MutualInformation\n",
"parameter_map_rigid['Metric1Weight'] = ['0.005']\n",
"parameter_map_rigid['WriteResultImage'] = ['false']\n",
"parameter_map_rigid['RandomSeed'] = ['30101983']\n",
"\n",
"parameter_object.AddParameterMap(parameter_map_rigid)\n",
"parameter_object.AddParameterMap(parameter_map_rigid)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Registration can either be done in one line with the registration function..."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# Call registration function with initial transfrom and number of threads\n",
"result_image, result_transform_parameters = itk.elastix_registration_method(\n",
" fixed_image, moving_image,\n",
" parameter_object=parameter_object,\n",
" initial_transform_parameter_file_name='data/TransformParameters.0.txt',\n",
" number_of_threads=4,\n",
" log_to_console=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
".. or by initiating an elastix image filter object."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"# Load Elastix Image Filter Object with initial transfrom and number of threads\n",
"elastix_object = itk.ElastixRegistrationMethod.New(fixed_image,moving_image)\n",
"# elastix_object.SetFixedImage(fixed_image)\n",
"# elastix_object.SetMovingImage(moving_image)\n",
"parameter_object.WriteParameterFile(parameter_map_rigid,\n",
" \"C:/Dev/ITKElastix/examples/temp/parameter_map_rigid.txt\")\n",
"\n",
"# Load Elastix Image Filter Object with initial transfrom\n",
"elastix_object = itk.ElastixRegistrationMethod.New(fixed_image, moving_image)\n",
"# elastix_object = itk.ElastixRegistrationMethod.New(fixed_image_n4, moving_image_n4)\n",
"elastix_object.SetOutputDirectory(\"C:/Dev/ITKElastix/examples/temp\")\n",
"elastix_object.SetParameterObject(parameter_object)\n",
"elastix_object.SetInitialTransformParameterFileName(\n",
" 'data/TransformParameters.0.txt')\n",
"elastix_object.SetNumberOfThreads(4)\n",
"\n",
"elastix_object.SetFixedPointSetFileName(\"C:/Dev/ITKElastix/examples/temp/F95.txt\")\n",
"elastix_object.SetMovingPointSetFileName(\"C:/Dev/ITKElastix/examples/temp/F05.txt\")\n",
"\n",
"elx_filename = \"C:/Dev/ITKElastix/examples/temp/F95_F05_fiducials.txt\"\n",
"out_elastix_transform = open(elx_filename, \"w\")\n",
"out_elastix_transform.writelines(['(Transform \"File\")\\n',\n",
" '(TransformFileName \"./F95_F05_fiducials.tfm\")\\n'])\n",
"out_elastix_transform.close()\n",
"elastix_object.SetInitialTransformParameterFileName(elx_filename)\n",
"\n",
"# Set additional options\n",
"elastix_object.SetLogToConsole(False)\n",
"elastix_object.SetLogFileName(\"elastix.log\")\n",
"elastix_object.SetLogToFile(True)\n",
"\n",
"# Update filter object (required)\n",
"elastix_object.UpdateLargestPossibleRegion()\n",
"\n",
"# Results of Registration\n",
"result_image = elastix_object.GetOutput()\n",
"result_transform_parameters = elastix_object.GetTransformParameterObject()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Pointing from a transform parameter file to the path of a second initial transform parameter file is supported from the 0.7.0 release of ITKElastix."
"comb_transform = elastix_object.GetCombinationTransform()\n",
"itk_transform = elastix_object.ConvertToItkTransform(comb_transform)\n",
"elastix_transform_parameters = elastix_object.GetTransformParameterObject()\n",
"\n",
"itk.transformwrite([itk_transform], \"C:/Dev/ITKElastix/examples/temp/rigid_transform.tfm\")\n",
"parameter_object.WriteParameterFile(elastix_transform_parameters, \"C:/Dev/ITKElastix/examples/temp/rigid_transform.txt\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -134,7 +81,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.0"
"version": "3.9.13"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 325971e

Please sign in to comment.