Skip to content

Commit 35d6f62

Browse files
committed
DOC: Document ITK/NumPy Image metadata interface
1 parent 074ae0b commit 35d6f62

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

docs/Quick_start_guide.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,22 @@ A common use case for using ITK in Python is to mingle NumPy and ITK operations
3030
The following script shows how to integrate NumPy and `itk.Image`:
3131

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

3535
NumPy and `itk.Mesh`:
3636

3737
.. literalinclude:: code/MixingITKAndNumPy.py
38-
:lines: 53-67
38+
:lines: 62-76
3939

4040
NumPy and `itk.Transform`:
4141

4242
.. literalinclude:: code/MixingITKAndNumPy.py
43-
:lines: 87-106
43+
:lines: 96-115
4444

4545
NumPy and `itk.Matrix`, VNL vectors, and VNL matrices:
4646

4747
.. literalinclude:: code/MixingITKAndNumPy.py
48-
:lines: 109-
48+
:lines: 118-
4949

5050
ITK and Xarray
5151
..............

docs/code/MixingITKAndNumPy.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,38 @@
2222
# Run filters on itk.Image
2323

2424
# View only of itk.Image, pixel data is not copied
25-
np_view = itk.array_view_from_image(itk_image)
25+
array_view = itk.array_view_from_image(itk_image)
2626

2727
# Copy of itk.Image, pixel data is copied
28-
np_copy = itk.array_from_image(itk_image)
28+
array_copy = itk.array_from_image(itk_image)
2929
# Equivalent
30-
np_copy = np.asarray(itk_image)
30+
array_copy = np.asarray(itk_image)
3131

32-
# Pixel array and image metadata
32+
# Image metadata
33+
# Sequences, e.g. spacing, are in zyx (NumPy) indexing order
34+
metadata = dict(itk_image)
35+
36+
# Pixel array and image metadata together
3337
# in standard Python data types + NumPy array
38+
# Sequences, e.g. spacing, are in xyz (ITK) indexing order
3439
image_dict = itk.dict_from_image(itk_image)
3540

3641

3742
# Do interesting things...
3843

3944

4045
# Convert back to ITK, view only, data is not copied
41-
itk_np_view = itk.image_view_from_array(np_copy)
46+
itk_image_view = itk.image_view_from_array(array_copy)
4247

4348
# Convert back to ITK, data is copied
44-
itk_np_copy = itk.image_from_array(np_copy)
49+
itk_image_copy = itk.image_from_array(array_copy)
50+
51+
# Add the metadata
52+
for k, v in metadata.items():
53+
itk_image_view[k] = v
4554

4655
# Save result
47-
itk.imwrite(itk_np_view, output_image_filename)
56+
itk.imwrite(itk_image_view, output_image_filename)
4857

4958
# Convert back to itk image data structure
5059
itk_image = itk.image_from_dict(image_dict)

0 commit comments

Comments
 (0)