1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
%extend itkPyVnl@PyVnlTypes@{
%pythoncode %{
def GetArrayViewFromVnlVector(vnl_vector):
"""Get a NumPy array view of a VNL vector."""
itksize = vnl_vector.size()
shape = [itksize]
pixelType = "@PixelType@"
numpy_dtype = _get_numpy_pixelid(pixelType)
memview = itkPyVnl@PyVnlTypes@._GetArrayViewFromVnlVector(vnl_vector)
ndarr_view = np.asarray(memview).view(dtype = numpy_dtype).reshape(shape).view(np.ndarray)
itk_view = NDArrayITKBase(ndarr_view, vnl_vector)
return itk_view
GetArrayViewFromVnlVector = staticmethod(GetArrayViewFromVnlVector)
def GetArrayFromVnlVector(vnl_vector):
"""Get a NumPy ndarray from VNL Vector.
This is a deep copy of the VNL vector and is completely safe and without potential side effects.
"""
arrayView = itkPyVnl@PyVnlTypes@.GetArrayViewFromVnlVector(vnl_vector)
# perform deep copy of the buffer
return np.array(arrayView, copy=True)
GetArrayFromVnlVector = staticmethod(GetArrayFromVnlVector)
def GetVnlVectorFromArray(ndarr):
"""Get a VNL vector from a NumPy array.
This is a deep copy of the NumPy array buffer and is completely safe without potential
side effects. It is not possible to have a view of a numpy array in a VNL vector since
there is no VNL vector constructor that allows sharing data.
"""
assert ndarr.ndim == 1 , \
"Only arrays of 1 dimension are supported."
ndarr = np.ascontiguousarray(ndarr)
vec = itkPyVnl@PyVnlTypes@._GetVnlVectorFromArray( ndarr, ndarr.shape)
return vec
GetVnlVectorFromArray = staticmethod(GetVnlVectorFromArray)
def GetArrayViewFromVnlMatrix(vnl_matrix):
"""Get a NumPy array view of a VNL matrix."""
cols = vnl_matrix.columns()
rows = vnl_matrix.rows()
dim = 2
shape = [rows,cols]
pixelType = "@PixelType@"
numpy_dtype = _get_numpy_pixelid(pixelType)
memview = itkPyVnl@PyVnlTypes@._GetArrayViewFromVnlMatrix(vnl_matrix)
ndarr_view = np.asarray(memview).view(dtype = numpy_dtype).reshape(shape).view(np.ndarray)
itk_view = NDArrayITKBase(ndarr_view, vnl_matrix)
return itk_view
GetArrayViewFromVnlMatrix = staticmethod(GetArrayViewFromVnlMatrix)
def GetArrayFromVnlMatrix(vnl_matrix):
"""Get a NumPy ndarray from VNL matrix.
This is a deep copy of the VNL matrix and is completely safe and without potential side effects.
"""
arrayView = itkPyVnl@PyVnlTypes@.GetArrayViewFromVnlMatrix(vnl_matrix)
# perform deep copy of the buffer
return np.array(arrayView, copy=True)
GetArrayFromVnlMatrix = staticmethod(GetArrayFromVnlMatrix)
def GetVnlMatrixFromArray(ndarr):
"""Get a VNL Matrix from a NumPy array.
This is a deep copy of the NumPy array buffer and is completely safe without potential
side effects. It is not possible to have a view of a numpy array in a VNL matrix since there is no
VNL matrix constructor that allows sharing data.
"""
assert ndarr.ndim == 2 , \
"Only arrays of 2 dimensions are supported."
ndarr = np.ascontiguousarray(ndarr)
mat = itkPyVnl@PyVnlTypes@._GetVnlMatrixFromArray( ndarr, ndarr.shape)
return mat
GetVnlMatrixFromArray = staticmethod(GetVnlMatrixFromArray)
%}
};
|