forked from NifTK/NiftyNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Code to enable NN to read other file types with support for SimpleITK
loading if installed
- Loading branch information
Eli GIBSON
committed
Jun 27, 2017
1 parent
fd717f8
commit 2ca006a
Showing
5 changed files
with
69 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import SimpleITK as sitk | ||
import nibabel | ||
import numpy as np | ||
|
||
|
||
class SimpleITKAsNibabel(nibabel.spatialimages.SpatialImage): | ||
''' Minimal interface to use a SimpleITK image as if it were | ||
a nibabel object. Currently only supports the subset of the | ||
interface used by NiftyNet and is read only | ||
''' | ||
def __init__(self, filename): | ||
try: | ||
self._SimpleITKImage = sitk.ReadImage(filename) | ||
except RuntimeError as err: | ||
if 'Unable to determine ImageIO reader' in str(err): | ||
nibabel.filebasedimages.ImageFileError(str(err)) | ||
else: | ||
raise | ||
self._header = SimpleITKAsNibabelHeader(self._SimpleITKImage) | ||
# get affine transform | ||
c=np.array([self._SimpleITKImage.TransformContinuousIndexToPhysicalPoint(p) for p in ((1,0,0),(0,1,0),(0,0,1),(0,0,0))]) | ||
affine = np.transpose(np.concatenate([np.concatenate([c[0:3]-c[3:],c[3:]],0),[[0.],[0.],[0.],[1.]]],1)) | ||
super(SimpleITKAsNibabel,self).__init__(sitk.GetArrayFromImage(self._SimpleITKImage), affine) | ||
class SimpleITKAsNibabelHeader(nibabel.spatialimages.SpatialHeader): | ||
def __init__(self, image_reference): | ||
super(SimpleITKAsNibabelHeader,self).__init__(data_dtype=sitk.GetArrayViewFromImage(image_reference).dtype,shape=sitk.GetArrayViewFromImage(image_reference).shape, | ||
zooms=image_reference.GetSpacing()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters