Skip to content

Commit

Permalink
Debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
dzenanz committed Nov 29, 2023
1 parent f1b09e5 commit 592f2f4
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 215 deletions.
132 changes: 36 additions & 96 deletions examples/ITK_Example04_InitialTransformAndMultiThreading.ipynb
Original file line number Diff line number Diff line change
@@ -1,104 +1,43 @@
{
"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,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import itk"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "TypeError",
"evalue": "in method 'itkElastixRegistrationMethodISS3ISS3_SetExternalInitialTransform', argument 2 of type 'itkTransformD33 *'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[1], line 20\u001b[0m\n\u001b[0;32m 18\u001b[0m elastix_object\u001b[38;5;241m.\u001b[39mSetParameterObject(parameter_object)\n\u001b[0;32m 19\u001b[0m initial_transform \u001b[38;5;241m=\u001b[39m itk\u001b[38;5;241m.\u001b[39mSimilarity2DTransform[itk\u001b[38;5;241m.\u001b[39mD]\u001b[38;5;241m.\u001b[39mNew()\n\u001b[1;32m---> 20\u001b[0m \u001b[43melastix_object\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mSetExternalInitialTransform\u001b[49m\u001b[43m(\u001b[49m\u001b[43minitial_transform\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 21\u001b[0m \u001b[38;5;66;03m# Set additional options\u001b[39;00m\n\u001b[0;32m 22\u001b[0m elastix_object\u001b[38;5;241m.\u001b[39mSetLogToConsole(\u001b[38;5;28;01mFalse\u001b[39;00m)\n",
"\u001b[1;31mTypeError\u001b[0m: in method 'itkElastixRegistrationMethodISS3ISS3_SetExternalInitialTransform', argument 2 of type 'itkTransformD33 *'"
]
}
],
"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('data/F95_Neck.nrrd', itk.SS)\n",
"moving_image = itk.imread('data/F05_Neck.nrrd', itk.SS)\n",
"fixed_image_n4 = itk.imread('data/F95_Neck-bias-corrected.nrrd', itk.SS)\n",
"moving_image_n4 = itk.imread('data/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['Transform'] = ['SimilarityTransform']\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": [
"\n",
"# 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",
"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",
"initial_transform = itk.Similarity2DTransform[itk.D].New()\n",
"elastix_object.SetExternalInitialTransform(initial_transform)\n",
"# Set additional options\n",
"elastix_object.SetLogToConsole(False)\n",
"\n",
Expand All @@ -107,20 +46,21 @@
"\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",
"# parameter_object.WriteParameterFile(elastix_transform_parameters, \"rigid_transform.txt\")\n",
"parameter_object.WriteParameterFiles(\n",
" elastix_transform_parameters,\n",
" [\"fiducial_transform.txt\", \"rigid_transform.txt\"],\n",
")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -134,7 +74,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.0"
"version": "3.9.13"
}
},
"nbformat": 4,
Expand Down
33 changes: 14 additions & 19 deletions examples/exampleoutput/TransformParameters.0.txt
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
(Transform "EulerTransform")
(NumberOfParameters 3)
(TransformParameters -0.001020 -0.954586 -0.005754)
(InitialTransformParametersFileName "NoInitialTransform")
(UseBinaryFormatForTransformationParameters "false")
(HowToCombineTransforms "Compose")

// Image specific
(CenterOfRotationPoint 127.5 127.5)
(Direction 1 0 0 1)
(FixedImageDimension 2)
(MovingImageDimension 2)
(FixedInternalImagePixelType "float")
(HowToCombineTransforms "Compose")
(Index 0 0)
(InitialTransformParameterFileName "InitialTransformParameters.0.txt")
(MovingImageDimension 2)
(MovingInternalImagePixelType "float")
(NumberOfParameters 4)
(Origin 0 0)
(Size 256 256)
(Index 0 0)
(Spacing 1.0000000000 1.0000000000)
(Origin 0.0000000000 0.0000000000)
(Direction 1.0000000000 0.0000000000 0.0000000000 1.0000000000)
(Spacing 1 1)
(Transform "SimilarityTransform")
(TransformParameters 1.001915129959456 -0.0044343920550079 -1.0140924954490822 0.04579056048047046)
(UseDirectionCosines "true")

// EulerTransform specific
(CenterOfRotationPoint 127.5000000000 127.5000000000)

// ResampleInterpolator specific
(ResampleInterpolator "FinalBSplineInterpolator")
(FinalBSplineInterpolationOrder 3)
(ResampleInterpolator "FinalBSplineInterpolator")

// Resampler specific
(CompressResultImage "false")
(DefaultPixelValue 0)
(Resampler "DefaultResampler")
(DefaultPixelValue 0.000000)
(ResultImageFormat "nii")
(ResultImagePixelType "float")
(CompressResultImage "false")
Loading

0 comments on commit 592f2f4

Please sign in to comment.