Skip to content

Commit 336be62

Browse files
committed
ENH: Better support for multi-component images in image_from_vtk_image
1 parent 2b88048 commit 336be62

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

Wrapping/Generators/Python/Tests/extras.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,18 @@ def custom_callback(name, progress):
541541
try:
542542
import vtk
543543

544+
def assert_images_equal(base, test):
545+
assert np.array_equal(itk.origin(base), itk.origin(test))
546+
assert np.array_equal(itk.spacing(base), itk.spacing(test))
547+
assert np.array_equal(itk.size(base), itk.size(test))
548+
assert np.array_equal(
549+
itk.array_view_from_image(base), itk.array_view_from_image(test)
550+
)
551+
if vtk.vtkVersion.GetVTKMajorVersion() >= 9:
552+
z_rot_base = itk.array_from_matrix(base.GetDirection())
553+
z_rot_test = itk.array_from_matrix(test.GetDirection())
554+
assert np.array_equal(z_rot_base, z_rot_test)
555+
544556
print("Testing vtk conversion")
545557
image = itk.image_from_array(np.random.rand(2, 3, 4))
546558
z_rot = np.asarray([[0, 1, 0], [-1, 0, 0], [0, 0, 1]], dtype=np.float64)
@@ -570,15 +582,16 @@ def custom_callback(name, progress):
570582

571583
vtk_image = itk.vtk_image_from_image(image)
572584
image_round = itk.image_from_vtk_image(vtk_image)
573-
assert np.array_equal(itk.origin(image), itk.origin(image_round))
574-
assert np.array_equal(itk.spacing(image), itk.spacing(image_round))
575-
assert np.array_equal(itk.size(image), itk.size(image_round))
576-
assert np.array_equal(
577-
itk.array_view_from_image(image), itk.array_view_from_image(image_round)
578-
)
579-
if vtk.vtkVersion.GetVTKMajorVersion() >= 9:
580-
z_rot_round = itk.array_from_matrix(image_round.GetDirection())
581-
assert np.array_equal(z_rot, z_rot_round)
585+
assert_images_equal(image, image_round)
586+
587+
for components in [2, 3, 4, 5, 10]:
588+
for size in [(12, 8, components), (4, 6, 8, components)]: # 2D and 3D
589+
numpy_image = np.random.rand(12, 8, components).astype(np.float32)
590+
input_image = itk.image_from_array(numpy_image, is_vector=True)
591+
# test itk -> vtk -> itk round trip
592+
vtk_image = itk.vtk_image_from_image(input_image)
593+
itk_image = itk.image_from_vtk_image(vtk_image)
594+
assert_images_equal(input_image, itk_image)
582595

583596
except ImportError:
584597
print("vtk not imported. Skipping vtk conversion tests")

Wrapping/Generators/Python/itk/support/extras.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -745,15 +745,14 @@ def image_from_vtk_image(vtk_image: "vtk.vtkImageData") -> "itkt.ImageBase":
745745
point_data = vtk_image.GetPointData()
746746
array = vtk_to_numpy(point_data.GetScalars())
747747
array = array.reshape(-1)
748-
is_vector = point_data.GetScalars().GetNumberOfComponents() != 1
748+
number_of_components = point_data.GetScalars().GetNumberOfComponents()
749+
is_vector = number_of_components != 1
749750
dims = list(vtk_image.GetDimensions())
750751
if is_vector and dims[-1] == 1:
751-
# 2D
752-
dims = dims[:2]
753-
dims.reverse()
754-
dims.append(point_data.GetScalars().GetNumberOfComponents())
755-
else:
756-
dims.reverse()
752+
dims = dims[:-1] # 2D, not 3D
753+
dims.reverse()
754+
if is_vector:
755+
dims.append(number_of_components)
757756
array.shape = tuple(dims)
758757
l_image = itk.image_view_from_array(array, is_vector)
759758

0 commit comments

Comments
 (0)