image registration using fft

asked 2012-10-15 01:32:51 -0500

mrgloom gravatar image

I'm trying to register 2 images.First image don't change and second image is changing at each iteration by homography matrix and I'm using minimizer to find maximum of phase correlation, but for some reason it's doesn't work, maybe I'm conceptually wrong or maybe I'm using minimizer in a wrong way.

here is code on matlab, I'm planning to rewrite it on opencv.

main.m

global model; 
global scene; 

scene = imread('001_001.tif'); 
%test distortion homography matrix m11 m12 m21 m22 m31 m32 then we add nonchangeble m13=0 m23=0 m33=1 
H= [0.95 -0.05 0.0000 1.0000 -0.0000 -0.0000];% so we can test precision
model=warp_im(scene,H); 

%m11 m12 m21 m22 m31 m32 
init_sol= [1 0 0 1 0 0]; %initial solution

opts= optimset('Algorithm','interior-point','MaxFunEvals',5000,'MaxIter',5000,'TolFun',1e-6,'TolX',1e-10); 

problem= createOptimProblem('fmincon','objective','compute_NCC','x0',init_sol,'options',opts); 

gs= GlobalSearch; 
[x,f]= run(gs,problem) 
param=x;

compute_NCC.m

function peak = compute_NCC(param) 

global scene; 
global model; 

img= warp_im(model,param); 

%padd with zeros
sz1= size(scene); % y x  
sz2= size(img); 
sz= abs(sz2-sz1); 
pad_im1=scene;pad_im2=img; 
if(sz1(1)>sz2(1))%y 
pad_im2= padarray(img,[sz(1),0],'post'); 
else 
pad_im1= padarray(scene,[sz(1),0],'post'); 
end; 
if(sz1(2)>sz2(2))%x 
pad_im2= padarray(pad_im2,[0,sz(2)],'post'); 
else 
pad_im1= padarray(pad_im1,[0,sz(2)],'post'); 
end; 

FFT1 = fftshift(fft2(pad_im1)); 
FFT2 = fftshift(fft2(pad_im2)); 

FFT = FFT1.*conj(FFT2); 
FFT = FFT./abs(FFT); 

mag = (ifft2(FFT)); 

peak= -abs(max(max(mag))) % minimize

warp_im.m

function out = warp_im(img,param) 
param=[param(1:2),0,param(3:4),0,param(5:6),1] 
H=reshape(param,3,3)' 

tform = maketform('projective',H'); 
out= imtransform(img,tform);
edit retag flag offensive close merge delete