-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add SimpleITK.Image to ANTsImage conversion (#726)
* add sitk_to_ants * move test to test_utils.py, install SimpleITK so test runs in coverage --------- Co-authored-by: Bryn Lloyd <12702862+dyollb@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
117 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
__all__ = ["nifti_to_ants"] | ||
|
||
import os | ||
from tempfile import mkstemp | ||
import numpy as np | ||
import ants | ||
|
||
|
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,63 @@ | ||
import numpy as np | ||
import ants | ||
|
||
|
||
def from_sitk(sitk_image: "SimpleITK.Image") -> ants.ANTsImage: | ||
""" | ||
Converts a given SimpleITK image into an ANTsPy image | ||
Parameters | ||
---------- | ||
img: SimpleITK.Image | ||
Returns | ||
------- | ||
ants_image: ANTsImage | ||
""" | ||
import SimpleITK as sitk | ||
|
||
ndim = sitk_image.GetDimension() | ||
|
||
if ndim < 3: | ||
print("Dimensionality is less than 3.") | ||
return None | ||
|
||
direction = np.asarray(sitk_image.GetDirection()).reshape((3, 3)) | ||
spacing = list(sitk_image.GetSpacing()) | ||
origin = list(sitk_image.GetOrigin()) | ||
|
||
data = sitk.GetArrayViewFromImage(sitk_image) | ||
|
||
ants_img: ants.ANTsImage = ants.from_numpy( | ||
data=data.ravel(order="F").reshape(data.shape[::-1]), | ||
origin=origin, | ||
spacing=spacing, | ||
direction=direction, | ||
) | ||
|
||
return ants_img | ||
|
||
|
||
def to_sitk(ants_image: ants.ANTsImage) -> "SimpleITK.Image": | ||
""" | ||
Converts a given ANTsPy image into an SimpleITK image | ||
Parameters | ||
---------- | ||
ants_image: ANTsImage | ||
Returns | ||
------- | ||
img: SimpleITK.Image | ||
""" | ||
|
||
import SimpleITK as sitk | ||
|
||
data = ants_image.view() | ||
shape = ants_image.shape | ||
|
||
sitk_img = sitk.GetImageFromArray(data.ravel(order="F").reshape(shape[::-1])) | ||
sitk_img.SetOrigin(ants_image.origin) | ||
sitk_img.SetSpacing(ants_image.spacing) | ||
sitk_img.SetDirection(ants_image.direction.flatten()) | ||
return sitk_img |
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