@@ -541,6 +541,18 @@ def custom_callback(name, progress):
541
541
try :
542
542
import vtk
543
543
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
+
544
556
print ("Testing vtk conversion" )
545
557
image = itk .image_from_array (np .random .rand (2 , 3 , 4 ))
546
558
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):
570
582
571
583
vtk_image = itk .vtk_image_from_image (image )
572
584
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 )
582
595
583
596
except ImportError :
584
597
print ("vtk not imported. Skipping vtk conversion tests" )
0 commit comments