Skip to content
Snippets Groups Projects
Commit 7924e119 authored by Johannes Stelzer's avatar Johannes Stelzer
Browse files

Nifti1 -> Nifti2

parent a33dbf43
No related branches found
No related tags found
No related merge requests found
...@@ -11,7 +11,7 @@ import numpy as np ...@@ -11,7 +11,7 @@ import numpy as np
from nibabel import load from nibabel import load
from nibabel.affines import apply_affine from nibabel.affines import apply_affine
from nibabel.volumeutils import shape_zoom_affine from nibabel.volumeutils import shape_zoom_affine
from nibabel import Nifti1Image from nibabel import Nifti2Image
import pyqtgraph as pg import pyqtgraph as pg
...@@ -55,13 +55,13 @@ def loadImageFromNifti(fileobject, pref, f_type): ...@@ -55,13 +55,13 @@ def loadImageFromNifti(fileobject, pref, f_type):
if f_type != 0: if f_type != 0:
color_cm = True color_cm = True
try: try:
image = Nifti1Image(fileobject.get_data(), fileobject.affine) image = Nifti2Image(fileobject.get_data(), fileobject.affine)
hdr = fileobject.header hdr = fileobject.header
except RuntimeError: except RuntimeError:
print("Cannot load Nifti object!") print("Cannot load Nifti object!")
if len(image.get_data().shape) == 2: if len(image.get_data().shape) == 2:
image = Nifti1Image(np.atleast_3d(image.get_data()), image.affine) image = Nifti2Image(np.atleast_3d(image.get_data()), image.affine)
if len(image.get_data().shape) == 3: if len(image.get_data().shape) == 3:
img = Image3D.Image3D(image=image, color=color_cm) img = Image3D.Image3D(image=image, color=color_cm)
elif len(image.get_data().shape) == 4: elif len(image.get_data().shape) == 4:
...@@ -106,14 +106,14 @@ def loadImageFromFile(filename, pref, f_type): ...@@ -106,14 +106,14 @@ def loadImageFromFile(filename, pref, f_type):
if (filetype=='.nii' or filetype=='.gz'): if (filetype=='.nii' or filetype=='.gz'):
try: try:
temp_img = load(filename) temp_img = load(filename)
image = Nifti1Image(temp_img.get_data(), temp_img.affine) image = Nifti2Image(temp_img.get_data(), temp_img.affine)
hdr = temp_img.header hdr = temp_img.header
except RuntimeError: except RuntimeError:
print("Cannot load .nii or nii.gz file given!") print("Cannot load .nii or nii.gz file given!")
elif (filetype=='.hdr' or filetype=='.img'): elif (filetype=='.hdr' or filetype=='.img'):
try: try:
temp_img = load(filename) temp_img = load(filename)
image = Nifti1Image(temp_img.get_data(), temp_img.affine) image = Nifti2Image(temp_img.get_data(), temp_img.affine)
image.dataobj[np.isnan(image.dataobj)] = 0 image.dataobj[np.isnan(image.dataobj)] = 0
hdr = temp_img.header hdr = temp_img.header
except RuntimeError: except RuntimeError:
...@@ -138,7 +138,7 @@ def loadImageFromFile(filename, pref, f_type): ...@@ -138,7 +138,7 @@ def loadImageFromFile(filename, pref, f_type):
# if affine is still build incorrectly, use identity # if affine is still build incorrectly, use identity
if np.linalg.det(affine[0:2, 0:2]) == 0: if np.linalg.det(affine[0:2, 0:2]) == 0:
affine = np.identity(4) affine = np.identity(4)
image = Nifti1Image(img_data.astype(np.float32), affine) image = Nifti2Image(img_data.astype(np.float32), affine)
else: # version 2 .v file else: # version 2 .v file
# sform, or qform, and if not, then fall-back affine # sform, or qform, and if not, then fall-back affine
if sform_code != 0: if sform_code != 0:
...@@ -148,7 +148,7 @@ def loadImageFromFile(filename, pref, f_type): ...@@ -148,7 +148,7 @@ def loadImageFromFile(filename, pref, f_type):
affine = qform_affine affine = qform_affine
else: else:
affine = shape_zoom_affine(img_data.shape, pixdim[1:4], False) affine = shape_zoom_affine(img_data.shape, pixdim[1:4], False)
image = Nifti1Image(img_data.astype(np.float32), affine) image = Nifti2Image(img_data.astype(np.float32), affine)
if sform_code != 0: if sform_code != 0:
image.set_sform(sform) image.set_sform(sform)
if qform_code != 0: if qform_code != 0:
...@@ -161,11 +161,11 @@ def loadImageFromFile(filename, pref, f_type): ...@@ -161,11 +161,11 @@ def loadImageFromFile(filename, pref, f_type):
if np.isnan(image.get_data()).any(): if np.isnan(image.get_data()).any():
notnan = image.get_data() notnan = image.get_data()
notnan[np.isnan(image.get_data())] = 0 notnan[np.isnan(image.get_data())] = 0
image = Nifti1Image(notnan, image.affine) image = Nifti2Image(notnan, image.affine)
# allow 2d-images here: # allow 2d-images here:
if len(image.get_data().shape) == 2: if len(image.get_data().shape) == 2:
image = Nifti1Image(np.atleast_3d(image.get_data()), image.affine) image = Nifti2Image(np.atleast_3d(image.get_data()), image.affine)
if len(image.get_data().shape) == 3: if len(image.get_data().shape) == 3:
img = Image3D.Image3D(image=image, color=color_cm) img = Image3D.Image3D(image=image, color=color_cm)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment