Skip to content

Commit

Permalink
ENH: Add ipynb examples Transformix, image, mesh, TranslationTransform
Browse files Browse the repository at this point in the history
Added examples on how to use recently added TransformixFilter member functions,
`SetTransform`, `SetInputMesh`, and `GetOutputMesh`.
  • Loading branch information
N-Dekker committed Nov 3, 2022
1 parent 767dfb0 commit 2aabcf5
Show file tree
Hide file tree
Showing 2 changed files with 233 additions and 0 deletions.
127 changes: 127 additions & 0 deletions examples/ITK_Example15_Transformix_image_TranslationTransform.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "4ba94dda",
"metadata": {},
"source": [
"# Minimal example on how to transform an image, using TransformixFilter with TranslationTransform"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "852e83ee",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Moving image:\n",
" 1 2 3 4 5 \n",
" 6 7 8 9 10 \n",
"11 12 13 14 15 \n",
"16 17 18 19 20 \n",
"21 22 23 24 25 \n",
"26 27 28 29 30 \n",
"\n",
"Creating a TransformixFilter (might take a while, please wait)...\n",
"\n",
"Output image:\n",
" 0 0 0 0 0 \n",
" 0 0 0 0 0 \n",
" 2 3 4 5 0 \n",
" 7 8 9 10 0 \n",
"12 13 14 15 0 \n",
"17 18 19 20 0 \n",
"\n"
]
}
],
"source": [
"import itk\n",
"\n",
"def print_image(image):\n",
" image_size = image.GetBufferedRegion().GetSize()\n",
" for row in range(image_size[1]):\n",
" for column in range(image_size[0]):\n",
" print('{:>2}'.format(int(image.GetPixel([column, row]))), ' ', end='')\n",
" print() \n",
" print()\n",
"\n",
"transform = itk.TranslationTransform.New()\n",
"transform.SetOffset([1, -2])\n",
"ImageType = itk.Image[itk.F, 2]\n",
"\n",
"number_of_columns = 5\n",
"number_of_rows = 6\n",
"\n",
"moving_image = ImageType.New()\n",
"moving_image.SetRegions([number_of_columns, number_of_rows])\n",
"moving_image.Allocate(True)\n",
"\n",
"# Set the pixel values consecutively to 1, 2, 3, ..., n.\n",
"pixel_value = 0\n",
"for row in range(number_of_rows):\n",
" for column in range(number_of_columns):\n",
" pixel_value += 1\n",
" moving_image.SetPixel([column, row], pixel_value)\n",
"\n",
"print('Moving image:')\n",
"print_image(moving_image)\n",
"\n",
"input_mesh = itk.Mesh[itk.F, 2].New()\n",
"input_mesh.SetPoint(0, [0.0, 0.0])\n",
"input_mesh.SetPoint(1, [1.0, 2.0])\n",
"\n",
"print('Creating a TransformixFilter (might take a while, please wait)...\\n')\n",
"transformix_filter = itk.TransformixFilter[ImageType].New()\n",
"\n",
"parameter_map = itk.elxParameterObjectPython.mapstringvectorstring()\n",
"parameter_map[\"Direction\"] = [\"1\", \"0\", \"0\", \"1\"]\n",
"parameter_map[\"Index\"] = [\"0\", \"0\"]\n",
"parameter_map[\"Origin\"] = [\"0\", \"0\"]\n",
"parameter_map[\"Size\"] = [str(number_of_columns), str(number_of_rows)]\n",
"parameter_map[\"Spacing\"] = [\"1\", \"1\"]\n",
"\n",
"parameter_object = itk.ParameterObject.New()\n",
"parameter_object.AddParameterMap(parameter_map)\n",
"\n",
"\n",
"transformix_filter.SetMovingImage(moving_image)\n",
"transformix_filter.SetTransformParameterObject(parameter_object)\n",
"transformix_filter.SetTransform(transform)\n",
"transformix_filter.Update()\n",
"\n",
"output_image = transformix_filter.GetOutput()\n",
"\n",
"print('Output image:')\n",
"print_image(output_image)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
106 changes: 106 additions & 0 deletions examples/ITK_Example16_Transformix_mesh_TranslationTransform.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "fe72d689",
"metadata": {},
"source": [
"# Minimal example on how to transform a mesh of points, using TransformixFilter with TranslationTransform"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "852e83ee",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Input Mesh:\n",
"itkPointF2 ([0, 0]) itkPointF2 ([0, 1]) itkPointF2 ([1, 0]) \n",
"\n",
"Creating a TransformixFilter (might take a while, please wait)...\n",
"\n",
"Output Mesh:\n",
"itkPointF2 ([0.03125, 0.0625]) itkPointF2 ([0.03125, 1.0625]) itkPointF2 ([1.03125, 0.0625]) \n",
"\n"
]
}
],
"source": [
"import itk\n",
"\n",
"def print_mesh(mesh):\n",
" for i in range(mesh.GetNumberOfPoints()):\n",
" print(mesh.GetPoint(i), ' ', end='')\n",
" print() \n",
" print() \n",
"\n",
"transform = itk.TranslationTransform.New()\n",
"transform.SetOffset([0.03125, 0.0625])\n",
"ImageType = itk.Image[itk.F, 2]\n",
"\n",
"# A moving image is required for TransformixFilter, just create a very small one.\n",
"moving_image = ImageType.New()\n",
"moving_image.SetRegions([1, 1])\n",
"moving_image.Allocate(True)\n",
"\n",
"input_mesh = itk.Mesh[itk.F, 2].New()\n",
"input_mesh.SetPoint(0, [0.0, 0.0])\n",
"input_mesh.SetPoint(1, [0.0, 1.0])\n",
"input_mesh.SetPoint(2, [1.0, 0.0])\n",
"\n",
"print('Input Mesh:')\n",
"print_mesh(input_mesh)\n",
"\n",
"print('Creating a TransformixFilter (might take a while, please wait)...\\n')\n",
"transformix_filter = itk.TransformixFilter[ImageType].New()\n",
"\n",
"parameter_map = itk.elxParameterObjectPython.mapstringvectorstring()\n",
"parameter_map[\"Direction\"] = [\"1\", \"0\", \"0\", \"1\"]\n",
"parameter_map[\"Index\"] = [\"0\", \"0\"]\n",
"parameter_map[\"Origin\"] = [\"0\", \"0\"]\n",
"parameter_map[\"Size\"] = [\"1\", \"1\"]\n",
"parameter_map[\"Spacing\"] = [\"1\", \"1\"]\n",
"\n",
"parameter_object = itk.ParameterObject.New()\n",
"parameter_object.AddParameterMap(parameter_map)\n",
"\n",
"transformix_filter.SetMovingImage(moving_image)\n",
"transformix_filter.SetTransformParameterObject(parameter_object)\n",
"transformix_filter.SetTransform(transform)\n",
"transformix_filter.SetInputMesh(input_mesh)\n",
"transformix_filter.Update()\n",
"\n",
"output_mesh = transformix_filter.GetOutputMesh()\n",
"print('Output Mesh:')\n",
"print_mesh(output_mesh)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit 2aabcf5

Please sign in to comment.