function varargout = gui4(varargin)
% GUI4 MATLAB code for gui4.fig
% GUI4, by itself, creates a new GUI4 or raises the existing
% singleton*.
%
% H = GUI4 returns the handle to a new GUI4 or the handle to
% the existing singleton*.
%
% GUI4('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUI4.M with the given input arguments.
%
% GUI4('Property','Value',...) creates a new GUI4 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before gui4_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to gui4_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help gui4
% Last Modified by GUIDE v2.5 20-Dec-2017 21:14:21
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @gui4_OpeningFcn, ...
'gui_OutputFcn', @gui4_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
% End initialization code - DO NOT EDIT
% --- Executes just before gui4 is made visible.
function gui4_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 gui4 (see VARARGIN)
% Choose default command line output for gui4
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes gui4 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = gui4_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;
% --- 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)
[filename,pathname]=uigetfile('*.bmp');%获取原始图像
A=strcat(pathname,filename);%合并文件名与路径
picture=imread(A);%载入*.txt文件
axes(handles.axes1);%选择axes1
imshow(picture);%显示图片
handles.picture=picture;
guidata(hObject,handles);
% --- 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)
[filename,pathname]=uigetfile('*.bmp');%获取原始图像
A=strcat(pathname,filename);%合并文件名与路径
target=imread(A);%载入*.txt文件
axes(handles.axes2);%选择axes1
imshow(target);%显示图片
handles.target=target;
guidata(hObject,handles);
% --- 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)
tic;
picture=handles.picture;
target=handles.target;
RES=normxcorr2(target,picture);%将两图像的灰度矩阵进行互相关并归一化
[ypeak, xpeak] = find(RES==max(RES(:)));%找到互相关矩阵中的最大值对应的目标矩阵偏移量
yoffSet = ypeak-size(target,1)+1;
xoffSet = xpeak-size(target,2)+1;%根据互相关定义,将偏移量转化为矩阵第一行第一列对应的位置
axes(handles.axes3);
imshow(picture);%在结果图上画出结果
hold on;
plot([xoffSet:xoffSet+size(target,2)-1],[yoffSet:yoffSet],'b.');
plot([xoffSet:xoffSet],[yoffSet:yoffSet+size(target,1)-1],'b.');
plot([xoffSet:xoffSet+size(target,2)-1],[yoffSet+size(target,1)-1:yoffSet+size(target,1)-1],'b.');
plot([xoffSet+size(target,2)-1:xoffSet+size(target,2)-1],[yoffSet:yoffSet+size(target,1)-1],'b.');%将得到的目标矩阵位置以蓝色图框表出
hold off;
a=toc;
set(handles.edit1,'string',num2str(a));
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
tic;
picture=handles.picture;
target=handles.target;
S=double(picture);%读入的图片数据类型是uint8,转换类型为double
g=double(target);
x=size(g,1); %获取g的行数
m=size(S,1);%S的X坐标
y=size(g,2); %获取g的列数
n=size(S,2);%S的Y坐标
Sxy=0*g; %定义临时变量
tempg=0;
tempg=double(tempg);
gvector=ones(x*y,1); %定义临时变量,将g矩阵存储为列向量的形式
Svector=0*gvector; %定义临时变量,将S矩阵的分块存储为列向量的形式
axes(handles.axes3);
imshow(picture);%在结果图上画出结果
hold on;
for i1=1:1:x %使用循环,将g矩阵存储为列向量的形式
for i2=1:1:y
gvector((i1-1)*y+i2,1)=g(i1,i2);
end
end
for numy=1:1:n-y+1 %使用循环,将S矩阵分块存储为列向量的形式
for numx=1:1:m-x+1
for i1=1:1:x
for i2=1:1:y
Sxy(i1,i2)=S(i1+numx-1,i2+numy-1);
end
end
for i1=1:1:x
for i2=1:1:y
Svector((i1-1)*y+i2,1)=Sxy(i1,i2);
end
end
R=corrcoef(gvector,Svector); %求得S分块与模板g的相关系数
if R(1,2)>0.95 %找出图片中与模板相匹配的位置,并标记
for i=1:1:y
text(numy+i-1,numx,'\o ','Color','blue');
end
for i=1:1:x
text(numy,numx+i-1,'\o ','Color','blue');
end
for i=1:1:x
text(numy+y-1,numx+i-1,'\o ','Color','blue');
end
for i=1:1:y
text(numy+i-1,numx+x-1,'\o ','Color','blue');
end
break;
end
end
if R(1,2)>0.95
break;
end
end
a=toc;
set(handles.edit1,'string',num2str(a));
% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
tic;
picture=handles.picture;
target=handles.target;
S=double(picture);%读入的图片数据类型是uint8,转换类型为double
g=double(target);
x=size(g,1); %获取g的行数
m=size(S,1); %S的X坐标
y=size(g,2); %获取g的列数
n=size(S,2); %S的Y坐标
[R,corr1]=Dfft(S,g,1,1); %使用FFT和IFFT函数,求解相关系数
gg=sum(sum(g.^2)); %使用FFT和IFFT函数,求解相关系数
St1=zeros(1,y); %定义变量
St2=St1; %定义变量
for numy=1:1:n-y+1
for numx=1:1:m-x+1
if (numx==1)&&(numy==1)%求解numx=1时的Sxy的值,为之后求解numx!=1时的Sxy做准备
Sxy=S(numx:1:x+numx-1,numy:1:y+numy-1);
ss0=sum(sum(Sxy.^2));
ss=ss0;
elseif (numx==1)&&(numy~=1) %使用公式(5)计算列的
St1=S(numx:1:numx+x-1,numy-1);
St2=S(numx:1:numx+x-1,numy+y-1);
ss0=ss0+sum(St2.^2)-sum(St1.^2);
ss=ss0;
else %根据公式(6)求解numx!=1时的Sxy的值,
St1=S(numx-1,numy:1:numy+y-1);
St2=S(numx+x-1,numy:1:numy+y-1);
ss=ss+d