Skip to content

Commit 2447a81

Browse files
committed
BUG: array_from_image shape for VectorImage with 1 component
A VectorImage with one component should have a shape with a value of 1 for component. Also make the ndim consistent. Closes #4646
1 parent 4926c2f commit 2447a81

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

Modules/Bridge/NumPy/wrapping/PyBuffer.i.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
i.e. k,j,i versus i,j,k. However C-order indexing is expected by most
1010
algorithms in NumPy / SciPy.
1111
"""
12+
import itk
1213

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

27-
if(image.GetNumberOfComponentsPerPixel() > 1):
28+
if image.GetNumberOfComponentsPerPixel() > 1 or isinstance(image, itk.VectorImage):
2829
shape = [image.GetNumberOfComponentsPerPixel(), ] + shape
2930

3031
if keep_axes == False:

Modules/Bridge/NumPy/wrapping/test/itkPyBufferTest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,19 @@ def test_NumPyBridge_itkVectorImage(self):
197197
self.assertEqual(vectorndarr.dtype, convertedvectorImage.dtype)
198198
self.assertTupleEqual(vectorndarr.shape, convertedvectorImage.shape)
199199

200+
vectorImage = VectorImageType.New()
201+
vectorImage.SetRegions(region)
202+
vectorImage.SetNumberOfComponentsPerPixel(1)
203+
vectorImage.Allocate()
204+
vectorndarr = itk.PyBuffer[VectorImageType].GetArrayViewFromImage(vectorImage)
205+
206+
convertedvectorImage = itk.PyBuffer[VectorImageType].GetImageViewFromArray(
207+
vectorndarr, is_vector=True
208+
)
209+
self.assertEqual(vectorndarr.ndim, convertedvectorImage.ndim)
210+
self.assertEqual(vectorndarr.dtype, convertedvectorImage.dtype)
211+
self.assertTupleEqual(vectorndarr.shape, convertedvectorImage.shape)
212+
200213
def test_NumPyBridge_itkRGBImage(self):
201214
"Try to convert an RGB ITK image to NumPy array view"
202215

Wrapping/Generators/Python/PyBase/pyBase.i

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,8 +514,10 @@ str = str
514514
def ndim(self):
515515
"""Equivalent to the np.ndarray ndim attribute when converted
516516
to an image with itk.array_view_from_image."""
517+
import itk
518+
517519
spatial_dims = self.GetImageDimension()
518-
if self.GetNumberOfComponentsPerPixel() > 1:
520+
if self.GetNumberOfComponentsPerPixel() > 1 or isinstance(self, itk.VectorImage):
519521
return spatial_dims + 1
520522
else:
521523
return spatial_dims
@@ -524,11 +526,13 @@ str = str
524526
def shape(self):
525527
"""Equivalent to the np.ndarray shape attribute when converted
526528
to an image with itk.array_view_from_image."""
529+
import itk
530+
527531
itksize = self.GetLargestPossibleRegion().GetSize()
528532
dim = len(itksize)
529533
result = [int(itksize[idx]) for idx in range(dim)]
530534
531-
if(self.GetNumberOfComponentsPerPixel() > 1):
535+
if self.GetNumberOfComponentsPerPixel() > 1 or isinstance(self, itk.VectorImage):
532536
result = [self.GetNumberOfComponentsPerPixel(), ] + result
533537
# ITK is C-order. The shape needs to be reversed unless we are a view on
534538
# a NumPy array that is Fortran-order.

0 commit comments

Comments
 (0)