def register_image( img, ref=None ): #img must be 3 channels, ref could be None which is random noise wiht a margin, a string with the file name and or path, or the word 'solid' which is a solid color with a margin """pystackreg library""" transformations = [] if ref == None: ref = generate_template(img) #default color elif ref == 'solid': #random color between 5 - 250 ref = generate_template(img, color=random_color()) elif ref == 'gray': ref = generate_template(img, color=[125, 125, 125]) else: ref = cv2.imread(ref) print("\nshowing original image...\n") plt.imshow(img) plt.show() print("\nshowing reference image...\n") plt.imshow(ref) plt.show() #Translational transformation sr = StackReg(StackReg.TRANSLATION) min_shape = [ min(ref.shape[0], img.shape[0]), min(ref.shape[1], img.shape[1]) ] img = cv2.resize(img, (min_shape[1], min_shape[0])) ref = cv2.resize(ref, (min_shape[1], min_shape[0])) out_tra = sr.register_transform(ref[:, :, 0], img[:, :, 0]) transformations.append(out_tra) #Rigid Body transformation sr = StackReg(StackReg.RIGID_BODY) out_rot = sr.register_transform(ref[:, :, 0], img[:, :, 0]) transformations.append(out_rot) #Scaled Rotation transformation sr = StackReg(StackReg.SCALED_ROTATION) out_sca = sr.register_transform(ref[:, :, 0], img[:, :, 0]) transformations.append(out_sca) #Affine transformation sr = StackReg(StackReg.AFFINE) out_aff = sr.register_transform(ref[:, :, 0], img[:, :, 0]) transformations.append(out_aff) #Bilinear transformation sr = StackReg(StackReg.BILINEAR) out_bil = sr.register_transform(ref[:, :, 0], img[:, :, 0]) transformations.append(out_bil) return transformations, ref
def registration(img1, img2, method='phase_cross_correlation'): """This function is used to register 2 images. Args: :param img1: the first image. :param img2: the second image. :param method: which method is used for registration. List methods: A Pyramid Approach to Subpixel Registration Based on Intensity (http://bigwww.epfl.ch/thevenaz/stackreg/#Related) - translation - rigid (translation + rotation) - scaled rotation (translation + rotation + scaling) - affine (translation + rotation + scaling + shearing) - bilinear (non-linear transformation; does not preserve straight lines) - phase_cross_correlation """ corrected_img2 = None if method == 'translation': sr = StackReg(StackReg.TRANSLATION) corrected_img2 = sr.register_transform(img1, img2) elif method == 'rigid': sr = StackReg(StackReg.RIGID_BODY) corrected_img2 = sr.register_transform(img1, img2) elif method == 'scaled_rotation': sr = StackReg(StackReg.SCALED_ROTATION) corrected_img2 = sr.register_transform(img1, img2) elif method == 'affine': sr = StackReg(StackReg.AFFINE) corrected_img2 = sr.register_transform(img1, img2) elif method == 'bilinear': sr = StackReg(StackReg.BILINEAR) corrected_img2 = sr.register_transform(img1, img2) elif method == 'phase_cross_correlation': shift_dis, _, _ = phase_cross_correlation(img1, img2) corrected_img2 = shift(img2, shift=shift_dis, mode='constant') return corrected_img2
""" from pystackreg import StackReg from skimage import io from matplotlib import pyplot as plt #The following example opens two different files and registers them #using all different possible transformations #load reference and "moved" image ref_img = io.imread('images/for_alignment/shale_for_alignment00.tif') offset_img = io.imread('images/for_alignment/shale_for_alignment01.tif') #Translational transformation sr = StackReg(StackReg.TRANSLATION) out_tra = sr.register_transform(ref_img, offset_img) plt.imshow(out_tra, cmap='gray') #Rigid Body transformation sr = StackReg(StackReg.RIGID_BODY) out_rot = sr.register_transform(ref_img, offset_img) #Scaled Rotation transformation #sr = StackReg(StackReg.SCALED_ROTATION) #out_sca = sr.register_transform(ref_img, offset_img) #Affine transformation sr = StackReg(StackReg.AFFINE) out_aff = sr.register_transform(ref_img, offset_img) #Bilinear transformation
num_rois = len(rois) if num_rois <= shown_rois: for index in range(num_rois, shown_rois): if index > 0: cv2.destroyWindow('roi_' + str(index)) #print('number of detected ROIs: ', num_rois) shown_rois = num_rois #cell_counter +=1 if len(rois) > 0: t0 = time.time() curr = rois[0] out = sr.register_transform(prev, curr) #out = out.astype('uint8') #print (time.time()-t0) #mip = np.maximum(mip,rois[0]) #print(np.amax(out)) mip = np.maximum(mip, out) prev = out mip_rescaled = np.clip(mip.astype('float') * 3, 20, 255).astype( 'uint8' ) #multiplication with 3 is to increase the contrast only #mip_rescaled = np.clip((mip/np.amax(mip)*255),20,255).astype('uint8') mip_resized = cv2.resize(mip_rescaled, dim,
from pystackreg import StackReg from skimage import io from matplotlib import pyplot as plt #The following example opens two different files and registers them #using all different possible transformations #load reference and "moved" image ref_img = io.imread( 'images/for_alignment/translated/shale_for_alignment00.tif') offset_img = io.imread( 'images/for_alignment/translated/shale_for_alignment01.tif') #Translational transformation sr = StackReg(StackReg.TRANSLATION) #Create an operator first out_tra = sr.register_transform(ref_img, offset_img) #Apply the operator plt.imshow(out_tra, cmap='gray') #Rigid Body transformation sr = StackReg(StackReg.RIGID_BODY) out_rot = sr.register_transform(ref_img, offset_img) #Scaled Rotation transformation #sr = StackReg(StackReg.SCALED_ROTATION) #out_sca = sr.register_transform(ref_img, offset_img) #Affine transformation sr = StackReg(StackReg.AFFINE) out_aff = sr.register_transform(ref_img, offset_img) #Plotting a few outputs
from pystackreg import StackReg from skimage import io import cv2 import matplotlib.pyplot as plt #load reference and "moved" image path = 'C:\\Users\\Andrea Bassi\\Documents\\Data\\PROCHIP\\Throughput_video\\registration_test\\' #filename = 'dual_color_stack' filename = 'PSF' ref = io.imread(path + 'im0.tif') mov = io.imread(path + 'im1.tif') #Translational transformation sr = StackReg(StackReg.RIGID_BODY) out_tra = sr.register_transform(ref, mov) imgplot = plt.imshow(out_tra) plt.show() #import cv2 #cv2.imshow("Out", out_tra/256) #cv2.waitKey(0)
plt.show() plt.imshow(template) plt.show() gray_test = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY) plt.imshow(gray_test) plt.show() #%% #load reference and "moved" image ref = template mov = img_rotated #Translational transformation sr = StackReg(StackReg.TRANSLATION) out_tra = sr.register_transform(ref[:, :, 0], mov[:, :, 0]) #Rigid Body transformation sr = StackReg(StackReg.RIGID_BODY) out_rot = sr.register_transform(ref[:, :, 0], mov[:, :, 0]) #Scaled Rotation transformation sr = StackReg(StackReg.SCALED_ROTATION) out_sca = sr.register_transform(ref[:, :, 0], mov[:, :, 0]) #Affine transformation sr = StackReg(StackReg.AFFINE) out_aff = sr.register_transform(ref[:, :, 0], mov[:, :, 0]) #Bilinear transformation sr = StackReg(StackReg.BILINEAR)