Created
March 30, 2017 13:35
-
-
Save MSchnei/0fe70453ce94baadc30b9bd5354f6529 to your computer and use it in GitHub Desktop.
Code snippet to shift nii data by specified number of voxels along specified axis
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
""" | |
Created on Thu Mar 30 11:55:27 2017 | |
@author: marian | |
""" | |
import os | |
import numpy as np | |
from nibabel import load, save, Nifti1Image | |
# %% Set parameters | |
# set path to volume that shoudl be shifted | |
pathIn = '/home/marian/Documents/Testing/hausdorfTests/hdSphere2Vox_innBrdr.nii.gz' | |
# set the number of voxels by which volume should be shifted | |
shift = 2 | |
# set the axis along which the shift should occur | |
axis = 2 | |
# %% Load Data | |
nii = load(pathIn) | |
basename = nii.get_filename().split(os.extsep, 1)[0] | |
dirname = os.path.dirname(nii.get_filename()) | |
orig = nii.get_data() # original data | |
data = nii.get_data() | |
borders = np.where(data > 0) | |
borders = list(borders) | |
borders[axis] = borders[axis]+shift | |
data = np.zeros((data.shape)) | |
data[borders] = 1 | |
data = data.astype('int') | |
# %% save shifted vol as nifti | |
out = Nifti1Image(data, header=nii.header, affine=nii.affine) | |
save(out, basename + "_shiftedVox"+str(shift)+"Axis"+str(axis)+".nii.gz") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment