Skip to content

Commit 03ae7de

Browse files
committed
ENH: Add test and strengthen the documentation
1 parent d6a3af1 commit 03ae7de

File tree

7 files changed

+52
-11
lines changed

7 files changed

+52
-11
lines changed

src/Bridge/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ if(ENABLE_QUICKVIEW)
22
add_subdirectory_if_module_enabled(VtkGlue)
33
endif()
44

5-
add_subdirectory(NumPy)
5+
add_subdirectory_if_module_enabled( NumPy )

src/Bridge/NumPy/ConvertNumPyArrayToitkImage/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ find_package( ITK REQUIRED )
66
include( ${ITK_USE_FILE} )
77

88
if(ITK_WRAP_PYTHON)
9+
enable_testing()
910
add_test(NAME ConvertNumPyArrayToitkImageTestPython
1011
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Code.py
12+
Slice.png
13+
SliceRevised.png
1114
)
1215
endif()

src/Bridge/NumPy/ConvertNumPyArrayToitkImage/Code.py

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,50 @@
2222
print("Usage: " + sys.argv[0] + " <input_image> <output_image>")
2323
sys.exit(1)
2424

25-
input_file_name = sys.argv[1]
25+
# Parse comamnd line arguments
26+
input_file_name = sys.argv[1]
2627
output_file_name = sys.argv[2]
2728

2829
# Read input image
29-
itk_image = itk.imread(input_file_name)
30+
PixelType = itk.ctype("unsigned char")
31+
ImageType = itk.Image[itk.UC, 2]
32+
itk_image = itk.imread(input_file_name, PixelType)
33+
OriginalRegion = itk_image.GetLargestPossibleRegion()
34+
OriginalSize = OriginalRegion.GetSize()
3035

31-
# Copy itk.Imag pixel data to numpy array
32-
np_array = itk.array_from_image(itk_image)
36+
print(f"The size of the ITK image data read from the input file = {OriginalSize}\n")
3337

34-
# Convert the numpy array pixel data back to ITK
35-
itk_np_copy = itk.image_from_array(np_array)
38+
# There are two ways to bridge the data structures.
39+
# i) Give direct access to memory holding the data called "View" functions for displaying
40+
# purpose. But you can't modify the data
41+
np_view_array = itk.GetArrayViewFromImage(itk_image, ttype=ImageType)
42+
print(f"The size of the NumPy array viewed from itk::Image = {np_view_array.shape}")
3643

37-
# Write out the numpy array as an ITK image
38-
itk.imwrite(itk_np_copy,output_file_name)
44+
# ii) Generate a copy of the data using using array_from_image function.
45+
# You can then freely modify the data as it has no effect on the original ITK image.
46+
47+
# Copy itk.Image pixel data to numpy array
48+
np_array = itk.GetArrayFromImage(itk_image, ttype=ImageType)
49+
print(f"The size of the NumPy array copied from itk::Image = {np_array.shape}")
50+
51+
# Create an ITK image from the numpy array and then write it out
52+
itk_np = itk.GetImageFromArray(np.ascontiguousarray(np_array))
53+
54+
region = itk_np.GetLargestPossibleRegion()
55+
size = region.GetSize()
56+
print(f"ITK image data size after convesion from NumPy = {size}\n")
57+
58+
itk.imwrite(itk_np, output_file_name)
59+
60+
61+
# The order of the indexes is according to the type of object the data is stored in.
62+
# A numpy array is indexed by [row , col ] for 2D data or [slice , row , col ] for 3D data.
63+
# While ITK image data is indexed by (x , y ) for 2D or (x , y , z ) for 3D data.
64+
# To demonstrate here, we create a 2D pixel array data
65+
# and access a pixel with the same indices
66+
67+
np_data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], np.int32)
68+
itk_np_view_data = itk.image_view_from_array(np_data)
69+
70+
print(f"ITK image data pixel value at [2,1] = {itk_np_view_data.GetPixel([2,1])}")
71+
print(f"NumPy array pixel value at [2,1] = {np_data[2,1]}")

src/Bridge/NumPy/ConvertNumPyArrayToitkImage/Documentation.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ Interface ITK with NumPy Array
1010
Synopsis
1111
--------
1212

13-
This example illustrates interfacing ITK Image data with NumPy array.
14-
13+
This example illustrates interfacing ITK Image data with NumPy array. While doing this, there are two key issues to
14+
to keep in mind. One, the order of indexes is different between ITK image data class and NumPy array.
15+
Second, there are two ways to access ITK image data as a NumPy array. i) Get direct access to memory with the data called
16+
"View" functions (array_view_from_image and image_view_from_array), or ii) Copy the data (array_from_image and image_from_array).
17+
If the view functions are used, the data can't be modified.
1518

1619
Results
1720
-------
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
59ac2cc1a14666a72b9c21c1abd1a45aeef567d2e4690b761df2b36904dbc37f52458b54e6a22b32186f13fe45a4c91da6e9e0c3bcfbe0e959c81b514fb97237
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
64dd1f56f598fb9dfe8ebbf725a5fe1530ecabe1962661376e1228a3640c31316741d7c703f482adfaf9086762b44018f05a863b37bb19608735c7fd8ba57ca0
Binary file not shown.

0 commit comments

Comments
 (0)