|
22 | 22 | # Run filters on itk.Image |
23 | 23 |
|
24 | 24 | # 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) |
26 | 26 |
|
27 | 27 | # 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) |
29 | 29 | # Equivalent |
30 | | -np_copy = np.asarray(itk_image) |
| 30 | +array_copy = np.asarray(itk_image) |
31 | 31 |
|
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 |
33 | 37 | # in standard Python data types + NumPy array |
| 38 | +# Sequences, e.g. spacing, are in xyz (ITK) indexing order |
34 | 39 | image_dict = itk.dict_from_image(itk_image) |
35 | 40 |
|
36 | 41 |
|
37 | 42 | # Do interesting things... |
38 | 43 |
|
39 | 44 |
|
40 | 45 | # 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) |
42 | 47 |
|
43 | 48 | # 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 |
45 | 54 |
|
46 | 55 | # Save result |
47 | | -itk.imwrite(itk_np_view, output_image_filename) |
| 56 | +itk.imwrite(itk_image_view, output_image_filename) |
48 | 57 |
|
49 | 58 | # Convert back to itk image data structure |
50 | 59 | itk_image = itk.image_from_dict(image_dict) |
|
0 commit comments