Пример #1
0
# Perform affine registration
# The output is an array-like object such that 
# np.asarray(T) is a customary 4x4 matrix 
print('Setting up registration...')
tic = time.time()
T = register(I, J, 
             similarity=similarity, 
             interp=interp, 
             optimizer=optimizer)
toc = time.time()
print('  Registration time: %f sec' % (toc-tic))


# Resample source image
print('Resampling source image...')
tic = time.time()
It = transform(I, T.inv(), reference=J)
toc = time.time()
print('  Resampling time: %f sec' % (toc-tic))

# Save resampled source
outfile =  source+'_TO_'+target+'.nii'
print ('Saving resampled source in: %s' % outfile)
save_image(It, outfile)

# Save transformation matrix
import numpy as np
np.save(outfile, np.asarray(T))

Пример #2
0
import numpy as np
import nibabel as ni
from scipy.ndimage import rotate
from nipy.neurospin.registration import register, transform

def add_padding(data,pad=10,value=0):
    new_data=value*np.ones((data.shape[0]+2*pad,data.shape[1]+2*pad,data.shape[2]+2*pad),dtype=data.dtype)
    new_data[pad:pad+data.shape[0],pad:pad+data.shape[1],pad:pad+data.shape[2]]=data
    return new_data

#synthesize volume
S0=255*np.ones((50,50,50))#.astype('uint16')
S0=add_padding(S0,10,100)
S0=add_padding(S0,40)   
#S0=rotate(S0,5,reshape=False)

S1=rotate(S0,30,reshape=False)
S0img=ni.Nifti1Image(S0,np.eye(4))
ni.save(S0img,'S0img.nii.gz')

S1img=ni.Nifti1Image(S1,np.eye(4))
ni.save(S1img,'S1img.nii.gz')

#register S1 to S0
T=register(S1img,S0img,interp='pv')

#save S1 as NS1 after registration
NS1img=transform(S1img, T)
ni.save(NS1img,'NS1img.nii.gz')