def affine_registration_nipy(in_path, ref_path, out_path, in_ref_mat = '', ref_in_mat = '', T = None, extra_params={}): """ Affine registation and resampling. Use Histogram registration from nipy. inputs: in_path: path to the source (input) image. ref_path: path to the target (reference) image. out_path: path to use to save the registered image. in_ref_mat: if bool(in_ref_mat) is True, save the 4x4 transformation matrix to a text file <in_ref_mat>. ref_in_mat: if bool(ref_in_mat) is True, save the reverse of the 4x4 transformation matrix to a text file <ref_in_mat>. T: affine transformation to use. if None, T will be estimated using HistogramRegistration and optimizers; if type(T) is not Affine, T = Affine(array=T) extra_params: extra parameters passing to HistogramRegistration, HistogramRegistration.optimize, resample return T """ source_image = load_image(in_path) target_image = load_image(ref_path) if T is None: print('assess the affine transformation using histogram registration. ') # R = HistogramRegistration(source_image, target_image) R = AllFeatures(HistogramRegistration,extra_params).run(source_image, target_image) # T = R.optimize('affine', optimizer='powell') T = AllFeatures(R.optimize,extra_params).run('affine', optimizer='powell') print('receive affine transformation %s' % T) else: if type(T) is not Affine: print('create Affine from T') T = Affine(array=T) print('using a predefined affine:\n%s\nwith a 4x4 matrix:\n%s\n' % (T, T.as_affine())) # It = resample(source_image, T.inv(), target_image) It = AllFeatures(resample,extra_params).run(source_image, T.inv(), target_image) # the second argument of resample takes an transformation from ref to mov # so that's why we need T.inv() here save_image(It, out_path) if in_ref_mat: np.savetxt(in_ref_mat, T.as_affine()) if ref_in_mat: np.savetxt(ref_in_mat, T.inv().as_affine()) return T
from nipy import load_image from nipy.algorithms.registration import (HistogramRegistration, Affine, resample) # Load input images fmri_img = load_image('mean_fmri.nii') t1_img = load_image('T1.nii') # First pass: rigid registration using mutual information reg = HistogramRegistration(fmri_img, t1_img, similarity='mi') T = reg.optimize('rigid') # Second pass: 12-parameter affine registration using a custom variant # of mutual information based on the Hellinger distance def mymi(H): # takes a 2D array representing the joint histogram as input P = H / H.sum() Pi = P.sum(0).reshape((H.shape[1], 1)) Pj = P.sum(1).reshape((H.shape[0], 1)) return np.sum((np.sqrt(P) - np.sqrt(Pi.T * Pj)) ** 2) T2 = Affine(T.as_affine()) reg2 = HistogramRegistration(fmri_img, t1_img, similarity=mymi) T2 = reg2.optimize(T2) # Resample the fMRI image in T1 space reg_fmri_img = resample(fmri_img, T2.inv(), reference=t1_img)
from nipy import load_image from nipy.algorithms.registration import (HistogramRegistration, Affine, resample) # Load input images fmri_img = load_image('mean_fmri.nii') t1_img = load_image('T1.nii') # First pass: rigid registration using mutual information reg = HistogramRegistration(fmri_img, t1_img, similarity='mi') T = reg.optimize('rigid') # Second pass: 12-parameter affine registration using a custom variant # of mutual information based on the Hellinger distance def mymi(H): # takes a 2D array representing the joint histogram as input P = H / H.sum() Pi = P.sum(0).reshape((H.shape[1], 1)) Pj = P.sum(1).reshape((H.shape[0], 1)) return np.sum((np.sqrt(P) - np.sqrt(Pi.T * Pj))**2) T2 = Affine(T.as_affine()) reg2 = HistogramRegistration(fmri_img, t1_img, similarity=mymi) T2 = reg2.optimize(T2) # Resample the fMRI image in T1 space reg_fmri_img = resample(fmri_img, T2.inv(), reference=t1_img)
'nobias_' + source + '.nii.gz') target_file = example_data.get_filename('neurospin', 'sulcal2000', 'nobias_' + target + '.nii.gz') # Optional arguments similarity = 'cc' interp = 'pv' optimizer = 'powell' # Make registration instance I = load_image(source_file) J = load_image(target_file) R = HistogramRegistration(I, J, similarity=similarity, interp=interp) # Global affine registration A = Affine() R.optimize(A) #Jt = resample(J, A, reference=I) Av = A.compose(Affine(I.affine)) Jat = resample(J, Av, reference=I, ref_voxel_coords=True) save_image(Jat, 'affine_anubis_to_ammon.nii') # Region matching t0 = time.time() ##corners, size = get_blocks(I.shape, 3, 1, 0) #.5 size ##corners, size = get_blocks(I.shape, 6, 2, 0) #.75 size ##corners, size = get_blocks(I.shape, 6, 1, 0) # .5 size corners, size = get_blocks(I.shape, 5, 2, 1)