Code covered by the BSD License  

Highlights from
Phase Based Template Matching

image thumbnail
from Phase Based Template Matching by Usman Qayyum
Phase information is used for matching the input imagery with the template.

Phase_base_gui(varargin)
function varargout = Phase_base_gui(varargin)

% PHASE BASED TEMPLATE MATCHING
% Phase information is used for matching the input imagery with the
% template. Both the images are filtered with canny edge detector. The timing
% efficiency is introduced by implementing skiping steps while doing
% correlation.

%The advantage of Phase based correlation technique is that it
% shows good response against shift/brightness variation and noise addition 
% i.e. salt & pepper/Gaussian noise. Templates includes small portion of cropped 
% from input imagery with gaussian noise introduced in them.

%Programmed by usman qayyum  
%mrusmanqayyum@yahoo.com   October 08,2008

% Edit the above text to modify the response to help Phase_base_gui
% Last Modified by GUIDE v2.5 08-Oct-2008 14:30:24
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Phase_base_gui_OpeningFcn, ...
                   'gui_OutputFcn',  @Phase_base_gui_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
global img1;
global imgt;
% End initialization code - DO NOT EDIT

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global img1;
[filename, pathname] = uigetfile('*.jpg', 'Pick Input image');
img1=imread(strcat(pathname,filename));
figure,imshow(img1);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global imgt;
[filename, pathname] = uigetfile('*.jpg', 'Pick template image');
imgt=imread(strcat(pathname,filename));
figure,imshow(imgt);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 global img1;
 global imgt; 
 
 lapping_dist=10;   %block overlapping distance in_actual_it_should be equal to 1
  
  %initialization of the variables
  maxx=double(0);
  row=1;
  col=1;
  
  tic
 
  if(isrgb(img1))
        img1_gray=edge(rgb2gray(img1),'canny');
        imgisrgb=1; 
  else
        img1_gray=edge((img1),'canny');
        imgisrgb=0; 
  end
    
  if(isrgb(imgt))
        imgt_gray=edge(rgb2gray(imgt),'canny');       
  else
        imgt_gray=edge((imgt),'canny');       
  end
 
 
   row_template=size(imgt,1);
   col_template=size(imgt,2);
   row_img=size(img1_gray,1);
   col_img=size(img1_gray,2);
    
    for i=1:lapping_dist:row_img-row_template      
       for j=1:lapping_dist:col_img-col_template    
              
            l=i;  %temp variables are used to avoid the over writing
            m=j;
            img_temp=fft2(double(img1_gray(l:l+row_template-1,m:m+col_template-1)));
            img_temp1=conj(fft2(double(imgt_gray(1:row_template,1:col_template))));
            res_corr(i,j)=  max(max(abs(ifft2(((img_temp.*img_temp1)./((abs(img_temp .*img_temp1)+.1)))))));
          
        end
    end
 
  
   [row,col]=find(res_corr==max(max(res_corr))); %coordinates finding where the peak of the correlation has been found
    
 %figure,imshow( res_corr),title 'Correlation Surface'      
 
 figure,imshow(img1),title 'Target Matched image'      
 rectangle('position',[col row 45 45],'LineWidth',2); %a certain offset can be found due to skipping step introduced for time efficient correlation
 
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 % --- Executes just before Phase_base_gui is made visible.
function Phase_base_gui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to Phase_base_gui (see VARARGIN)
% Choose default command line output for Phase_base_gui
  handles.output = hObject;
% Update handles structure
  guidata(hObject, handles);
% UIWAIT makes Phase_base_gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% --- Outputs from this function are returned to the command line.
function varargout = Phase_base_gui_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


 
%Programmed by usman qayyum  
%mrusmanqayyum@yahoo.com   October 08,2008

Contact us at files@mathworks.com