Skip to content

Commit

Permalink
Merge pull request InsightSoftwareConsortium#4648 from thewtex/master
Browse files Browse the repository at this point in the history
BUG: array_from_image shape for VectorImage with 1 component
  • Loading branch information
thewtex committed May 7, 2024
2 parents e2cdab4 + 2447a81 commit 9b8963e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Modules/Bridge/NumPy/wrapping/PyBuffer.i.in
Expand Up @@ -9,6 +9,7 @@
i.e. k,j,i versus i,j,k. However C-order indexing is expected by most
algorithms in NumPy / SciPy.
"""
import itk

if image.GetBufferPointer() is None:
return None
Expand All @@ -24,7 +25,7 @@
dim = len(itksize)
shape = [int(itksize[idx]) for idx in range(dim)]

if(image.GetNumberOfComponentsPerPixel() > 1):
if image.GetNumberOfComponentsPerPixel() > 1 or isinstance(image, itk.VectorImage):
shape = [image.GetNumberOfComponentsPerPixel(), ] + shape

if keep_axes == False:
Expand Down
13 changes: 13 additions & 0 deletions Modules/Bridge/NumPy/wrapping/test/itkPyBufferTest.py
Expand Up @@ -197,6 +197,19 @@ def test_NumPyBridge_itkVectorImage(self):
self.assertEqual(vectorndarr.dtype, convertedvectorImage.dtype)
self.assertTupleEqual(vectorndarr.shape, convertedvectorImage.shape)

vectorImage = VectorImageType.New()
vectorImage.SetRegions(region)
vectorImage.SetNumberOfComponentsPerPixel(1)
vectorImage.Allocate()
vectorndarr = itk.PyBuffer[VectorImageType].GetArrayViewFromImage(vectorImage)

convertedvectorImage = itk.PyBuffer[VectorImageType].GetImageViewFromArray(
vectorndarr, is_vector=True
)
self.assertEqual(vectorndarr.ndim, convertedvectorImage.ndim)
self.assertEqual(vectorndarr.dtype, convertedvectorImage.dtype)
self.assertTupleEqual(vectorndarr.shape, convertedvectorImage.shape)

def test_NumPyBridge_itkRGBImage(self):
"Try to convert an RGB ITK image to NumPy array view"

Expand Down
8 changes: 6 additions & 2 deletions Wrapping/Generators/Python/PyBase/pyBase.i
Expand Up @@ -514,8 +514,10 @@ str = str
def ndim(self):
"""Equivalent to the np.ndarray ndim attribute when converted
to an image with itk.array_view_from_image."""
import itk
spatial_dims = self.GetImageDimension()
if self.GetNumberOfComponentsPerPixel() > 1:
if self.GetNumberOfComponentsPerPixel() > 1 or isinstance(self, itk.VectorImage):
return spatial_dims + 1
else:
return spatial_dims
Expand All @@ -524,11 +526,13 @@ str = str
def shape(self):
"""Equivalent to the np.ndarray shape attribute when converted
to an image with itk.array_view_from_image."""
import itk
itksize = self.GetLargestPossibleRegion().GetSize()
dim = len(itksize)
result = [int(itksize[idx]) for idx in range(dim)]
if(self.GetNumberOfComponentsPerPixel() > 1):
if self.GetNumberOfComponentsPerPixel() > 1 or isinstance(self, itk.VectorImage):
result = [self.GetNumberOfComponentsPerPixel(), ] + result
# ITK is C-order. The shape needs to be reversed unless we are a view on
# a NumPy array that is Fortran-order.
Expand Down

0 comments on commit 9b8963e

Please sign in to comment.