""" Test script to generate artifically shifted images and process using the feature tracking methods. """ #%%----------------------------------------------------------------------------- # setup #------------------------------------------------------------------------------- import numpy as np import matplotlib.pyplot as plt plt.ion() import cv2 as cv import lib_feature_tracking as lib ##Load data imgs = np.load('random positions.npy') #imgs = imgs[0:10] #just do 10 for testing ##Setup range of shifts xshifts = np.arange(1.5,-1.55,-0.1) #xshifts = np.arange(-630,631,1)/10. yshifts = np.zeros( xshifts.shape) shifts = zip(xshifts,yshifts) ##Other setup win = (128,128,256,256) #window size to use (x0,y0,w,h) M = 10 #image magnification 10 for 0.1 pixel shift #%%----------------------------------------------------------------------------- # Setup matcher methods #------------------------------------------------------------------------------- ncc = lib.NCC() orb = lib.ORB(patchSize=64, fastThreshold=36, distance_threshold=64 ) brisk = lib.BRISK(fastThreshold=36, patternScale=1.8, distance_threshold=64 ) usurf = lib.USURF(threshold=50, distance_threshold=0.5, norm=cv.NORM_L1 ) surf = lib.SURF(threshold=50, distance_threshold=0.5, norm=cv.NORM_L1 ) #%%----------------------------------------------------------------------------- # process multiple speckle patterns and average #------------------------------------------------------------------------------- #clear all stored results ncc.reset() orb.reset() brisk.reset() usurf.reset() surf.reset() ##Processing loop for n in range(0, len(shifts)): shift = shifts[n] print 'doing shift', shift for i in range(0,imgs.shape[0]): ref_img = imgs[i] #extract a shifted the window img0,img1 = lib.get_shifted_windows(ref_img, win0=win, delta=shift, M=10) ##Do ORB Ax,Ay,theta = orb.calc_shift_and_rotation(img0, img1, adj_fast_threshold=True) #print '\torb: {0:0.2f}{1:0.2f}{1:0.2f}'.format(Ax,Ay,theta) ##Do NCC processsing Ax,Ay = ncc.calc_shift(img0,img1) #print '\tncc: {0:0.2f}{1:0.2f}'.format(Ax,Ay) ##Do BRISK Ax,Ay,theta = brisk.calc_shift_and_rotation(img0, img1) #print '\tbrisk: {0:0.2f}{1:0.2f}{1:0.2f}'.format(Ax,Ay,theta) ##Do USURF Ax,Ay,theta = usurf.calc_shift_and_rotation(img0, img1) #print '\tusurf: {0:0.2f}{1:0.2f}{1:0.2f}'.format(Ax,Ay,theta) ##Do SURF Ax,Ay,theta = surf.calc_shift_and_rotation(img0, img1) #print '\tsurf: {0:0.2f}{1:0.2f}{1:0.2f}'.format(Ax,Ay,theta) #Find the means i1 = n*imgs.shape[0] i2 = (n+1)*imgs.shape[0] print 'Applied shift: Ax,Ay = {0:0.2f},{0:0.2f}'.format(*shift) print 'NCC: Ax,Ay = {0:0.2f},{0:0.2f}'.format( np.mean(ncc.Axs[i1: i2] ), np.mean(ncc.Ays[i1: i2] )) print 'ORB: Ax,Ay = {0:0.2f},{0:0.2f}'.format( np.mean(orb.Axs[i1: i2] ), np.mean(orb.Ays[i1: i2] )) print 'BRISK: Ax,Ay = {0:0.2f},{0:0.2f}'.format( np.mean(brisk.Axs[i1: i2] ), np.mean(brisk.Ays[i1: i2] )) print 'USURF: Ax,Ay = {0:0.2f},{0:0.2f}'.format( np.mean(usurf.Axs[i1: i2] ), np.mean(usurf.Ays[i1: i2] )) print 'SURF: Ax,Ay = {0:0.2f},{0:0.2f}'.format( np.mean(surf.Axs[i1: i2] ), np.mean(surf.Ays[i1: i2] )) print '\n' #make results arrays Axs_ncc = np.array(ncc.Axs).reshape( len(shifts), imgs.shape[0]) Ays_ncc = np.array(ncc.Ays).reshape( len(shifts), imgs.shape[0]) Axs_orb = np.array(orb.Axs).reshape( len(shifts), imgs.shape[0]) Ays_orb = np.array(orb.Ays).reshape( len(shifts), imgs.shape[0]) Axs_brisk = np.array(brisk.Axs).reshape( len(shifts), imgs.shape[0]) Ays_brisk = np.array(brisk.Ays).reshape( len(shifts), imgs.shape[0]) Axs_usurf = np.array(usurf.Axs).reshape( len(shifts), imgs.shape[0]) Ays_usurf = np.array(usurf.Ays).reshape( len(shifts), imgs.shape[0]) Axs_surf = np.array(surf.Axs).reshape( len(shifts), imgs.shape[0]) Ays_surf = np.array(surf.Ays).reshape( len(shifts), imgs.shape[0]) #%%----------------------------------------------------------------------------- # save #------------------------------------------------------------------------------- np.savez( 'modelled shifts.npz', shifts=shifts, Axs_ncc=Axs_ncc, Ays_ncc=Ays_ncc, Axs_orb=Axs_orb, Ays_orb=Ays_orb, Axs_brisk=Axs_brisk, Ays_brisk=Ays_brisk, Axs_usurf=Axs_usurf, Ays_usurf=Ays_usurf, Axs_surf=Axs_surf , Ays_surf=Ays_surf ) #%%----------------------------------------------------------------------------- # Load from file #------------------------------------------------------------------------------- data = np.load('modelled shifts.npz') for key in data.keys(): exec( key+'=data["'+key+'"]') #%%----------------------------------------------------------------------------- # Plot - bias error #------------------------------------------------------------------------------- f = plt.figure( figsize=(6,3)) ax1 = f.add_axes( (0.13,0.13,0.8,0.8)) ax1.plot( xshifts, Axs_ncc.mean(axis=1)-xshifts, '--k', label='NCC', lw=2) ax1.plot( xshifts, Axs_orb.mean(axis=1)-xshifts, '.-', label='ORB') ax1.plot( xshifts, Axs_brisk.mean(axis=1)-xshifts, '.-', label='BRISK') ax1.plot( xshifts, Axs_usurf.mean(axis=1)-xshifts, '.-', label='USURF') ax1.plot( xshifts, Axs_surf.mean(axis=1)-xshifts, '.-', label='SURF') ax1.set_xlabel('Applied translation [pixels]') ax1.set_ylabel('Bias error in measured shift [pixels]') ax1.set_xlim(-2,3) plt.legend() #set font sizes texts = f.findobj(plt.Text) res = plt.setp(texts, fontsize=8) f.savefig('../figures/pixel locking.pdf', dpi=600, transparent=True)