Skip to content

Commit

Permalink
BUG: mat mul not *
Browse files Browse the repository at this point in the history
  • Loading branch information
stnava committed Jul 6, 2019
1 parent 5ed4d11 commit 30b0038
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions ants/utils/convert_nibabel.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

__all__ = ['to_nibabel', 'from_nibabel']
__all__ = ["to_nibabel", "from_nibabel"]

import os
from tempfile import mktemp
Expand All @@ -12,13 +11,19 @@ def to_nibabel(image):
Convert an ANTsImage to a Nibabel image
"""
if image.dimension != 3:
raise ValueError('Only 3D images currently supported')
raise ValueError("Only 3D images currently supported")

import nibabel as nib

array_data = image.numpy()
affine = np.hstack([image.direction*np.diag(image.spacing),np.array(image.origin).reshape(3,1)])
affine = np.vstack([affine, np.array([0,0,0,1.])])
affine[:2,:] *= -1
affine = np.hstack(
[
np.matmul(image.direction, np.diag(image.spacing)),
np.array(image.origin).reshape(3, 1),
]
)
affine = np.vstack([affine, np.array([0, 0, 0, 1.0])])
affine[:2, :] *= -1
new_img = nib.Nifti1Image(array_data, affine)
return new_img

Expand All @@ -27,7 +32,7 @@ def from_nibabel(nib_image):
"""
Convert a nibabel image to an ANTsImage
"""
tmpfile = mktemp(suffix='.nii.gz')
tmpfile = mktemp(suffix=".nii.gz")
nib_image.to_filename(tmpfile)
new_img = iio2.image_read(tmpfile)
os.remove(tmpfile)
Expand Down

0 comments on commit 30b0038

Please sign in to comment.