Skip to content

Commit 074ae0b

Browse files
committed
DOC: Mesh and Transform NumPy conversion
PointSet to be added to the documentation following the release of InsightSoftwareConsortium/ITK#3799.
1 parent d24dc0c commit 074ae0b

File tree

5 files changed

+4344
-10
lines changed

5 files changed

+4344
-10
lines changed

docs/Quick_start_guide.rst

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,25 @@ ITK and NumPy
2727

2828
A common use case for using ITK in Python is to mingle NumPy and ITK operations on raster data. ITK provides a large number of I/O image formats and several sophisticated image processing algorithms not available in any other packages. The ability to intersperse that with the SciPy ecosystem provides a great tool for rapid prototyping.
2929

30-
The following script shows how to integrate NumPy and ITK:
30+
The following script shows how to integrate NumPy and `itk.Image`:
3131

3232
.. literalinclude:: code/MixingITKAndNumPy.py
33-
:lines: 8-33
33+
:lines: 16-50
3434

35+
NumPy and `itk.Mesh`:
3536

36-
Similar functions are available to work with `itk.Matrix`, VNL vectors and matrices:
37+
.. literalinclude:: code/MixingITKAndNumPy.py
38+
:lines: 53-67
39+
40+
NumPy and `itk.Transform`:
41+
42+
.. literalinclude:: code/MixingITKAndNumPy.py
43+
:lines: 87-106
44+
45+
NumPy and `itk.Matrix`, VNL vectors, and VNL matrices:
3746

3847
.. literalinclude:: code/MixingITKAndNumPy.py
39-
:lines: 35-
48+
:lines: 109-
4049

4150
ITK and Xarray
4251
..............

docs/code/MixingITKAndNumPy.py

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
#!/usr/bin/env python3
22

33
import sys
4+
from pathlib import Path
45

5-
input_filename = sys.argv[1]
6-
output_filename = sys.argv[2]
6+
data_dir = Path(__file__).parent.resolve() / '..' / 'data'
7+
8+
input_image_filename = sys.argv[1]
9+
temp_dir = Path(input_image_filename).parent
10+
output_image_filename = sys.argv[2]
11+
input_mesh_filename = data_dir / 'cow.vtk'
12+
output_mesh_filename = temp_dir / 'cow.vtk'
13+
input_transform_filename = data_dir / 'rigid.tfm'
14+
output_transform_filename = temp_dir / 'rigid.tfm'
715

816
import itk
917
import numpy as np
1018

1119
# Read input image
12-
itk_image = itk.imread(input_filename)
20+
itk_image = itk.imread(input_image_filename)
1321

1422
# Run filters on itk.Image
1523

@@ -21,8 +29,12 @@
2129
# Equivalent
2230
np_copy = np.asarray(itk_image)
2331

32+
# Pixel array and image metadata
33+
# in standard Python data types + NumPy array
34+
image_dict = itk.dict_from_image(itk_image)
35+
2436

25-
# Do NumPy stuff...
37+
# Do interesting things...
2638

2739

2840
# Convert back to ITK, view only, data is not copied
@@ -32,7 +44,67 @@
3244
itk_np_copy = itk.image_from_array(np_copy)
3345

3446
# Save result
35-
itk.imwrite(itk_np_view, output_filename)
47+
itk.imwrite(itk_np_view, output_image_filename)
48+
49+
# Convert back to itk image data structure
50+
itk_image = itk.image_from_dict(image_dict)
51+
52+
53+
# Read input mesh
54+
itk_mesh = itk.meshread(input_mesh_filename)
55+
56+
# Convert to standard Python data types + NumPy arrays
57+
mesh_dict = itk.dict_from_mesh(itk_mesh)
58+
59+
60+
# Do interesting things...
61+
62+
63+
# Convert back to itk mesh data structure
64+
itk_mesh = itk.mesh_from_dict(mesh_dict)
65+
66+
# Save result
67+
itk.meshwrite(itk_mesh, output_mesh_filename)
68+
69+
70+
# itk.Mesh inherits from itk.PointSet,
71+
# create a PointSet from the Mesh
72+
itk_pointset = itk.PointSet[itk.F, 3].New()
73+
itk_pointset.SetPoints(itk_mesh.GetPoints())
74+
itk_pointset.SetPointData(itk_mesh.GetPointData())
75+
76+
# Convert to standard Python data types + NumPy arrays
77+
pointset_dict = itk.pointset_from_dict(itk_pointset)
78+
79+
80+
# Do interesting things...
81+
82+
83+
# Convert back to itk pointset instance
84+
itk_pointset = itk.pointset_from_dict(pointset_dict)
85+
86+
87+
# Read input transforms
88+
#
89+
# This is a Python list
90+
#
91+
# When there is more than one transformation
92+
# the list defines a transformation chain
93+
itk_transforms = itk.transformread(input_transform_filename)
94+
95+
# Convert to standard Python data types + NumPy arrays
96+
transform_dicts = [itk.dict_from_transform(t) for t in itk_transforms]
97+
98+
99+
# Do interesting things...
100+
101+
102+
# Convert back to itk transform instance
103+
itk_transforms = [itk.transform_from_dict(t) for t in transform_dicts]
104+
105+
# Save result
106+
itk.transformwrite(itk_transforms, output_transform_filename)
107+
36108

37109
# VNL matrix from np.ndarray
38110
arr = np.zeros([3,3], np.uint8)
@@ -53,3 +125,5 @@
53125

54126
# np.ndarray from itk.Matrix
55127
arr = itk.array_from_matrix(mat)
128+
# Equivalent
129+
arr = np.asarray(mat)

docs/code/test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55
import os
66
import tempfile
7+
import shutil
78

89
def add_test(cmd):
910
cmd.insert(0, sys.executable)
@@ -12,7 +13,7 @@ def add_test(cmd):
1213
def cleanup(files):
1314
for f in files:
1415
if os.path.isdir(f):
15-
os.rmdir(f)
16+
shutil.rmtree(f)
1617
else:
1718
os.remove(f)
1819

0 commit comments

Comments
 (0)