|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 |
|
3 | 3 | import sys |
| 4 | +from pathlib import Path |
4 | 5 |
|
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' |
7 | 15 |
|
8 | 16 | import itk |
9 | 17 | import numpy as np |
10 | 18 |
|
11 | 19 | # Read input image |
12 | | -itk_image = itk.imread(input_filename) |
| 20 | +itk_image = itk.imread(input_image_filename) |
13 | 21 |
|
14 | 22 | # Run filters on itk.Image |
15 | 23 |
|
16 | 24 | # View only of itk.Image, pixel data is not copied |
17 | | -np_view = itk.array_view_from_image(itk_image) |
| 25 | +array_view = itk.array_view_from_image(itk_image) |
18 | 26 |
|
19 | 27 | # Copy of itk.Image, pixel data is copied |
20 | | -np_copy = itk.array_from_image(itk_image) |
| 28 | +array_copy = itk.array_from_image(itk_image) |
21 | 29 | # Equivalent |
22 | | -np_copy = np.asarray(itk_image) |
| 30 | +array_copy = np.asarray(itk_image) |
| 31 | + |
| 32 | +# Image metadata |
| 33 | +# Sequences, e.g. spacing, are in zyx (NumPy) indexing order |
| 34 | +metadata = dict(itk_image) |
23 | 35 |
|
| 36 | +# Pixel array and image metadata together |
| 37 | +# in standard Python data types + NumPy array |
| 38 | +# Sequences, e.g. spacing, are in xyz (ITK) indexing order |
| 39 | +image_dict = itk.dict_from_image(itk_image) |
24 | 40 |
|
25 | | -# Do NumPy stuff... |
| 41 | + |
| 42 | +# Do interesting things... |
26 | 43 |
|
27 | 44 |
|
28 | 45 | # Convert back to ITK, view only, data is not copied |
29 | | -itk_np_view = itk.image_view_from_array(np_copy) |
| 46 | +itk_image_view = itk.image_view_from_array(array_copy) |
30 | 47 |
|
31 | 48 | # Convert back to ITK, data is copied |
32 | | -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 |
| 54 | + |
| 55 | +# Save result |
| 56 | +itk.imwrite(itk_image_view, output_image_filename) |
| 57 | + |
| 58 | +# Convert back to itk image data structure |
| 59 | +itk_image = itk.image_from_dict(image_dict) |
| 60 | + |
| 61 | + |
| 62 | +# Read input mesh |
| 63 | +itk_mesh = itk.meshread(input_mesh_filename) |
| 64 | + |
| 65 | +# Convert to standard Python data types + NumPy arrays |
| 66 | +mesh_dict = itk.dict_from_mesh(itk_mesh) |
| 67 | + |
| 68 | + |
| 69 | +# Do interesting things... |
| 70 | + |
| 71 | + |
| 72 | +# Convert back to itk mesh data structure |
| 73 | +itk_mesh = itk.mesh_from_dict(mesh_dict) |
| 74 | + |
| 75 | +# Save result |
| 76 | +itk.meshwrite(itk_mesh, output_mesh_filename) |
| 77 | + |
| 78 | + |
| 79 | +# itk.Mesh inherits from itk.PointSet, |
| 80 | +# create a PointSet from the Mesh |
| 81 | +itk_pointset = itk.PointSet[itk.F, 3].New() |
| 82 | +itk_pointset.SetPoints(itk_mesh.GetPoints()) |
| 83 | +itk_pointset.SetPointData(itk_mesh.GetPointData()) |
| 84 | + |
| 85 | +# Convert to standard Python data types + NumPy arrays |
| 86 | +pointset_dict = itk.pointset_from_dict(itk_pointset) |
| 87 | + |
| 88 | + |
| 89 | +# Do interesting things... |
| 90 | + |
| 91 | + |
| 92 | +# Convert back to itk pointset instance |
| 93 | +itk_pointset = itk.pointset_from_dict(pointset_dict) |
| 94 | + |
| 95 | + |
| 96 | +# Read input transforms |
| 97 | +# |
| 98 | +# This is a Python list |
| 99 | +# |
| 100 | +# When there is more than one transformation |
| 101 | +# the list defines a transformation chain |
| 102 | +itk_transforms = itk.transformread(input_transform_filename) |
| 103 | + |
| 104 | +# Convert to standard Python data types + NumPy arrays |
| 105 | +transform_dicts = [itk.dict_from_transform(t) for t in itk_transforms] |
| 106 | + |
| 107 | + |
| 108 | +# Do interesting things... |
| 109 | + |
| 110 | + |
| 111 | +# Convert back to itk transform instance |
| 112 | +itk_transforms = [itk.transform_from_dict(t) for t in transform_dicts] |
33 | 113 |
|
34 | 114 | # Save result |
35 | | -itk.imwrite(itk_np_view, output_filename) |
| 115 | +itk.transformwrite(itk_transforms, output_transform_filename) |
| 116 | + |
36 | 117 |
|
37 | 118 | # VNL matrix from np.ndarray |
38 | 119 | arr = np.zeros([3,3], np.uint8) |
|
53 | 134 |
|
54 | 135 | # np.ndarray from itk.Matrix |
55 | 136 | arr = itk.array_from_matrix(mat) |
| 137 | +# Equivalent |
| 138 | +arr = np.asarray(mat) |
0 commit comments