Skip to content

Commit

Permalink
ENH: Add vtkImageData example
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed May 2, 2018
1 parent 6e80382 commit d9a09d0
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ js/package-lock.json
005_32months_T2_RegT1_Reg2Atlas_ManualBrainMask_Stripped.nrrd
examples/005_32months_T2_RegT1_Reg2Atlas_ManualBrainMask_Stripped.nrrd
examples/cthead1.png
examples/vase.vti

js/lib/ZstdDecompress/web-build/
.pytest_cache/
118 changes: 118 additions & 0 deletions examples/vtkImageData.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Install dependencies for this example\n",
"# Note: This does not include itk-jupyter-widgets, itself\n",
"import sys\n",
"!{sys.executable} -m pip install vtk requests"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import vtk\n",
"\n",
"import requests\n",
"import shutil\n",
"import os\n",
"\n",
"from itkwidgets import view"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Download data\n",
"file_name = 'vase.vti'\n",
"if not os.path.exists(file_name):\n",
" response = requests.get('https://data.kitware.com/api/v1/file/5a826bdc8d777f0685782960/download', stream=True)\n",
" with open(file_name, 'wb') as fp:\n",
" response.raw.decode_content = True\n",
" shutil.copyfileobj(response.raw, fp)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"reader = vtk.vtkXMLImageDataReader()\n",
"reader.SetFileName(file_name)\n",
"reader.Update()\n",
"vtk_image = reader.GetOutput()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "a50c990db54a4d41b274aba2869e782d",
"version_major": 2,
"version_minor": 0
},
"text/html": [
"<p>Failed to display Jupyter Widget of type <code>Viewer</code>.</p>\n",
"<p>\n",
" If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n",
" that the widgets JavaScript is still loading. If this message persists, it\n",
" likely means that the widgets JavaScript library is either not installed or\n",
" not enabled. See the <a href=\"https://ipywidgets.readthedocs.io/en/stable/user_install.html\">Jupyter\n",
" Widgets Documentation</a> for setup instructions.\n",
"</p>\n",
"<p>\n",
" If you're reading this message in another frontend (for example, a static\n",
" rendering on GitHub or <a href=\"https://nbviewer.jupyter.org/\">NBViewer</a>),\n",
" it may mean that your frontend doesn't currently support widgets.\n",
"</p>\n"
],
"text/plain": [
"Viewer(image=<itkImagePython.itkImageUC3; proxy of <Swig Object of type 'itkImageUC3 *' at 0x7fc0d2764ba0> >)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"view(vtk_image)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
14 changes: 14 additions & 0 deletions itkwidgets/widget_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
import numpy as np
import itk
from .trait_types import ITKImage, itkimage_serialization
HAVE_VTK = False
try:
import vtk
HAVE_VTK = True
except ImportError:
pass

@widgets.register
class Viewer(widgets.DOMWidget):
Expand All @@ -26,6 +32,14 @@ def view(image):
if isinstance(image, np.ndarray):
image_from_array = itk.GetImageViewFromArray(image)
viewer.image = image_from_array
elif HAVE_VTK and isinstance(image, vtk.vtkImageData):
from vtk.util import numpy_support as vtk_numpy_support
array = vtk_numpy_support.vtk_to_numpy(image.GetPointData().GetScalars())
array.shape = tuple(image.GetDimensions())[::-1]
image_from_array = itk.GetImageViewFromArray(array)
image_from_array.SetSpacing(image.GetSpacing())
image_from_array.SetOrigin(image.GetOrigin())
viewer.image = image_from_array
else:
viewer.image = image
return viewer

0 comments on commit d9a09d0

Please sign in to comment.