Last active
January 18, 2021 19:00
-
-
Save ofgulban/df2a0a2437a13055b84e09dd4a345c91 to your computer and use it in GitHub Desktop.
Make affine matrix of a nifti file identity matrix.
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
"Make affine matrix of a nifti file identity matrix." | |
import os | |
import nibabel as nb | |
import numpy as np | |
NII = "/path/to/tile.nii.gz" | |
nii = nb.load(NII) | |
data = np.asarray(nii.dataobj) | |
hdr = nii.header | |
hdr.set_sform(hdr.get_qform()) | |
aff = hdr.get_qform() | |
img = nb.Nifti1Image(data, affine=aff, header=hdr) | |
basename, ext = nii.get_filename().split(os.extsep, 1) | |
out_name = '{}_{}.{}'.format(basename, "fixed", ext) | |
nb.save(img, out_name) | |
print("Finished.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment