模式识别 matlab函数,MATLAB GUI模式识别 线性判别器实现(含源码)详细直观
function varargout =pr_1(varargin)% PR_1 MATLAB code forpr_1.fig% PR_1, by itself, creates a newPR_1 or raises the existing% singleton*.%% H = PR_1 returns the handle toa new PR_1 or the handle to%...
function varargout =
pr_1(varargin)
% PR_1 MATLAB code for
pr_1.fig
% PR_1, by itself, creates a new
PR_1 or raises the existing
% singleton*.
%
% H = PR_1 returns the handle to
a new PR_1 or the handle to
% the existing
singleton*.
%
% PR_1('CALLBACK',hObject,eventData,handles,...)
calls the local
% function named CALLBACK in
PR_1.M with the given input arguments.
%
% PR_1('Property','Value',...)
creates a new PR_1 or raises the
% existing singleton*.
Starting from the left, property value pairs
are
% applied to the GUI before
pr_1_OpeningFcn gets called. An
% unrecognized property name or
invalid value makes property application
% stop. All
inputs are passed to pr_1_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 pr_1
% Last Modified by GUIDE v2.5
07-Mar-2017 10:38:27
% Begin initialization code -
DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',
mfilename,
...
'gui_Singleton', gui_Singleton,
...
'gui_OpeningFcn', @pr_1_OpeningFcn,
...
'gui_OutputFcn',
@pr_1_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 pr_1
is made visible.
function
pr_1_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 pr_1 (see VARARGIN)
% Choose default command line
output for pr_1
handles.output =
hObject;
% Update handles
structure
guidata(hObject,
handles);
% UIWAIT makes pr_1 wait for
user response (see UIRESUME)
%
uiwait(handles.figure1);
% --- Outputs from this
function are returned to the command line.
function varargout =
pr_1_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)
global im %使用全局变量
%选择图片路径
[filename,pathname] =
...
uigetfile({'*.jpg';'*.bmp';'*.gif'},'选择图片');
%合成路径+文件名
str=[pathname
filename];
%读取图片
im=imread(str);
%使用第一个axes
axes(handles.axes1);
cla;%清空原先的图片内容
%显示图片
imshow(im);
% --- 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 im test1
test2%使用全局变量
%计算rgb平均值
[row,col] =
size(im(:,:,1));
sumr =
sum(sum(im(:,:,1)));
sumg =
sum(sum(im(:,:,2)));
sumb =
sum(sum(im(:,:,3)));
aver =
sumr/(row*col);
aveg =
sumg/(row*col);
aveb =
sumb/(row*col);
%训练集1:提取图像中红色部分的50个点,并得到其R和G值
counter=1;
for i=1:1:row
flag =0;
for j=1:1:col
%人为设定的判别标准,有一定缺陷
%选取标准:像素R值大平均R值+25,且,G值小于平均G值-20,且,为红色点
if
im(i,j,1)>(aver+25) && im(i,j,2)
im(i,j,2)+im(i,j,3)<2*im(i,j,1)
test1(counter,1) = im(i,j,1);%得到R值
test1(counter,2) = im(i,j,2);%得到G值
counter=counter+1;
end;
if
counter==51
flag=1;
break;
end;
end;
if flag==1
break;
end;
end;
%训练集2:提取图像中非红色部分的50个点,并得到其R和G值
counter=1;
for i=1:1:row
flag =0;
for j=1:1:col
if
im(i,j,1)
im(i,j,2)+im(i,j,3)>2*im(i,j,1)
test2(counter,1) = im(i,j,1);
test2(counter,2) = im(i,j,2);
counter=counter+1;
end;
if
counter==51
flag=1;
break;
end;
end;
if flag==1
break;
end;
end;
%使用第二个axes
axes(handles.axes2);
hold off;
plot(test1(:,1),test1(:,2),'r+');
axis([40 200 50
140]);
xlabel('R','FontSize',12,'FontWeight','bold','Color','r');
hold on;
plot(test2(:,1),test2(:,2),'g*');
axis([40 200 50
140]);
ylabel('G','FontSize',12,'FontWeight','bold','Color','g');
hold on;
% --- Executes during object
creation, after setting all properties.
function
edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata
reserved - to be defined in a future version of
MATLAB
% handles empty - handles not created until after all
CreateFcns called
% Hint: edit controls usually
have a white background on Windows.
% See ISPC and
COMPUTER.
if ispc &&
isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press
in pushbutton4.
function
pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata
reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see
GUIDATA)
global test1 test2 test3
w
%使用最小误差平方和法,也就是伪逆法,构造线性分类器
%首先将数据集的矩阵变为首为1的矩阵
for i=1:100
test3(i,1) = 1;
if i<51
test3(i,2) = test1(i,1);
test3(i,3) = test1(i,2);
d(i) = 1;
else
test3(i,2)
= test2(i-50,1);
test3(i,3)
= test2(i-50,2);
d(i) =
-1;
end
end
Y = test3'*test3;
inv_Y = inv(Y);
w =
inv_Y*test3'*d';%计算判别方程函数系数
%使用第二个axes
axes(handles.axes2);
x=40:0.1:200;
y=-w(1)/w(3)-w(2)/w(3)*x;
plot(x,y,'-','LineWidth',2);%在axes2中画出判别函数(直线方程)
axis([40 200 50
140]);
%将判别函数显示在edit1中
set(handles.edit1,'string',['y='
num2str(-w(1)/w(3)) '+' num2str(-w(2)/w(3))
'*x'],'FontSize',15);
% --- 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)
global im im2
w%使用全局变量
[row,col] =
size(im(:,:,1));
for i=1:row
for j=1:col
if
w(1)+w(2)*double(im(i,j,1))+w(3)*double(im(i,j,2)) >
0
im2(i,j,1)=200;im2(i,j,2)=10;im2(i,j,3)=10;
end
if
w(1)+w(2)*double(im(i,j,1))+w(3)*double(im(i,j,2)) <=
0
im2(i,j,1)=200;im2(i,j,2)=200;im2(i,j,3)=200;
end
%
flag(i,j)=(w(1)+w(2)*double(im(i,j,1))+w(3)*double(im(i,j,2)));
end
end
%使用第三个axes
axes(handles.axes3);
cla;%清空前面的图片
im2=uint8(im2);
imshow(im2);
% --- 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)
close();
更多推荐
所有评论(0)