Skip to content

Commit

Permalink
ENH: Update Bridge Numpy (2016.12.05)
Browse files Browse the repository at this point in the history
Francois Budin (3):
      BUG: Wrap RGB, RGBA, and Vector images types
      ENH: Adding test for ITK images with RGB/RGBA/Vector pixels
      ENH: Adding conversion from VNL objects to Numpy objects

Matt McCormick (3):
      Merge pull request InsightSoftwareConsortium#9 from fbudin69500/WrapRGBRGBAVectorImages
      Merge pull request InsightSoftwareConsortium#10 from fbudin69500/Add_tests_for_RGB_RGBA_Vector_images
      Merge pull request InsightSoftwareConsortium#11 from fbudin69500/numpy_vnl_objects

Diff:
InsightSoftwareConsortium/ITKBridgeNumPy@9b5b17a...ab8df8c

Change-Id: Iffd201bb5b438cac64199bc969ba9b4ae44816e2
  • Loading branch information
fbudin69500 committed Dec 13, 2016
1 parent 0f5b8f9 commit 4779d40
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Modules/Remote/BridgeNumPy.remote.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,17 @@ itk_fetch_module(BridgeNumPy
array = itk.PyBuffer[ImageType].GetArrayFromImage(image)
image = itk.PyBuffer[ImageType].GetImageFromArray(array)
It also enables conversion of a VNL vector or matrix into a NumPy array and
back. For example:
import itk
v = itk.vnl_vector[itk.F]()
v.set_size(2)
arr = itk.PyVnl[itk.F].GetArrayFromVnlVector(v)
v = itk.PyVnl[itk.F].GetVnlVectorFromArray(arr)
See http://insight-journal.org/browse/publication/85
https://hdl.handle.net/1926/188"
GIT_REPOSITORY ${git_protocol}://github.com/InsightSoftwareConsortium/ITKBridgeNumPy.git
GIT_TAG 9b5b17a462e88478f3ea780b805b6a3a5e10dee5
GIT_TAG ab8df8c1c8494416d5c8471382413c49c9163d9b
)
1 change: 1 addition & 0 deletions Utilities/Maintenance/BuildHeaderTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
'itkFFTWComplexToComplexFFTImageFilter.h',
'itkFFTWCommon.h',
'itkPyBuffer.h', # needs Python.h, etc
'itkPyVnl.h', # needs Python.h, etc
'itkVanHerkGilWermanErodeDilateImageFilter.h', # circular include's
'itkBSplineDeformableTransform.h', # deprecated
'vtkCaptureScreen.h', # these includes require VTK
Expand Down
47 changes: 47 additions & 0 deletions Wrapping/Generators/Python/itkExtras.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,53 @@ def GetImageFromArray(arr):
ImageType = itk.Image[PixelType,arr.ndim]
return itk.PyBuffer[ImageType].GetImageFromArray(arr)

def GetArrayFromVnlVector(vnlVector):
"""Get an Array with the content of the vnlVector
"""
# Check for numpy
if not HAVE_NUMPY:
raise ImportError('Numpy not available.')
# Finds the vnl vector type
import itk
VectorPixelType = itk.template(vnlVector)[1][0]
keys = [k for k in itk.PyVnl.keys() if k[0] == VectorPixelType]
if len(keys ) == 0:
raise RuntimeError("No suitable template parameter can be found.")
# Create a numpy array of the type of the vnl vector
return itk.PyVnl[keys[0]].GetArrayFromVnlVector(vnlVector)

def GetVnlVectorFromArray(arr):
"""Get a vnl vector from a Python array.
"""
if not HAVE_NUMPY:
raise ImportError('Numpy not available.')
import itk
PixelType = _get_itk_pixelid(arr)
return itk.PyVnl[PixelType].GetVnlVectorFromArray(arr)

def GetArrayFromVnlMatrix(vnlMatrix):
"""Get an Array with the content of the vnl matrix
"""
# Check for numpy
if not HAVE_NUMPY:
raise ImportError('Numpy not available.')
# Finds the vnl matrix type
import itk
PixelType = itk.template(vnlMatrix)[1][0]
keys = [k for k in itk.PyVnl.keys() if k[0] == PixelType]
if len(keys ) == 0:
raise RuntimeError("No suitable template parameter can be found.")
# Create a numpy array of the type of the vnl matrix
return itk.PyVnl[keys[0]].GetArrayFromVnlMatrix(vnlMatrix)

def GetVnlMatrixFromArray(arr):
"""Get a vnl matrix from a Python array.
"""
if not HAVE_NUMPY:
raise ImportError('Numpy not available.')
import itk
PixelType = _get_itk_pixelid(arr)
return itk.PyVnl[PixelType].GetVnlMatrixFromArray(arr)

# return an image
from itkTemplate import image, output
Expand Down

0 comments on commit 4779d40

Please sign in to comment.