def register(self, src, trg, trg_mask=None, src_mask=None): """ Implementation of pair-wise registration using thunder-registration For more information on the model estimation, refer to https://github.com/thunder-project/thunder-registration This function takes two 2D single channel images and estimates a 2D translation that best aligns the pair. The estimation is done by maximising the correlation of the Fourier transforms of the images. Once, the translation is estimated, it is applied to the (multi-channel) image to warp and, possibly, ot hte ground-truth. Different interpolations schemes could be more suitable for images and ground-truth values (or masks). :param src: 2D single channel source moving image :param trg: 2D single channel target reference image :param src_mask: Mask of source image. Not used in this method. :param trg_mask: Mask of target image. Not used in this method. :return: Estimated 2D transformation matrix of shape 2x3 """ # Initialise instance of CrossCorr object ccreg = registration.CrossCorr() # padding_value = 0 # Compute translation between pair of images model = ccreg.fit(src, reference=trg) # Get translation as an array translation = [-x for x in model.toarray().tolist()[0]] # Fill in transformation matrix warp_matrix = np.eye(2, 3) warp_matrix[0, 2] = translation[1] warp_matrix[1, 2] = translation[0] # Return transformation matrix return warp_matrix
def RegisterImages(path, outputpathfile, slice1=42, limit=30): tiffiles, indx = osDB.getFileContString(path, 'tif') #load first 30 tifffiles img1 = tifffile.imread(os.path.join(path, tiffiles.sort_values().iloc[0])) img2 = np.zeros(([30, img1.shape[0]-3, img1.shape[1], img1.shape[2]]), dtype = img1.dtype) img2[0, :, :, :] = img1[:slice1, :, :] #img10 = td.images.fromtif(os.path.join(outputdata['Raw_Folder'].loc[cindex], '') for i, imgfile in enumerate(tiffiles.sort_values().iloc[1:limit]): #print(os.path.join(outputdata['Raw_Folder'].loc[cindex], imgfile)) img2[i+1, : ,: ,:] = tifffile.imread(os.path.join(path, imgfile))[:slice1, :, :] reg = registration.CrossCorr() reference = img2.mean(axis=0) reference = scipy.signal.medfilt(reference) registrationModel = reg.fit(img2, reference = reference) #displacements = registrationModel.toarray() images = registrationModel.transform(img2) #images2 = images.toarray() meanImg = images.mean() #images.cache() saveNifti(meanImg, outputpathfile)
path_base, path_head = os.path.split(path) path_base, flyID =os.path.split(path_base) detect ='fromfile' if not os.path.exists(path_base+os.path.sep+flyID+folderN+os.path.sep): os.makedirs(path_base+os.path.sep+flyID+folderN+os.path.sep) fname = path_base+os.path.sep+flyID+folderN+os.path.sep+flyID+'_stimtimes'+detect+'.csv' times = [29, 59, 89, 119, 149, 178, 208, 238, 268, 298] stimData = pd.DataFrame({'Stimulation_time': times} ) stimData.to_csv(fname) # In[5]: ## registration -- correct for movement in image fname = path_base+os.path.sep+flyID+folderN+os.path.sep+flyID+'_registration.json' #create new registration reg = registration.CrossCorr() #reference = rawdata[100:110, : , :, :].mean().toarray() reference = rawdata.mean().toarray() #plt.imshow(amax(reference,0), cmap='gray', clim=(0,2000)) #reg.prepare(rawdata, startIdx=100, stopIdx=110); registrationModel = reg.fit(rawdata, reference = reference) #registrationModel.save(fname, overwrite=True) #import json #with open(fname, 'w') as outfile: # json.dump(registrationModel, fname) # In[6]: displacements = registrationModel.toarray()