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 7, 2022
1 parent c14e98d commit db606f8
Show file tree
Hide file tree
Showing 2 changed files with 282 additions and 0 deletions.
146 changes: 146 additions & 0 deletions examples/ITK_Example15_Transformix_image_TranslationTransform.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "4ba94dda",
"metadata": {},
"source": [
"# Minimal example on how to transform an image, using TransformixFilter with TranslationTransform"
]
},
{
"cell_type": "markdown",
"id": "5fc4a7b9",
"metadata": {},
"source": [
"This example shows how to transform an image, using `itk.TransformixFilter` with `itk.TranslationTransform`.\n",
"\n",
"Start by importing packages and creating an TransformixFilter object:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "852e83ee",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"import itk\n",
"import numpy as np\n",
"\n",
"print('Creating a TransformixFilter (might take a while, please wait)...\\n')\n",
"transformix_filter = itk.TransformixFilter[ImageType].New()"
]
},
{
"cell_type": "markdown",
"id": "65d4e03c",
"metadata": {},
"source": [
"Create the example input: a small (5x6) image, a translation (1, -2), and a rather trivial parameter map.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9fbb7de3",
"metadata": {},
"outputs": [],
"source": [
"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(np.asarray(output_image))\n",
"print()\n",
"\n",
"translation = [1, -2]\n",
"print('Translation:', translation)\n",
"\n",
"transform = itk.TranslationTransform.New()\n",
"transform.SetOffset(translation)\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\"]"
]
},
{
"cell_type": "markdown",
"id": "2947cf4a",
"metadata": {},
"source": [
"Pass the example input to the TransformixFilter object, apply the transformation, and retrieve the output image. Ready!"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "26f9394e",
"metadata": {},
"outputs": [],
"source": [
"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.Update()\n",
"\n",
"output_image = transformix_filter.GetOutput()\n",
"\n",
"print('Output image:')\n",
"print(np.asarray(output_image))\n",
"print()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f4832730",
"metadata": {},
"outputs": [],
"source": []
}
],
"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
}
136 changes: 136 additions & 0 deletions examples/ITK_Example16_Transformix_mesh_TranslationTransform.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "fe72d689",
"metadata": {},
"source": [
"# Minimal example on how to transform a mesh of points, using TransformixFilter with TranslationTransform"
]
},
{
"cell_type": "markdown",
"id": "2ef87562",
"metadata": {},
"source": [
"This example shows how to transform a mesh, using `itk.TransformixFilter` with `itk.TranslationTransform`.\n",
"\n",
"Start by importing packages, defining a little print helper function, and creating an TransformixFilter object:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "852e83ee",
"metadata": {
"scrolled": true
},
"outputs": [],
"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",
"print('Creating a TransformixFilter (might take a while, please wait)...\\n')\n",
"transformix_filter = itk.TransformixFilter[ImageType].New()\n"
]
},
{
"cell_type": "markdown",
"id": "b09bb751",
"metadata": {},
"source": [
"Create the example input: a mesh of three points, a small translation (0.03125, 0.0625), a \"dummy\" (1x1) image, and a rather trivial parameter map."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "539fc713",
"metadata": {},
"outputs": [],
"source": [
"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",
"translation = [0.03125, 0.0625]\n",
"print('Translation:', translation)\n",
"\n",
"transform = itk.TranslationTransform.New()\n",
"transform.SetOffset(translation)\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",
"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\"]"
]
},
{
"cell_type": "markdown",
"id": "3c1541e7",
"metadata": {},
"source": [
"Pass the example input to the TransformixFilter object, apply the transformation, and retrieve the output mesh. Ready!"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "606a65b9",
"metadata": {},
"outputs": [],
"source": [
"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 db606f8

Please sign in to comment.