Skip to content

Commit

Permalink
ENH: Add test and strengthen the documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
andinet committed May 19, 2022
1 parent d6a3af1 commit 03ae7de
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Bridge/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ if(ENABLE_QUICKVIEW)
add_subdirectory_if_module_enabled(VtkGlue)
endif()

add_subdirectory(NumPy)
add_subdirectory_if_module_enabled( NumPy )
3 changes: 3 additions & 0 deletions src/Bridge/NumPy/ConvertNumPyArrayToitkImage/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ find_package( ITK REQUIRED )
include( ${ITK_USE_FILE} )

if(ITK_WRAP_PYTHON)
enable_testing()
add_test(NAME ConvertNumPyArrayToitkImageTestPython
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Code.py
Slice.png
SliceRevised.png
)
endif()
49 changes: 41 additions & 8 deletions src/Bridge/NumPy/ConvertNumPyArrayToitkImage/Code.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,50 @@
print("Usage: " + sys.argv[0] + " <input_image> <output_image>")
sys.exit(1)

input_file_name = sys.argv[1]
# Parse comamnd line arguments
input_file_name = sys.argv[1]
output_file_name = sys.argv[2]

# Read input image
itk_image = itk.imread(input_file_name)
PixelType = itk.ctype("unsigned char")
ImageType = itk.Image[itk.UC, 2]
itk_image = itk.imread(input_file_name, PixelType)
OriginalRegion = itk_image.GetLargestPossibleRegion()
OriginalSize = OriginalRegion.GetSize()

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

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

# Write out the numpy array as an ITK image
itk.imwrite(itk_np_copy,output_file_name)
# ii) Generate a copy of the data using using array_from_image function.
# You can then freely modify the data as it has no effect on the original ITK image.

# Copy itk.Image pixel data to numpy array
np_array = itk.GetArrayFromImage(itk_image, ttype=ImageType)
print(f"The size of the NumPy array copied from itk::Image = {np_array.shape}")

# Create an ITK image from the numpy array and then write it out
itk_np = itk.GetImageFromArray(np.ascontiguousarray(np_array))

region = itk_np.GetLargestPossibleRegion()
size = region.GetSize()
print(f"ITK image data size after convesion from NumPy = {size}\n")

itk.imwrite(itk_np, output_file_name)


# The order of the indexes is according to the type of object the data is stored in.
# A numpy array is indexed by [row , col ] for 2D data or [slice , row , col ] for 3D data.
# While ITK image data is indexed by (x , y ) for 2D or (x , y , z ) for 3D data.
# To demonstrate here, we create a 2D pixel array data
# and access a pixel with the same indices

np_data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], np.int32)
itk_np_view_data = itk.image_view_from_array(np_data)

print(f"ITK image data pixel value at [2,1] = {itk_np_view_data.GetPixel([2,1])}")
print(f"NumPy array pixel value at [2,1] = {np_data[2,1]}")
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ Interface ITK with NumPy Array
Synopsis
--------

This example illustrates interfacing ITK Image data with NumPy array.

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

Results
-------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
59ac2cc1a14666a72b9c21c1abd1a45aeef567d2e4690b761df2b36904dbc37f52458b54e6a22b32186f13fe45a4c91da6e9e0c3bcfbe0e959c81b514fb97237
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
64dd1f56f598fb9dfe8ebbf725a5fe1530ecabe1962661376e1228a3640c31316741d7c703f482adfaf9086762b44018f05a863b37bb19608735c7fd8ba57ca0
Binary file not shown.

0 comments on commit 03ae7de

Please sign in to comment.