def RegisterToDummy(start, start_ref=None): #scale dummy mask with busbar oriented upper horizontal. dummy = np.zeros(start.shape) border = [int(np.round(x * 2 / 53)) for x in start.shape] busbar = int(np.round(start.shape[0] * 23 / 53)) dummy[border[0]:-border[0], border[1]:-border[1]] = 0.05 dummy[busbar:busbar + border[0], :] = 1 if start_ref is None: start_ref = start start_gauss = gaussian(start_ref, sigma=0.5) result = ird.similarity(dummy, start_gauss, numiter=30, constraints={ 'angle': [0, 360], 'tx': [0, 2], 'ty': [0, 2], 'scale': [1, 0.02] }) start_reg = ird.transform_img(start, tvec=result['tvec'].round(4), angle=result['angle'], scale=result['scale']) return start_reg
def _register_to_template_2(self, template_closed, sample_closed): # fixed = sitk.GetImageFromArray(template_closed.astype(np.float) * 255) # fixed = sitk.DiscreteGaussian(fixed, 2.0) # moving = sitk.GetImageFromArray(sample_closed.astype(np.float) * 255) # moving = sitk.DiscreteGaussian(moving, 2.0) im0 = template_closed.astype(np.uint8) im1 = sample_closed.astype(np.uint8) # using imreg_dft # result = ird.similarity(im0, im1, numiter=2, constraints=None) constraints = {} constraints['scale'] = [1, 0.0] # result = ird.similarity(im0, im1, numiter=2, constraints=constraints) constraints['angle'] = [0, 1] constraints['tx'] = [0, 5] constraints['ty'] = [0, 5] result = ird.similarity(im0, im1, numiter=2, constraints=constraints) assert "timg" in result # Maybe we don't want to show plots all the time # if os.environ.get("IMSHOW", "yes") == "yes": # import matplotlib.pyplot as plt # ird.imshow(im0, im1, result['timg']) # plt.show() return self._transform_sample_2(result)
def image_registration(self): #Runs at the beginning of a new trial def trans_mat(angle, x, y, scale): #Utility function to get the transformation matrix angle = -1 * np.radians(angle) scale = 1 / scale x = -1 * x y = -1 * y rot_ext = np.array([[ np.cos(angle), -np.sin(angle), y * np.cos(angle) - x * np.sin(angle) ], [ np.sin(angle), np.cos(angle), y * np.sin(angle) + x * np.cos(angle) ]]) scale_mat = np.array([[scale, 1, 1], [1, scale, 1]]) return rot_ext * scale_mat self.mouse.trial_image = np.empty( (self.camera.resolution[0], self.camera.resolution[1], 3), dtype=np.uint8) self.camera.capture(self.mouse.trial_image, 'rgb') #Image registration #IMPROVE: Could run the registration on a different processor warnings.filterwarnings("ignore", ".*the returned array has changed*") tf = ird.similarity(self.mouse.ref_im[:, :, 1], self.mouse.trial_image[:, :, 1], numiter=3) print('scale\tangle\tty\ttx') print('{0:.3}\t{1:.3}\t{2:.3}\t{3:.3}'.format(tf['scale'], tf['angle'], tf['tvec'][0], tf['tvec'][1])) #Check if results of image registration don't cross boundaries if all((abs(tf['angle']) <= self.max_angle, all(np.abs(tf['tvec']) <= self.max_trans), self.max_scale[0] <= tf['scale'] <= self.max_scale[1])): #Transform the target to new position self.R = trans_mat(tf['angle'], tf['tvec'][1], tf['tvec'][0], tf['scale']) cent_targ = self.mouse.targets - np.array([ int(self.camera.resolution[0] / 2), int(self.camera.resolution[0] / 2) ]) #translate targets to center of image trans_coord = np.dot(self.R, np.append(cent_targ, 1)) + np.array([ int(self.camera.resolution[0] / 2), int(self.camera.resolution[0] / 2) ]) targ_pos = np.dot(self.coeff, np.append(trans_coord, 1)) print('TARGET\ttx\tty') print('{0}\t{1:.01f}\t{2:.01f}'.format('0', trans_coord[0], trans_coord[1])) return targ_pos else: print('No laser stimulation: Image registration failed.') writeToLogFile(self.expSettings.logFP, self.mouse, 'image registration failure') return None
def register(self, constraints=None, transform=None): if transform is None: t=ird.similarity(self.pm.R0, self.I0zcn, numiter=3, constraints=constraints) transform = { your_key: t[your_key] for your_key in ['angle','scale','tvec'] } self.transform=transform self.I0T=ird.transform_img_dict(self.I0zcn, self.transform) self.data=ird.transform_img_dict(self.I1zcf, self.transform)
def similarity(template_array, sub_array, scale_exponent=0.4): """ Aligns array 'sub_array' to the template 'template_array' using rotational transformation. Args: template_array : Template array which will be used as a reference for rotational aligning sub_array : Subject array (already corrected for translation) scale_exponent : Exponent to which the array will be scaled Returns: mod_array : Modified subject array aligned using rotational & translational transformation """ list_array = modify_array([template_array, sub_array], scale_exponent) template_arrayslice = list_array[0] sub_arrayslice = list_array[1] dict_constraint = {'scale': [1.0, 0], 'tx': [0, 0], 'ty': [0, 0]} dict_rot = ird.similarity(template_arrayslice, sub_arrayslice, numiter=3, order=2, constraints=dict_constraint) mod_array = ird.transform_img(sub_array, scale=1.0, angle=dict_rot['angle'], tvec=dict_rot['tvec'], mode='nearest') print("Rotation = {0}, Translation = {1}".format(dict_rot['angle'], dict_rot['tvec'])) return mod_array
def register_directory_pairwise(image_dir, out_dir='', save_txt=True, save_image=False, **kwargs): '''Run FFT-based rigid image registration comparing sequential images in a directory image_dir : str The path to the image files out_dir : str The path to write the output image fles save_txt : bool Write the affine transformation parameters to a .txt file save_image : bool save the registered output file **kwargs : dict The arguments for the imreg_dft function ''' imd = glob.glob(image_dir + '/*.tif') imd1 = list(imd) imd2 = list(imd) imd1.pop(-1) imd2.pop(0) test_frame = imread2(imd1[0]) if (len(test_frame.shape) > 2) and (test_frame.shape[-1] > 1): rgb_flag = True warnings.warn("RGB image detected, first channel will be used.") else: rgb_flag = False text_file = open(image_dir + "_transform_params.txt", "w") for im1, im2 in zip(imd1, imd2): frame_a, frame_b = imread2(im1), imread2(im2) if rgb_flag: frame_a, frame_b = frame_a[:, :, 0], frame_b[:, :, 0] result = ird.similarity(frame_a, frame_b, **kwargs) transform_params = result['scale'], result['angle'], result['tvec'] if save_image: imname = os.path.split(im1)[-1][:-4] param_str = '_scale'+str(scale)+'_angle'+str(rotangle)\ +'_trans'+str(transvec[0])+'_'+str(transvec[1]) savestr = out_dir + '/' + imname + '.png' toimage(regim).save(savestr) pass if save_txt: print("{0}\t{1}\t{2}".format(*transform_params), file=text_file) text_file.close()
def register(self, constraints=None, transform=None): if transform is None: t=ird.similarity(self.R0, self.R1, numiter=3, constraints=constraints) transform = { your_key: t[your_key] for your_key in ['angle','scale','tvec'] } self.transform=transform self.RT=ird.transform_img_dict(self.R1, self.transform) self.BT=ird.transform_img_dict(self.B1, self.transform) self.ST=ird.transform_img_dict(self.S1, self.transform)
def shapeAlignImage(image, shapeImage): constraints = {} constraints['angle'] = (0, .1) constraints['scale'] = (1, .001) constraints['tx'] = (0, 5) constraints['ty'] = (0, 5) if image.shape[2] > 1: result = ird.similarity(rgb2gray(shapeImage), rgb2gray(image[:, :, [4, 2, 1]] / 3), numiter=10, constraints=constraints) else: result = ird.similarity(rgb2gray(shapeImage), np.squeeze(image / 3), numiter=10, constraints=constraints) #ird.translation(rgb2gray(shapeImage),rgb2gray(image[:,:,[4,2,1]]/3)) #tvec = result["tvec"].round(4) returnResult = np.zeros(image.shape) if len(image.shape) == 3: for index in range(0, image.shape[2]): im = ird.transform_img(image[:, :, index], scale=result['scale'], angle=result['angle'], tvec=(result['tvec'][0], result['tvec'][1]), order=3) if image.dtype == np.dtype('uint16'): returnResult[:, :, index] = np.uint16(im) elif image.dtype == np.dtype('uint8'): returnResult[:, :, index] = np.uint16(im) else: returnResult[:, :, index] = im else: im = ird.transform_img(image[:, :], scale=result['scale'], angle=result['angle'], tvec=(result['tvec'][0], result['tvec'][1]), order=3) if image.dtype == np.dtype('uint16'): returnResult[:, :] = np.uint16(im) elif image.dtype == np.dtype('uint8'): returnResult[:, :] = np.uint16(im) else: returnResult[:, :] = im return returnResult, result['scale'], result['angle'], result['tvec']
def align_videos(self, filenames, template_frame): """Return filenames of generated videos""" progress_global = QProgressDialog('Total progress aligning all files', 'Abort', 0, 100, self) progress_global.setAutoClose(True) progress_global.setMinimumDuration(0) def callback_global(x): progress_global.setValue(x * 100) QApplication.processEvents() callback_global(0) ret_filenames = [] for i, filename in enumerate(filenames): callback_global(i / float(len(filenames))) progress_shifts = QProgressDialog('Finding best shifts for ' + filename, 'Abort', 0, 100, self) progress_shifts.setAutoClose(True) progress_shifts.setMinimumDuration(0) progress_apply = QProgressDialog('Applying shifts for ' + filename, 'Abort', 0, 100, self) progress_apply.setAutoClose(True) progress_apply.setMinimumDuration(0) def callback_apply(x): progress_apply.setValue(x * 100) QApplication.processEvents() progress_load = QProgressDialog('Loading ' + filename, 'Abort', 0, 100, self) progress_load.setAutoClose(True) progress_load.setMinimumDuration(0) def callback_load(x): progress_load.setValue(x * 100) QApplication.processEvents() frames = file_io.load_file(filename, callback_load) callback_load(1) reference_frame = frames[self.ref_no.value()] # todo: this needs to be renamed... reference frame is already established as something else if not self.use_shift_checkbox.isChecked(): if self.scaling_checkbox.isChecked() or self.rotation_checkbox.isChecked(): shift = ird.similarity(template_frame, reference_frame) if not self.rotation_checkbox.isChecked(): shift['angle'] = 0.0 if not self.scaling_checkbox.isChecked(): shift['scale'] = 1.0 else: shift = ird.translation(template_frame, reference_frame) if progress_shifts.wasCanceled(): return else: shift = {'tvec': [self.tvec_y_sb.value(), self.tvec_x_sb.value()], 'angle': self.rotation_sb.value(), 'scale': self.scale_sb.value()} shifted_frames = self.apply_shifts(frames, shift, callback_apply) path = pfs.save_project(filename, self.project, shifted_frames, self.Defaults.manip, 'video') pfs.refresh_list(self.project, self.video_list, self.video_list_indices, self.Defaults.list_display_type, self.toolbutton_values) ret_filenames.append(path) callback_global(1) return ret_filenames
def compute_shift(self, ref_frame, frame): if self.scaling_checkbox.isChecked() or self.rotation_checkbox.isChecked(): shift = ird.similarity(ref_frame, frame) if not self.rotation_checkbox.isChecked(): shift['angle'] = 0.0 if not self.scaling_checkbox.isChecked(): shift['scale'] = 1.0 else: shift = ird.translation(ref_frame, frame) shift['scale'] = 1.0 return shift
def register(self, constraints=None, transform=None): if transform is None: t = ird.similarity(self.R0, self.R1, numiter=3, constraints=constraints) transform = { your_key: t[your_key] for your_key in ['angle', 'scale', 'tvec'] } self.transform = transform
def _align_imreg_dft(im, ref, **kargs): """Return the translation vector to shirt im to align to ref using the imreg_dft method.""" constraints = kargs.pop("constraints", { "angle": [0.0, 0.0], "scale": [1.0, 0.0] }) with warnings.catch_warnings(): # This causes a warning due to the masking warnings.simplefilter("ignore") result = imreg_dft.similarity(ref, im, constraints=constraints) tvec = result["tvec"] return np.array(tvec), result
def register_imgs(main_cam_frame: np.ndarray, drift_cam_frame: np.ndarray) -> RegArray: if check_size(main_cam_frame, drift_cam_frame): i1, i2 = main_cam_frame, drift_cam_frame else: i1, i2 = pad_inputs(main_cam_frame, drift_cam_frame) result = ird.similarity(i1, i2) out = RegArray(zoom=result['scale'], rotation=result['angle'], shift_x=result['tvec'][0], shift_y=result['tvec'][1]) return out
def compute_transform(self): src_img_path = resized(self.imageA.path, factor=DOWNSCALING_FACTOR) mvg_img_path = resized(self.imageB.path, factor=DOWNSCALING_FACTOR) cache_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'cache.txt') if os.path.exists(cache_path): cache_file = open(cache_path, "r") lines = cache_file.readlines() cache_file.close() cache = {k: v for k, v in [l.rsplit('\t', 1) for l in lines]} else: cache = {} key = "{}\t{}\t{}\t{}".format(src_img_path, mvg_img_path, IMREG_DFT_NUMITER, IMREG_DFT_EXTEND) try: val = json.loads(cache[key]) self.tvec = val['tvec'] self.angle = val['angle'] self.scale = val['scale'] self.success = val['success'] except KeyError: src_data = sp.misc.imread(src_img_path, True) mvg_data = sp.misc.imread(mvg_img_path, True) src_data = imreg_dft.utils.extend_by(src_data, IMREG_DFT_EXTEND) mvg_data = imreg_dft.utils.extend_by(mvg_data, IMREG_DFT_EXTEND) result = ird.similarity(src_data, mvg_data, numiter=IMREG_DFT_NUMITER) self.tvec = result['tvec'][1], -result['tvec'][0] # (Y,X) self.angle = result['angle'] / 180.0 * math.pi self.scale = result['scale'] self.success = result['success'] val = json.dumps({ 'tvec': self.tvec, 'angle': self.angle, 'scale': self.scale, 'success': self.success }) cache_file = open(cache_path, "a+") cache_file.write("\n") cache_file.write("{}\t{}".format(key, val)) cache_file.close()
def register(self, constraints=None, transform=None): if transform is None: t = ird.similarity(self.pm.R0, self.I0zcn, numiter=3, constraints=constraints) transform = { your_key: t[your_key] for your_key in ['angle', 'scale', 'tvec'] } self.transform = transform self.I0T = ird.transform_img_dict(self.I0zcn, self.transform) self.data = ird.transform_img_dict(self.I1zcf, self.transform)
def xuanzhuan(): # the TEMPLATE # the image to be transformed im3 = sp.misc.imrotate(im1, 0.0) ct = {'angle': (2, 5)} # ct = None result = ird.similarity(im0, im3, numiter=3, constraints=ct) assert "timg" in result # Maybe we don't want to show plots all the time ird.imshow(im0, im3, result['timg']) plt.show()
def run(self): # this needs to be in a thread to not lock up the gui self.frame.alignStatus.Show() # apply edge detection to both input images if(self.detectEdgesBool): skim1edges = self.detectEdges(self.path1) skim2edges = self.detectEdges(self.path2) skimage.io.imsave(edges1File,skim1edges) skimage.io.imsave(edges2File,skim2edges) im1wx = wx.Image(edges1File, wx.BITMAP_TYPE_ANY) im2wx = wx.Image(edges2File, wx.BITMAP_TYPE_ANY) else: im1wx = wx.Image(self.path1, wx.BITMAP_TYPE_ANY) im2wx = wx.Image(self.path2, wx.BITMAP_TYPE_ANY) # scale image 2 w1 = im1wx.GetWidth() h1 = im1wx.GetHeight() w2 = im2wx.GetWidth() h2 = im2wx.GetHeight() im2scaled = im2wx.Scale(w1,h1) # not a good scaling method im2scaled.SaveFile(image2scaledFile) # read in image 1 and scaled image 2 if(self.detectEdgesBool): im1 = sp.misc.imread(edges1File,True) else: im1 = sp.misc.imread(self.path1,True) im2 = sp.misc.imread(image2scaledFile,True) # align image 2 result = ird.similarity(im1, im2, numiter=3) assert "timg" in result im2aligned = result['timg'] # overlay overlay = im1 + im2aligned sp.misc.imsave(overlayFile,overlay) # display overlay self.frame.img3 = wx.Image(overlayFile, wx.BITMAP_TYPE_ANY) img3thumb = self.frame.img3.Scale(OutputMaxSize,OutputMaxSize) self.frame.imageCtrl3.SetBitmap(wx.Bitmap(img3thumb)) self.frame.Refresh() self.frame.alignStatus.Hide()
def rotateImg(to_register, original): import imreg_dft as ird import imageio im0 = copy.deepcopy(original) im1 = copy.deepcopy(to_register) #result0 = ird.similarity(im0[:, :, 0], im1[:, :, 0], numiter=3) #result1 = ird.similarity(im0[:, :, 1], im1[:, :, 1], numiter=3) result2 = ird.similarity(im0[:, :, 2], im1[:, :, 2], numiter=3) #im1[:, :, 0] = result0['timg'] #im1[:, :, 1] = result1['timg'] im1[:, :, 2] = result2['timg'] if result2['angle'] > 0.1 or result2['angle'] < -0.1: #print("using registered") return im1 else: #print("using normal") return to_register
def similarity(params): """ Transform target image to make it similar to source image """ # The template source_image, target_image, size, source_out_dir, target_out_dir, verbose = params im0 = extract_n_preprocess_dicom(source_image, size) # The image to be transformed im1 = extract_n_preprocess_dicom(target_image, size) filename = os.path.basename(os.path.normpath(source_image)) if verbose: print("Comparing %r .........." % filename) # Transform im1 to im0 result = ird.similarity(im0, im1, numiter=3) source_image_path = os.path.join(source_out_dir, filename) target_image_path = os.path.join(target_out_dir, filename) if verbose: print("Saving %r .........." % source_image_path) cv2.imwrite(source_image_path, im0) cv2.imwrite(target_image_path, result['timg']) if verbose: print("Saved %r .........." % source_image_path)
def imreg(location): x = 0 ref_image = glob.glob(location + '/*_ref_A_.fits') images = glob.glob(location + '/*_a_.fits') if len(ref_image) == 1: ref_data = fits.getdata(ref_image[0]) # ref_data = np.array(ref_data, dtype='float64') print("\n-> Aligning images with imreg...") for i in images: data = fits.getdata(i) # data = np.array(data, dtype='float64') corrected_image = imreg_dft.similarity(ref_data, data) hdu = fits.PrimaryHDU(corrected_image['timg']) hdu.writeto(i[:-8] + '_A_.fits') os.remove(i) x += 1 print("-> %.1f%% aligned..." % (float(x) / float(len(images)) * 100)) else: print("-> Alignment failed: Reference image missing")
def _align_transform(image_ref: ndarray, image: ndarray, **kwargs) -> Tuple[ndarray, SimilarityTransform]: """ Calculate the transformation needed to align the image with the a reference image :param image_ref: a refernce image to align with :param image: an image to align with image_ref :param kwargs: keyword arguments passed to imreg_dft.imreg.similarity :returns: a transformation that will align the image with image_ref, and the aligned greyscale image """ from imreg_dft import similarity from skimage.color import rgb2gray from ..imaging import image_as_rgb # imreg_dft.similarity can't handle colour images image_ref_gray = rgb2gray(image_as_rgb(image_ref)) image_gray = rgb2gray(image_as_rgb(image)) transform_params = similarity(image_ref_gray, image_gray, **kwargs) transform = SimilarityTransform(scale=transform_params["scale"], rotation=transform_params["angle"], translation=transform_params["tvec"]) return transform_params["timg"], transform
def image(source_s, reference=None, method="astroalign"): """ Aligns the source astronomical image(s) to the reference astronomical image ARGUMENTS source_s -- the image(s) to align; fitsio HDU object, numpy array, or a list of either one of the above KEYWORD ARGUMENTS reference -- the image against which to align the source image; fitsio HDU object or numpy array. If None, the best option is chosen from among the sources. method -- the library to use to align the images. options are: astroalign (default), skimage, imreg, skimage, chi2 RETURNS a transformed copy of the source image[s] in the same data type which was passed in """ # make sure that we can handle source as a list sources = [] outputs = [] if isinstance(source_s, list): sources = source_s else: sources.append(source_s) if reference is None: reference = ref_image(sources) print(reference.header["ORIGNAME"]) np_ref = to_np( reference, "Cannot align to unexpected type {}; expected numpy array or FITS HDU") for source in sources: np_src = to_np( source, "Cannot align unexpected type {}; expected numpy array or FITS HDU" ) # possibly unneccessary but unsure about scoping output = np.array([]) if method == "astroalign": try: output = astroalign.register(np_src, np_ref)[0] except NameError: raise ValueError(DISABLED.format(method, "astroalign")) elif method == "skimage": try: shift = register_translation(np_ref, np_src, 100)[0] output_fft = fourier_shift(np.fft.fftn(np_src), shift) output = np.fft.ifftn(output_fft) except NameError: raise ValueError(DISABLED.format(method, "scipy or numpy")) elif method == "chi2": try: dx, dy = chi2_shift(np_ref, np_src, upsample_factor='auto')[:2] output = fft_tools.shift.shiftnd(data, (-dx, -dy)) except NameError: raise ValueError(DISABLED.format(method, "image_registration")) elif method == "imreg": try: output = imreg_dft.similarity(np_ref, np_src)["timg"] except NameError: raise ValueError(DISABLED.format(method, "imreg_dft")) else: raise ValueError("Unexpected alignment method {}!".format(method)) if isinstance(source, HDU_TYPES): output = PrimaryHDU(output, source.header) outputs.append(output) return outputs if isinstance(source_s, list) else outputs[0]
# Duplicate image2 = image1.filter(ImageFilter.BLUR) image2.save(os.path.join(basedir,"sample2"), "png") fig, ax = plt.subplots(1, 2) ax[0].imshow(image1) ax[1].imshow(image2) # In[10]: im1 = sp.misc.imread(os.path.join(basedir, "sample1"), True) im2 = sp.misc.imread(os.path.join(basedir, "sample2"), True) result = ird.similarity(im1, im2, numiter=3) print (result["success"]) # The rate of success means the proportion which the two images are aligned. # # # Now I'm rotaing the above reimbursement in 30 degress. # In[11]: basedir = os.path.join('../research/data/') # the TEMPLATE doc1 = 'http://www.camara.gov.br/cota-parlamentar/documentos/publ/2437/2015/5645173.pdf' # Original
import os import scipy as sp import imageio import imreg_dft as ird basedir = os.path.join('..', 'examples') # the TEMPLATE im0 = imageio.imread(os.path.join(basedir, "sample1.png"), as_gray=True) # the image to be transformed im1 = imageio.imread(os.path.join(basedir, "sample3.png"), as_gray=True) result = ird.similarity(im0, im1, numiter=3) assert "timg" in result # Maybe we don't want to show plots all the time if os.environ.get("IMSHOW", "yes") == "yes": import matplotlib.pyplot as plt ird.imshow(im0, im1, result['timg']) plt.show()
def align(im, ref, method=None, **kargs): """Use one of a variety of algroithms to align two images. Args: im (ndarray) image to align ref (ndarray) reference array Keyword Args: method (str or None): If given specifies which module to try and use. Options: 'scharr', 'chi2_shift', 'imreg_dft', 'cv2' **kargs (various): All other keyword arguments are passed to the specific algorithm. Returns (ImageArray or ndarray) aligned image Notes: Currently three algorithms are supported: - image_registration module's chi^2 shift: This uses a dft with an automatic up-sampling of the fourier transform for sub-pixel alignment. The metadata key *chi2_shift* contains the translation vector and errors. - imreg_dft module's similarity function. This implements a full scale, rotation, translation algorithm (by default cosntrained for just translation). It's unclear how much sub-pixel translation is accomodated. - cv2 module based affine transform on a gray scale image. from: http://www.learnopencv.com/image-alignment-ecc-in-opencv-c-python/ """ #To be consistent with x-y co-ordinate systems if all([m is None for m in [imreg_dft, chi2_shift, cv2]]): raise ImportError( 'align requires one of imreg_dft, chi2_shift or cv2 modules to be available.' ) if method == "scharr" and imreg_dft is not None: im = im.T ref = ref.T scale = np.ceil(np.max(im.shape) / 500.0) ref1 = ref.gaussian_filter(sigma=scale, mode="wrap").scharr() im1 = im.gaussian_filter(sigma=scale, mode="wrap").scharr() im1 = im1.align(ref1, method="imreg_dft") tvec = np.array(im1["tvec"]) new_im = im.shift(tvec) new_im["tvec"] = tuple(-tvec) new_im = new_im.T elif (method is None and chi2_shift is not None) or method == "chi2_shift": kargs["zeromean"] = kargs.get("zeromean", True) result = np.array(chi2_shift(ref, im, **kargs)) new_im = im.__class__(fft_tools.shiftnd(im, -result[0:2])) new_im.metadata.update(im.metadata) new_im.metadata["chi2_shift"] = result elif (method is None and imreg_dft is not None) or method == "imreg_dft": constraints = kargs.pop("constraints", { "angle": [0.0, 0.0], "scale": [1.0, 0.0] }) result = imreg_dft.similarity(ref, im, constraints=constraints) new_im = result.pop("timg").view(type=ImageArray) new_im.metadata.update(im.metadata) new_im.metadata.update(result) elif (method is None and cv2 is not None) or method == "cv2": im1_gray = cv2.cvtColor(ref, cv2.COLOR_BGR2GRAY) im2_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) # Find size of image1 sz = im.shape # Define the motion model warp_mode = cv2.MOTION_TRANSLATION warp_matrix = np.eye(2, 3, dtype=np.float32) # Specify the number of iterations. number_of_iterations = 5000 # Specify the threshold of the increment # in the correlation coefficient between two iterations termination_eps = 1e-10 # Define termination criteria criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, number_of_iterations, termination_eps) # Run the ECC algorithm. The results are stored in warp_matrix. (_, warp_matrix) = cv2.findTransformECC(im1_gray, im2_gray, warp_matrix, warp_mode, criteria) # Use warpAffine for Translation, Euclidean and Affine new_im = cv2.warpAffine(im, warp_matrix, (sz[1], sz[0]), flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP) else: # No cv2 available so don't do anything. raise RuntimeError("Couldn't find an image alignment algorithm to use") return new_im.T
str(img_idx) + ".jpg") float_img_name = os.path.join(data_in_dir, sub_dir[ihc_idx], sub_dir_IHC[ihc_idx], "level" + str(lv_idx), str(img_idx) + ".jpg") print(float_img_name) print(fix_img_name) Img_fix_col = Image.open(fix_img_name) Img_float_col = Image.open(float_img_name) Img_fix = sp.misc.fromimage( Img_fix_col, True ) # flatten is True, means we convert images into graylevel images. Img_float = sp.misc.fromimage(Img_float_col, True) # sim = ird.similarity(Img_fix, Img_float) con_s = dict(angle=[0, 0], scale=[1, 1]) sim = ird.similarity(Img_fix, Img_float, constraints=con_s) # con_s= {"angle": 0, "scale": 0} # sim = ird.translation(Img_fix,Img_float,constraints=con_s) tvec = sim["tvec"].round(4) angle = sim["angle"] score = sim["success"] offset_list.append([tvec[1], tvec[0]]) angle_list.append(angle) score_list.append(score) print("Translation is {}, angle is {}, success rate {:.4g}".format( tuple(tvec), angle, sim["success"])) TImg = sp.misc.imrotate(Img_float_col, sim["angle"]) fix_img = np.array(Img_fix_col) M = np.float32([[1, 0, tvec[0]], [0, 1, tvec[1]]]) dst = cv2.warpAffine(TImg, M, (Img_fix.shape[0], Img_fix.shape[1])) t_dst = np.array(dst)
import scipy as sp import scipy.misc import imreg_dft as ird from astropy.io import fits print "Libraries imported..." names_i = os.listdir('./data_corr_trimmed/i') names_v = os.listdir('./data_corr_trimmed/v') print "Directories enlisted..." print "Entering I alignments..." im_ref = fits.open('data_corr_trimmed/v/' + names_v[8])[0].data for i in range(len(names_i)): im_test = fits.open('data_corr_trimmed/i/' + names_i[i])[0].data result = ird.similarity(im_ref, im_test, numiter=2) print i, names_i[i], "tvec:", result['tvec'], " success:", result[ 'success'] out_hdu = fits.PrimaryHDU(result['timg']) out_hdu.writeto('data_align/i/' + names_i[i]) print "I alignments done..." print "Entering V alignments..." im_ref = fits.open('data_corr_trimmed/v/' + names_v[8])[0].data for i in range(len(names_v)): im_test = fits.open('data_corr_trimmed/v/' + names_v[i])[0].data result = ird.similarity(im_ref, im_test, numiter=2) print i, names_v[i], "tvec:", result['tvec'], " success:", result[ 'success'] out_hdu = fits.PrimaryHDU(result['timg']) out_hdu.writeto('data_align/v/' + names_v[i])
def execute_primary_function(self, input_paths=None): """Return filenames of generated videos""" [reference_frame, to_align_paths] = self.get_alignment_inputs(input_paths) assert ([ os.path.splitext(os.path.basename(path))[0] for path in to_align_paths ] == self.shift_table_data[self.Labels.shift_table_col1]) progress_global = QProgressDialog('Total progress aligning all files', 'Abort', 0, 100, self) progress_global.setAutoClose(True) progress_global.setMinimumDuration(0) def callback_global(x): progress_global.setValue(x * 100) QApplication.processEvents() callback_global(0) progress_global.canceled.connect( functools.partial(self.cancel_progress_dialog, progress_global)) ret_filenames = [] shifts = {} for i, filename in enumerate(to_align_paths): callback_global(i / float(len(to_align_paths))) progress_shifts = QProgressDialog( 'Finding best shifts for ' + filename, 'Abort', 0, 100, self) progress_shifts.setAutoClose(True) progress_shifts.setMinimumDuration(0) progress_shifts.canceled.connect( functools.partial(self.cancel_progress_dialog, progress_shifts)) progress_load = QProgressDialog('Loading ' + filename, 'Abort', 0, 100, self) progress_load.setAutoClose(True) progress_load.setMinimumDuration(0) def callback_load(x): progress_load.setValue(x * 100) QApplication.processEvents() frames = file_io.load_file(filename, callback_load) callback_load(1) to_align_frame = frames[self.ref_no.value()] frame_no, h, w = frames.shape to_align_frame = np.reshape(to_align_frame, (1, h, w)) to_align_frame = self.crop_border( self.spatial_filter(to_align_frame))[0] # to_align_frame = to_align_frame[0] if self.scaling_checkbox.isChecked( ) or self.rotation_checkbox.isChecked(): shift = ird.similarity(reference_frame, to_align_frame) if not self.rotation_checkbox.isChecked(): shift['angle'] = 0.0 if not self.scaling_checkbox.isChecked(): shift['scale'] = 1.0 else: shift = ird.translation(reference_frame, to_align_frame) # if not self.use_shift_checkbox.isChecked(): # else: # shift = {'tvec': [self.tvec_y_sb.value(), self.tvec_x_sb.value()], 'angle': self.rotation_sb.value(), # 'scale': self.scale_sb.value()} shifts[filename] = shift # Apply the found shift (row i) to all stacks in row i for col_key in self.shift_table_data.keys(): # i = row filename = os.path.normpath( os.path.join(self.project.path, self.shift_table_data[col_key][i])) + '.npy' frames = file_io.load_file(filename, callback_load) callback_load(1) progress_apply = QProgressDialog( 'Applying shifts for ' + filename, 'Abort', 0, 100, self) progress_apply.setAutoClose(True) progress_apply.setMinimumDuration(0) def callback_apply(x): progress_apply.setValue(x * 100) QApplication.processEvents() progress_apply.canceled.connect( functools.partial(self.cancel_progress_dialog, progress_apply)) shifted_frames = self.apply_shifts(frames, shift, callback_apply) path = pfs.save_project(filename, self.project, shifted_frames, self.Defaults.manip, 'video') pfs.refresh_list(self.project, self.video_list, [], self.Defaults.list_display_type, self.toolbutton_values) ret_filenames.append(path) callback_global(1) # save shifts to csv save_loc = QFileDialog.getSaveFileName( self, 'Save Shifts', QSettings().value('path_of_last_project'), '(*.csv)')[0] if save_loc: # fields = list(shifts[list(shifts.keys())[0]].keys()) # for key in shifts.keys(): # for field in fields: # if field not in ['angle', 'tvec', 'scale', 'success']: # del shifts[key][field] # else: # shifts[key][field.encode('ascii')] = shifts[key][field] # del shifts[key][field] # keys_copy = list(shifts.keys()) # for key in keys_copy: # shifts[key.encode('ascii')] = shifts[key] # del shifts[key] # # for field in fields: # # if field not in ["File aligned", 'angle', 'tvec', 'scale', 'success']: # # for key in shifts.keys(): # # del shifts[key][field] # # else: # # shifts[] # keys_copy = list(shifts.keys()) # for key in keys_copy: # x, y = shifts[key][b'tvec'] # shifts[key][b'tvec-x'] = x # shifts[key][b'tvec-y'] = y # del shifts[key][b'tvec'] fields = list(shifts[list(shifts.keys())[0]].keys()) for key in list(shifts.keys()): for field in fields: if field not in ['angle', 'tvec', 'scale', 'success']: del shifts[key][field] for key in list(shifts.keys()): x, y = shifts[key]['tvec'] shifts[key]['tvec-x'] = x shifts[key]['tvec-y'] = y del shifts[key]['tvec'] fields = ["File aligned"] + list(shifts[list( shifts.keys())[0]].keys()) with open(save_loc, "w", newline='') as f: w = csv.DictWriter(f, fields) w.writeheader() for k in shifts: w.writerow( {field: shifts[k].get(field) or k for field in fields}) # for key, val in sorted(shifts.items()): # row = {"File aligned": key} # row.update(val) # w.writerow(row) # with open(save_loc, 'wb') as csv_file: # writer = csv.writer(csv_file) # for key, value in shifts.items(): # writer.writerow([key, value]) return ret_filenames
def execute_primary_function(self, input_paths=None): """Return filenames of generated videos""" [reference_frame, to_align_paths] = self.get_alignment_inputs(input_paths) assert([os.path.splitext(os.path.basename(path))[0] for path in to_align_paths] == self.shift_table_data[self.Labels.shift_table_col1]) progress_global = QProgressDialog('Total progress aligning all files', 'Abort', 0, 100, self) progress_global.setAutoClose(True) progress_global.setMinimumDuration(0) def callback_global(x): progress_global.setValue(x * 100) QApplication.processEvents() callback_global(0) progress_global.canceled.connect(functools.partial(self.cancel_progress_dialog, progress_global)) ret_filenames = [] shifts = {} for i, filename in enumerate(to_align_paths): callback_global(i / float(len(to_align_paths))) progress_shifts = QProgressDialog('Finding best shifts for ' + filename, 'Abort', 0, 100, self) progress_shifts.setAutoClose(True) progress_shifts.setMinimumDuration(0) progress_shifts.canceled.connect(functools.partial(self.cancel_progress_dialog, progress_shifts)) progress_load = QProgressDialog('Loading ' + filename, 'Abort', 0, 100, self) progress_load.setAutoClose(True) progress_load.setMinimumDuration(0) def callback_load(x): progress_load.setValue(x * 100) QApplication.processEvents() frames = file_io.load_file(filename, callback_load) callback_load(1) to_align_frame = frames[self.ref_no.value()] frame_no, h, w = frames.shape to_align_frame = np.reshape(to_align_frame, (1, h, w)) to_align_frame = self.crop_border(self.spatial_filter(to_align_frame))[0] # to_align_frame = to_align_frame[0] if self.scaling_checkbox.isChecked() or self.rotation_checkbox.isChecked(): shift = ird.similarity(reference_frame, to_align_frame) if not self.rotation_checkbox.isChecked(): shift['angle'] = 0.0 if not self.scaling_checkbox.isChecked(): shift['scale'] = 1.0 else: shift = ird.translation(reference_frame, to_align_frame) # if not self.use_shift_checkbox.isChecked(): # else: # shift = {'tvec': [self.tvec_y_sb.value(), self.tvec_x_sb.value()], 'angle': self.rotation_sb.value(), # 'scale': self.scale_sb.value()} shifts[filename] = shift # Apply the found shift (row i) to all stacks in row i for col_key in self.shift_table_data.keys(): # i = row filename = os.path.normpath(os.path.join(self.project.path, self.shift_table_data[col_key][i])) + '.npy' frames = file_io.load_file(filename, callback_load) callback_load(1) progress_apply = QProgressDialog('Applying shifts for ' + filename, 'Abort', 0, 100, self) progress_apply.setAutoClose(True) progress_apply.setMinimumDuration(0) def callback_apply(x): progress_apply.setValue(x * 100) QApplication.processEvents() progress_apply.canceled.connect(functools.partial(self.cancel_progress_dialog, progress_apply)) shifted_frames = self.apply_shifts(frames, shift, callback_apply) path = pfs.save_project(filename, self.project, shifted_frames, self.Defaults.manip, 'video') pfs.refresh_list(self.project, self.video_list, [], self.Defaults.list_display_type, self.toolbutton_values) ret_filenames.append(path) callback_global(1) # save shifts to csv save_loc = QFileDialog.getSaveFileName(self, 'Save Shifts', QSettings().value('path_of_last_project'), '(*.csv)') if save_loc: # fields = list(shifts[list(shifts.keys())[0]].keys()) # for key in shifts.keys(): # for field in fields: # if field not in ['angle', 'tvec', 'scale', 'success']: # del shifts[key][field] # else: # shifts[key][field.encode('ascii')] = shifts[key][field] # del shifts[key][field] # keys_copy = list(shifts.keys()) # for key in keys_copy: # shifts[key.encode('ascii')] = shifts[key] # del shifts[key] # # for field in fields: # # if field not in ["File aligned", 'angle', 'tvec', 'scale', 'success']: # # for key in shifts.keys(): # # del shifts[key][field] # # else: # # shifts[] # keys_copy = list(shifts.keys()) # for key in keys_copy: # x, y = shifts[key][b'tvec'] # shifts[key][b'tvec-x'] = x # shifts[key][b'tvec-y'] = y # del shifts[key][b'tvec'] fields = list(shifts[list(shifts.keys())[0]].keys()) for key in list(shifts.keys()): for field in fields: if field not in ['angle', 'tvec', 'scale', 'success']: del shifts[key][field] for key in list(shifts.keys()): x, y = shifts[key]['tvec'] shifts[key]['tvec-x'] = x shifts[key]['tvec-y'] = y del shifts[key]['tvec'] fields = ["File aligned"] + list(shifts[list(shifts.keys())[0]].keys()) with open(save_loc, "w", newline='') as f: w = csv.DictWriter(f, fields) w.writeheader() for k in shifts: w.writerow({field: shifts[k].get(field) or k for field in fields}) # for key, val in sorted(shifts.items()): # row = {"File aligned": key} # row.update(val) # w.writerow(row) # with open(save_loc, 'wb') as csv_file: # writer = csv.writer(csv_file) # for key, value in shifts.items(): # writer.writerow([key, value]) return ret_filenames
def similarity(imref, imtgt): return ird.similarity(imref, imtgt, numiter=3)['timg']
def align(im, ref, method=None, **kargs): """Use one of a variety of algroithms to align two images. Args: im (ndarray) image to align ref (ndarray) reference array Keyword Args: method (str or None): If given specifies which module to try and use. Options: 'scharr', 'chi2_shift', 'imreg_dft', 'cv2' **kargs (various): All other keyword arguments are passed to the specific algorithm. Returns (ImageArray or ndarray) aligned image Notes: Currently three algorithms are supported: - image_registration module's chi^2 shift: This uses a dft with an automatic up-sampling of the fourier transform for sub-pixel alignment. The metadata key *chi2_shift* contains the translation vector and errors. - imreg_dft module's similarity function. This implements a full scale, rotation, translation algorithm (by default cosntrained for just translation). It's unclear how much sub-pixel translation is accomodated. - cv2 module based affine transform on a gray scale image. from: http://www.learnopencv.com/image-alignment-ecc-in-opencv-c-python/ """ # To be consistent with x-y co-ordinate systems if all([m is None for m in [imreg_dft, chi2_shift, cv2]]): raise ImportError("align requires one of imreg_dft, chi2_shift or cv2 modules to be available.") if method == "scharr" and imreg_dft is not None: im = im.T ref = ref.T scale = np.ceil(np.max(im.shape) / 500.0) ref1 = ref.gaussian_filter(sigma=scale, mode="wrap").scharr() im1 = im.gaussian_filter(sigma=scale, mode="wrap").scharr() im1 = im1.align(ref1, method="imreg_dft") tvec = np.array(im1["tvec"]) new_im = im.shift(tvec) new_im["tvec"] = tuple(-tvec) new_im = new_im.T elif (method is None and chi2_shift is not None) or method == "chi2_shift": kargs["zeromean"] = kargs.get("zeromean", True) result = np.array(chi2_shift(ref, im, **kargs)) new_im = im.__class__(fft_tools.shiftnd(im, -result[0:2])) new_im.metadata.update(im.metadata) new_im.metadata["chi2_shift"] = result elif (method is None and imreg_dft is not None) or method == "imreg_dft": constraints = kargs.pop("constraints", {"angle": [0.0, 0.0], "scale": [1.0, 0.0]}) cls = im.__class__ with warnings.catch_warnings(): # This causes a warning due to the masking warnings.simplefilter("ignore") result = imreg_dft.similarity(ref, im, constraints=constraints) new_im = (result.pop("timg")).view(type=cls) new_im.metadata.update(im.metadata) new_im.metadata.update(result) elif (method is None and cv2 is not None) or method == "cv2": im1_gray = cv2.cvtColor(ref, cv2.COLOR_BGR2GRAY) im2_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) # Find size of image1 sz = im.shape # Define the motion model warp_mode = cv2.MOTION_TRANSLATION warp_matrix = np.eye(2, 3, dtype=np.float32) # Specify the number of iterations. number_of_iterations = 5000 # Specify the threshold of the increment # in the correlation coefficient between two iterations termination_eps = 1e-10 # Define termination criteria criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, number_of_iterations, termination_eps) # Run the ECC algorithm. The results are stored in warp_matrix. (_, warp_matrix) = cv2.findTransformECC(im1_gray, im2_gray, warp_matrix, warp_mode, criteria) # Use warpAffine for Translation, Euclidean and Affine new_im = cv2.warpAffine(im, warp_matrix, (sz[1], sz[0]), flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP) else: # No cv2 available so don't do anything. raise RuntimeError("Couldn't find an image alignment algorithm to use") return new_im.T
def image_registration(self): # Runs at the beginning of a new trial def trans_mat(angle, x, y, scale): # Utility function to get the transformation matrix angle = -1 * np.radians(angle) scale = 1 / scale x = -1 * x y = -1 * y rot_ext = np.array([[ np.cos(angle), -np.sin(angle), y * np.cos(angle) - x * np.sin(angle) ], [ np.sin(angle), np.cos(angle), y * np.sin(angle) + x * np.cos(angle) ]]) scale_mat = np.array([[scale, 1, 1], [1, scale, 1]]) return rot_ext * scale_mat self.mouse.update({ 'trial_image': np.empty( (self.camera.resolution()[1], self.camera.resolution()[0], 3), dtype=np.uint16) }) self.camera.capture(self.mouse.get('trial_image'), 'rgb') timestamp = time() self.mouse.update( {'trial_name': "M" + str(self.tag % 10000) + '_' + str(timestamp)}) self.task.DataLogger.writeToLogFile( self.tag, 'Image', { 'name': self.mouse.get('trial_name'), 'type': 'trial', 'reference': self.mouse.get('ref_name') }, timestamp) # Image registration # TODO: Could run the registration on a different processor warnings.filterwarnings("ignore", ".*the returned array has changed*") tf = ird.similarity(self.mouse.get('ref_im')[:, :, 1], self.mouse.get('trial_image')[:, :, 1], numiter=3) print('scale\tangle\tty\ttx') print('{0:.3}\t{1:.3}\t{2:.3}\t{3:.3}'.format(tf['scale'], tf['angle'], tf['tvec'][0], tf['tvec'][1])) # Check if results of image registration don't cross boundaries if all((abs(tf['angle']) <= self.max_angle, all(np.abs(tf['tvec']) <= self.max_trans), self.max_scale[0] <= tf['scale'] <= self.max_scale[1])): # Transform the target to new position self.R = trans_mat(tf['angle'], tf['tvec'][1], tf['tvec'][0], tf['scale']) x_target = self.mouse.get('targets')[0] y_target = self.mouse.get('targets')[1] # Shift target to fit origin at center of frame cent_targ = np.array([ int(x_target) - int(self.camera.resolution()[0] / 2), int(y_target) - int(self.camera.resolution()[1] / 2) ]) trans_coord = np.dot(self.R, np.append(cent_targ, 1)) + np.array([ int(self.camera.resolution()[0] / 2), int(self.camera.resolution()[1] / 2) ]) # Convert image target to motor target targ_pos = np.dot(self.coeff, np.append(trans_coord, 1)) print('TARGET\ttx\tty') print('{0}\t{1:.01f}\t{2:.01f}'.format('0', trans_coord[0], trans_coord[1])) self.task.DataLogger.writeToLogFile( self.tag, 'ImageRegistration', { 'scale': tf['scale'], 'angle': tf['angle'], 'trans_x': tf['tvec'][0], 'trans_y': tf['tvec'][1] }, time()) return targ_pos else: print('No laser stimulation: Image registration failed.') self.task.DataLogger.writeToLogFile(self.tag, 'ImageRegFail', None, time()) return None
import os import scipy as sp import scipy.misc import matplotlib.pyplot as plt import imreg_dft as ird basedir = os.path.join('..', 'examples') # the TEMPLATE im0 = sp.misc.imread(os.path.join(basedir, "sample1.png"), True) # the image to be transformed im1 = sp.misc.imread(os.path.join(basedir, "sample3.png"), True) result = ird.similarity(im0, im1, numiter=3) ird.imshow(im0, im1, result['timg']) plt.show()
def get_regstat(fixed_image, moving_image, out_stats, out_tm, out_qc, tm): x = [[]] time1 = time.time() # loading images target = plt.imread(fixed_image) #tar=cv2.imread(fixed_image) target_name = os.path.split(fixed_image)[1] # print(target_name) # target=cv2.resize(target,(5705,5705)) # tar=cv2.resize(tar,(5705,5705)) moving = plt.imread(moving_image) #mov=cv2.imread(moving_image) # moving=cv2.resize(moving,(5705,5705)) # mov=cv2.resize(mov,(5705,5705)) moving_name = os.path.split(moving_image)[-1] # print(moving_name) # registration #a=target.shape[0] b=target.shape[1] #moving=cv2.resize(moving,(b,b)) mov_avg=np.mean(moving.astype("float")) print(target.shape) print(moving.shape) res=ird.similarity(target,moving) def_moving=res['timg'] scale=res['scale'] angle=res['angle'] (t0,t1)=res['tvec'] #def_moving, scale, angle, (t0, t1) = imreg.similarity(target, moving) def_mov_array=imreg.similarity_matrix(scale,angle,(t0,t1)) np.save(out_tm, def_mov_array) def_avg=np.mean(def_moving.astype("float")) # def_moving=cv2.resize(def_moving,(5705,5705)) time2 = time.time() ti = time2-time1 # statistical results cc = np.corrcoef(moving.flat, target.flat) r1 = cc[0, 1] score1, diff = compare_ssim(target, moving, full=True, multichannel=True) loss_before = 0.5*(1-r1) + 0.5*(1-score1) cc = np.corrcoef(def_moving.flat, target.flat) r2 = cc[0, 1] score2, diff = compare_ssim(def_moving, target, full=True) loss_after = 0.5*(1-r2) + 0.5*(1-score2) mi_bef = mutual_info_score(moving.ravel(), target.ravel()) mi_aft = mutual_info_score(target.ravel(), def_moving.ravel()) mae = np.sum(np.absolute(target.astype("float") - moving.astype("float"))) u1 = np.sum(target.astype("float") + moving.astype("float")) mean_before = np.divide(mae, u1) u2 = np.sum(target.astype("float") + def_moving.astype("float")) mae = np.sum(np.absolute(target.astype( "float") - def_moving.astype("float"))) mean_after = np.divide(mae, u2) t = Texttable() #print(def_moving.shape) if mi_aft > mi_bef: plt.imsave(tm, def_moving,cmap='gray') else: plt.imsave(tm, moving,cmap='gray') # plt.imsave(tm,def_moving,cmap='gray') x.append([target_name, moving_name, r1, r2, score1, score2, loss_before, loss_after, mi_bef, mi_aft, mean_before, mean_after,mov_avg,def_avg, ti]) t.add_rows(x) t.set_cols_align(['r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r','r', 'r']) t.header(['TARGET', 'MOVING', 'PCC_BEFORE', 'PCC_AFTER', 'SSIM_BEFORE', 'SSIM_AFTER', 'loss_BEFORE', 'loss_AFTER', 'MI_BEFORE', 'MI_AFTER', 'MEAN_BEFORE', 'MEAN_AFTER', 'AVERAGE_INTENSITY_BEFORE', 'AVERAGE_INTENSITY_AFTER','TIME']) def_mov1 = cv2.imread(tm,0) # plotting the results transform = AsinhStretch() + AsymmetricPercentileInterval(0.1, 99.99) targ= transform(target) #targ=cv2.cvtColor(targ.astype(np.uint8),cv2.COLOR_GRAY2RGB) #targ_im=Image.fromarray(targ) plt.imsave('target.tiff', targ, cmap='gray') #plt.imsave(r'/media/u0132399/L-drive/GBW-0075_MILAN_Multiplex/u0140317/registration_comparison/imreg/output_images/target.tiff', targ, cmap='gray') movi = transform(moving) #movi=cv2.cvtColor(movi.astype(np.uint8),cv2.COLOR_GRAY2BGR) plt.imsave('moving.tiff', movi, cmap='gray') #movi_im=Image.fromarray(movi) #plt.imsave(r'/media/u0132399/L-drive/GBW-0075_MILAN_Multiplex/u0140317/registration_comparison/imreg/output_images/moving.tiff', movi, cmap='gray') targ = cv2.imread('target.tiff') #targ=Image.open(targ_im) movi= cv2.imread('moving.tiff') #movi=Image.open(movi_im) #targ = cv2.imread( r'/media/u0132399/L-drive/GBW-0075_MILAN_Multiplex/u0140317/registration_comparison/imreg/output_images/target.tiff') #movi = cv2.imread(r'/media/u0132399/L-drive/GBW-0075_MILAN_Multiplex/u0140317/registration_comparison/imreg/output_images/moving.tiff') # def_mov1=cv2.resize(def_mov1,(5705,5705)) def_mov1 = transform(def_mov1) #print(def_mov2.shape) #def_mov1=cv2.cvtColor(def_mov1.astype(np.uint8),cv2.COLOR_GRAY2BGR) plt.imsave('def_moving.tiff', def_mov1, cmap='gray') #def_mov1=Image.fromarray(def_mov1) #plt.imsave('/media/u0132399/L-drive/GBW-0075_MILAN_Multiplex/u0140317/registration_comparison/imreg/output_images/def_moving.tiff', def_mov1, cmap='gray') def_mov1 = cv2.imread('def_moving.tiff') #def_mov1=Image.open(def_mov1) #def_mov1 = cv2.imread(r'/media/u0132399/L-drive/GBW-0075_MILAN_Multiplex/u0140317/registration_comparison/imreg/output_images/def_moving.tiff') #targ = targ.astype(np.uint8) #movi = movi.astype(np.uint8) #def_mov1 = def_mov1.astype(np.uint8) tar1 = np.zeros(targ.shape) mov1 = np.zeros(movi.shape) def_mov = np.zeros(def_mov1.shape) tar1[:, :, 0] = targ[:, :, 0] mov1[:, :, 2] = movi[:, :, 2] def_mov[:, :, 2] = def_mov1[:, :, 2] tar1 = tar1.astype(np.uint8) mov1 = mov1.astype(np.uint8) def_mov = def_mov.astype(np.uint8) # mov[np.where((mov>[15]).all(axis=2))]=[255,0,0] # def_mov1[np.where((def_mov1>[15]).all(axis=2))]=[255,0,0] alpha = 1 beta = 1 gamma = 0 t1 = cv2.addWeighted(tar1, alpha, mov1, beta, gamma) # filename=target_name+moving_name+'unreg_imreg.tif' # cv2.imwrite(,t1) t2 = cv2.addWeighted(tar1, alpha, def_mov, beta, gamma) # filename=target_name+moving_name+'reg_imreg.tif' # cv2.imwrite(filename,t2) if mi_aft > mi_bef: cv2.imwrite(out_qc, t2) else: cv2.imwrite(out_qc, t1) # plt.figure(figsize=(10,10)) # plt.axis('off') # plt.imshow(t1,cmap='gray') # plt.show(block=False) # plt.pause(5) # plt.close() # plt.figure(figsize=(10,10)) # plt.axis('off') # plt.imshow(t2,cmap='gray') # plt.show(block=False) # plt.pause(5) # plt.close() df = pd.DataFrame(x, columns=['TARGET', 'MOVING', 'PCC_BEFORE', 'PCC_AFTER', 'SSIM_BEFORE', 'SSIM_AFTER', 'loss_BEFORE', 'loss_AFTER', 'MI_BEFORE', 'MI_AFTER', 'MEAN_BEFORE', 'MEAN_AFTER','AVERAGE_INTENSITY_BEFORE', 'AVERAGE_INTENSITY_AFTER', 'TIME']) #df[columns]= ['PCC_BEFORE','PCC_AFTER','SSIM_BEFORE' ,'SSIM_AFTER','loss_BEFORE','loss_AFTER','MI_BEFORE','MI_AFTER','MEAN_BEFORE','MEAN_AFTER'] #writer = pd.ExcelWriter(out_stats) df.to_csv(out_stats, index=None, header=True) # writer.save() #print(t.draw()) return t
import os import scipy as sp import scipy.misc import imreg_dft as ird from astropy.io import fits print "Libraries imported..." names_i = os.listdir('./data_corr_trimmed/i') names_v = os.listdir('./data_corr_trimmed/v') print "Directories enlisted..." print "Entering I alignments..." im_ref = fits.open('data_corr_trimmed/v/'+names_v[8])[0].data for i in range (len(names_i)): im_test = fits.open('data_corr_trimmed/i/'+names_i[i])[0].data result = ird.similarity(im_ref,im_test,numiter=2) print i,names_i[i],"tvec:",result['tvec']," success:",result['success'] out_hdu = fits.PrimaryHDU(result['timg']) out_hdu.writeto('data_align/i/'+names_i[i]) print "I alignments done..." print "Entering V alignments..." im_ref = fits.open('data_corr_trimmed/v/'+names_v[8])[0].data for i in range (len(names_v)): im_test = fits.open('data_corr_trimmed/v/'+names_v[i])[0].data result = ird.similarity(im_ref,im_test,numiter=2) print i,names_v[i],"tvec:",result['tvec']," success:",result['success'] out_hdu = fits.PrimaryHDU(result['timg']) out_hdu.writeto('data_align/v/'+names_v[i]) print "V alignments done..."
def register_directory(image_dir, ref_im='', out_dir='', save_txt=True, save_image=False, **kwargs): '''Run FFT-based rigid image registration comparing images in a directory to a reference image Inputs ------ image_dir : str The path to the image files ref_im : array The image to which each image in hte directory will be registered. Defaults to the first image in the directory out_dir : str The path to write the output .txt fles save_txt : bool Write the affine transformation parameters to a .txt file save_image : bool save the registered output file **kwargs : dict The arguments for the imreg_dft function ''' imd = glob.glob(image_dir + '/*.tif') imd1 = list(imd) imd1.pop(-1) test_frame = imread2(imd1[0]) if (len(test_frame.shape) > 2) and (test_frame.shape[-1] > 1): rgb_flag = True test_frame = test_frame[:, :, 0] warnings.warn("RGB image detected, first channel will be used.") else: rgb_flag = False if not ref_im: ref_im = test_frame # text_file = open(out_dir+'/'+"transform_params.txt", "w") text_file = open(image_dir + "_transform_params.txt", "w") for im1 in imd1: frame_a = imread2(im1) if rgb_flag: frame_a = frame_a[:, :, 0] result = ird.similarity(ref_im, frame_a, **kwargs) transform_params = result['scale'], result['angle'], result['tvec'] if save_image: imname = os.path.split(im1)[-1][:-4] param_str = '_scale'+str(scale)+'_angle'+str(rotangle)\ +'_trans'+str(transvec[0])+'_'+str(transvec[1]) savestr = out_dir + '/' + imname + '.png' toimage(result['timg']).save(savestr) pass if save_txt: print("{0}\t{1}\t{2}".format(*transform_params), file=text_file) text_file.close()