Skip to content

Commit

Permalink
ENH: add support for direction in VTK image
Browse files Browse the repository at this point in the history
  • Loading branch information
dave3d committed Jun 25, 2021
1 parent 0809a7d commit 80bcb13
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
18 changes: 18 additions & 0 deletions Wrapping/Generators/Python/Tests/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,10 @@ def custom_callback(name, progress):

print("Testing vtk conversion")
image = itk.image_from_array(np.random.rand(2, 3, 4))
z_rot = np.asarray([[0,1,0], [-1,0,0], [0,0,1]], dtype=np.float64)
z_rot_itk = itk.matrix_from_array(z_rot)
image.SetDirection(z_rot_itk)

vtk_image = itk.vtk_image_from_image(image)
image_round = itk.image_from_vtk_image(vtk_image)
assert np.array_equal(itk.origin(image), itk.origin(image_round))
Expand All @@ -594,10 +598,20 @@ def custom_callback(name, progress):
assert np.array_equal(
itk.array_view_from_image(image), itk.array_view_from_image(image_round)
)
if vtk.vtkVersion.GetVTKMajorVersion()>=9:
z_rot_round = itk.array_from_matrix(image_round.GetDirection())
assert np.array_equal(z_rot, z_rot_round)
else:
print("VTK version <9. Direction unsupported.")


image = itk.image_from_array(
np.random.rand(5, 4, 2).astype(np.float32), is_vector=True
)
z_rot = np.asarray([[0,1], [-1,0]], dtype=np.float64)
z_rot_itk = itk.matrix_from_array(z_rot)
image.SetDirection(z_rot_itk)

vtk_image = itk.vtk_image_from_image(image)
image_round = itk.image_from_vtk_image(vtk_image)
assert np.array_equal(itk.origin(image), itk.origin(image_round))
Expand All @@ -606,6 +620,10 @@ def custom_callback(name, progress):
assert np.array_equal(
itk.array_view_from_image(image), itk.array_view_from_image(image_round)
)
if vtk.vtkVersion.GetVTKMajorVersion()>=9:
z_rot_round = itk.array_from_matrix(image_round.GetDirection())
assert np.array_equal(z_rot, z_rot_round)

except ImportError:
print("vtk not imported. Skipping vtk conversion tests")
pass
29 changes: 27 additions & 2 deletions Wrapping/Generators/Python/itk/support/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,17 @@ def vtk_image_from_image(l_image: "itkt.ImageOrImageSource") -> "vtk.vtkImageDat
dims = [1] * 3
dims[:dim] = itk.size(l_image)
vtk_image.SetDimensions(dims)
# Todo: Add Direction with VTK 9
# Copy direction matrix for VTK>=9
import vtk
if vtk.vtkVersion.GetVTKMajorVersion()>=9:
l_direction = l_image.GetDirection()
direction = itk.array_from_matrix(l_direction).flatten().tolist()
if len(direction) == 4:
# Change 2d matrix to 3d
direction = [ direction[0], direction[1], 0.0,
direction[2], direction[3], 0.0,
0.0, 0.0, 1.0 ]
vtk_image.SetDirectionMatrix(direction)
if l_image.GetImageDimension() == 3:
PixelType = itk.template(l_image)[1][0]
if PixelType == itk.Vector:
Expand Down Expand Up @@ -772,7 +782,22 @@ def image_from_vtk_image(vtk_image: "vtk.vtkImageData") -> "itkt.ImageBase":
l_origin = [0.0] * dim
l_origin[:dim] = vtk_image.GetOrigin()[:dim]
l_image.SetOrigin(l_origin)
# Todo: Add Direction with VTK 9
# Direction support with VTK 9
import vtk
if vtk.vtkVersion.GetVTKMajorVersion()>=9:
direction = vtk_image.GetDirectionMatrix()
if dim==3:
direction_array = np.identity(3)
for y in (0,1,2):
for x in (0,1,2):
direction_array[x,y] = direction.GetElement(x,y)
elif dim==2:
direction_array = np.identity(2)
for y in (0,1):
for x in (0,1):
direction_array[x,y] = direction.GetElement(x,y)
l_direction = itk.matrix_from_array(direction_array)
l_image.SetDirection(l_direction)
return l_image


Expand Down

0 comments on commit 80bcb13

Please sign in to comment.