Вы находитесь на странице: 1из 7

function varargout = EdgeDetection(varargin)

% EDGEDETECTION M-file for EdgeDetection.fig


% EDGEDETECTION, by itself, creates a new EDGEDETECTION or raises the
existing
% singleton*.
%
% H = EDGEDETECTION returns the handle to a new EDGEDETECTION or the
handle to
% the existing singleton*.
%
% EDGEDETECTION('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in EDGEDETECTION.M with the given input
arguments.
%
% EDGEDETECTION('Property','Value',...) creates a new EDGEDETECTION or
raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before EdgeDetection_OpeningFunction gets called.
An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to EdgeDetection_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 EdgeDetection

% Last Modified by GUIDE v2.5 31-Aug-2007 14:33:20

% Begin initialization code - DO NOT EDIT


gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @EdgeDetection_OpeningFcn, ...
'gui_OutputFcn', @EdgeDetection_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 EdgeDetection is made visible.


function EdgeDetection_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 EdgeDetection (see VARARGIN)

% Choose default command line output for EdgeDetection


handles.output = hObject;

% Update handles structure


guidata(hObject, handles);

% UIWAIT makes EdgeDetection wait for user response (see UIRESUME)


% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = EdgeDetection_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 LoadFile.


function LoadFile_Callback(hObject, eventdata, handles)
% hObject handle to LoadFile (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clc;

[handles.filename, pathname] = uigetfile('*.bmp','½Ð¿ï¾Ü¤@ÓÀɮרӶ}±Ò');


if isequal(handles.filename,0)
disp('User selected Cancel');
else
disp(['User selected', fullfile(pathname, handles.filename)]);
In = [pathname, handles.filename];
end

[handles.image,handles.map]=imread(In); %Ū¨ú¹Ï¹³
axes(handles.axes1); %«ü©wÅã¥Ü¹Ï¤ùªº¦a¤è
imshow(handles.image,handles.map) %Åã¥Üì¹Ï¹³
title('ì©l¹Ï§Î');
guidata(hObject,handles);

% --- Executes on button press in RunButton.


function RunButton_Callback(hObject, eventdata, handles)
% hObject handle to RunButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

bmparr = mat2gray(double(handles.image),[0,255]);
[xlen ylen] = size(bmparr);
workarr = zeros(xlen,ylen);

% ¥ÎSobelºâ¤l¨Ó³B²z

dx = zeros(xlen,ylen);
dy = zeros(xlen,ylen);
dxy = zeros(xlen,ylen);
h1=[-1 0 1;-2 0 2;-1 0 1]/8;
h2=[-1 -2 -1;0 0 0;1 2 1]/8;
for i=1:xlen
for j=1:ylen
for k=1:3
for l=1:3
xp=i-k+2;
yp=j-l+2;
if((xp>=1)&&(xp=xlen)&&(yp>=1)&&(yp=ylen))
dx(i,j)=dx(i,j)+h1(k,l)*bmparr(xp,yp);
dy(i,j)=dy(i,j)+h2(k,l)*bmparr(xp,yp);
end
end
end
end
end
dx = abs(dx);
dy = abs(dy);
dxy = dx.*dx+dy.*dy;
th = 4.0*sum(sum(dxy(2:xlen-1,2:ylen-1)))/((xlen-2)*(ylen-2));
for i=2:xlen-1
for j=2:ylen-1
if(dxy(i,j)>th)
workarr(i,j)=1;
else
workarr(i,j)=0;
end
end
end
workarr=mat2gray(workarr);
axes(handles.axes2); %«ü©wÅã¥Ü¹Ï¤ùªº¦a¤è
imshow(workarr);
title('¸gSobelºâ¤l³B²z«áªº¹Ï§Î');

% ¥ÎPrewittºâ¤l¨Ó³B²z
dx=zeros(xlen,ylen);
dy=zeros(xlen,ylen);
dxy=zeros(xlen,ylen);
h1=[-1 0 1;-1 0 1;-1 0 1]/6;
h2=[-1 -1 -1;0 0 0;1 1 1]/6;
for i=1:xlen
for j=1:ylen
for k=1:3
for l=1:3
xp=i-k+2;
yp=j-l+2;
if((xp>=1)&&(xp=xlen)&&(yp>=1)&&(yp=ylen))
dx(i,j)=dx(i,j)+h1(k,l)*bmparr(xp,yp);
dy(i,j)=dy(i,j)+h2(k,l)*bmparr(xp,yp);
end
end
end
end
end
dx=abs(dx);
dy=abs(dy);
dxy=dx.*dx+dy.*dy;
th=4.0*sum(sum(dxy(2:xlen-1,2:ylen-1)))/((xlen-1)*(ylen-1));
for i=2:xlen-1
for j=2:ylen-1
if(dxy(i,j)>th) workarr(i,j)=1;
else workarr(i,j)=0; end
end
end
workarr=mat2gray(workarr);
axes(handles.axes3); %«ü©wÅã¥Ü¹Ï¤ùªº¦a¤è
imshow(workarr);
title('¸gPrewittºâ¤l³B²z«áªº¹Ï§Î');

% ¥ÎRobertsºâ¤l¨Ó³B²z

dx=zeros(xlen,ylen);
dy=zeros(xlen,ylen);
dxy=zeros(xlen,ylen);
h1=[1 0;0 -1]/sqrt(2);
h2=[0 1;-1 0]/sqrt(2);
for i=1:xlen
for j=1:ylen
for k=1:2
for l=1:2
xp=i-k+2;
yp=j-l+2;
if((xp>=1)&&(xp=xlen)&&(yp>=1)&&(yp=ylen))
dx(i,j)=dx(i,j)+h1(k,l)*bmparr(xp,yp);
dy(i,j)=dy(i,j)+h2(k,l)*bmparr(xp,yp);
end
end
end
end
end
dx=abs(dx);
dy=abs(dy);
dxy=dx.*dx+dy.*dy;
th=6.0*sum(sum(dxy(2:xlen-1,2:ylen-1)))/((xlen-2)*(ylen-2));
for i=2:xlen-1
for j=2:ylen-1
if(dxy(i,j)>th) workarr(i,j)=1;
else workarr(i,j)=0; end
end
end
workarr=mat2gray(workarr);
axes(handles.axes4); %«ü©wÅã¥Ü¹Ï¤ùªº¦a¤è
imshow(workarr);
title('¸gRobertsºâ¤l³B²z«áªº¹Ï§Î');
% ¥ÎMarrºâ¤l¨Ó³B²z
sigma=2;
m=ceil(sigma*3)*2+1;
h=fspecial('log',m,sigma);
h=h-sum(h(:))/(m^2);
diff2=zeros(xlen,ylen);
dt=ceil((m-1)/2);
for i=1:xlen
for j=1:ylen
for k=1:m
for l=1:m
xp=i-k+1+dt;
yp=j-l+1+dt;
if((xp>=1)&&(xp=xlen)&&(yp>=1)&&(yp=ylen))
diff2(i,j)=diff2(i,j)+h(k,l)*double(bmparr(xp,yp)); end
end
end
end
end
th=0.75*mean2(abs(diff2(2:xlen-1,2:ylen-1)));
for i=2:xlen-1
for j=2:ylen-1
if(diff2(i,j)<0)
if((diff2(i,j-1)>0)&&(diff2(i,j-1)-diff2(i,j)>th))
workarr(i,j)=1; end
if((diff2(i,j+1)>0)&&(diff2(i,j+1)-diff2(i,j)>th))
workarr(i,j)=1; end
if((diff2(i-1,j)>0)&&(diff2(i-1,j)-diff2(i,j)>th))
workarr(i,j)=1; end
if((diff2(i+1,j)>0)&&(diff2(i+1,j)-diff2(i,j)>th))
workarr(i,j)=1; end
elseif(diff2(i,j)==0)
if((diff2(i,j-1)<0)&&(diff2(i,j+1)>0)&&(diff2(i,j+1)-
diff2(i,j-1)>2*th)) workarr(i,j)=1; end
if((diff2(i,j-1)>0)&&(diff2(i,j+1)<0)&&(diff2(i,j-1)-
diff2(i,j+1)>2*th)) workarr(i,j)=1; end
if((diff2(i-1,j)<0)&&(diff2(i+1,j)>0)&&(diff2(i+1,j)-
diff2(i-1,j)>2*th)) workarr(i,j)=1; end
if((diff2(i-1,j)>0)&&(diff2(i+1,j)<0)&&(diff2(i-1,j)-
diff2(i+1,j)>2*th)) workarr(i,j)=1; end
end
end
end
workarr=mat2gray(workarr);
axes(handles.axes5); %«ü©wÅã¥Ü¹Ï¤ùªº¦a¤è
imshow(workarr);
title('¸gMarrºâ¤l³B²z«áªº¹Ï§Î');

% ¥ÎCannyºâ¤l¨Ó³B²z

sigma=1;
var=1:30;
dt=max(find(exp(-(var.*var)/(2*sigma*sigma))>0.0001));
if(isempty(dt)) dt=1; end
m=2*dt+1;
tmp=(-dt:dt);
gau=exp(-(tmp.*tmp)/(2*sigma*sigma))/(2*pi*sigma*sigma);
[fx,fy]=meshgrid(-dt:dt,-dt:dt);
h1=-fx.*exp(-(fx.*fx+fy.*fy)/(2*sigma*sigma))/(pi*sigma*sigma);
h2=h1';
fbmparr=zeros(xlen,ylen);
dx=zeros(xlen,ylen);
dy=zeros(xlen,ylen);
dxy=zeros(xlen,ylen);
bmps=repmat(false,xlen,ylen);
for i=1:xlen
for j=1:ylen
for k=1:m
for l=1:m
xp=i-k+1+dt;
yp=j-l+1+dt;
if(xp<1) xp=1;
elseif(xp>xlen) xp=xlen; end
if(yp<1) yp=1;
elseif(yp>ylen) yp=ylen; end
fbmparr(i,j)=fbmparr(i,j)+gau(k)*gau(l)*bmparr(xp,yp);
end
end
end
end
for i=1:xlen
for j=1:ylen
for k=1:m
for l=1:m
xp=i-k+1+dt;
yp=j-l+1+dt;
if(xp<1) xp=1;
elseif(xp>xlen) xp=xlen; end
if(yp<1) yp=1;
elseif(yp>ylen) yp=ylen; end
dx(i,j)=dx(i,j)+h1(k,l)*fbmparr(xp,yp);
dy(i,j)=dy(i,j)+h2(k,l)*fbmparr(xp,yp);
end
end
end
end
dxy=sqrt(dx.*dx+dy.*dy);
dmx=max(dxy(:));
if(dmx>0) dxy=dxy/dmx; end
[cts,x]=imhist(dxy,64);
hth=min(find(cumsum(cts)>.7*xlen*ylen))/64;
lth=.4*hth;
r=[];
c=[];
for i=2:xlen-1
for j=2:ylen-1
if(((dy(i,j)=0)&&(dx(i,j)>-dy(i,j)))||
((dy(i,j)>=0)&&(dx(i,j)<-dy(i,j))))
delta=abs(dy(i,j)/dx(i,j));
grad1=dxy(i,j+1)*(1-delta)+dxy(i-1,j+1)*delta;
grad2=dxy(i,j-1)*(1-delta)+dxy(i+1,j-1)*delta;
elseif(((dx(i,j)>0)&&(dx(i,j)=-dy(i,j)))||
((dx(i,j)<0)&&(dx(i,j)>=-dy(i,j))))
delta=abs(dx(i,j)/dy(i,j));
grad1=dxy(i-1,j)*(1-delta)+dxy(i-1,j+1)*delta;
grad2=dxy(i+1,j)*(1-delta)+dxy(i+1,j-1)*delta;
elseif(((dx(i,j)=0)&&(dx(i,j)>dy(i,j)))||
((dx(i,j)>=0)&&(dx(i,j)<dy(i,j))))
delta=abs(dx(i,j)/dy(i,j));
grad1=dxy(i-1,j)*(1-delta)+dxy(i-1,j-1)*delta;
grad2=dxy(i+1,j)*(1-delta)+dxy(i+1,j+1)*delta;
elseif(((dy(i,j)<0)&&(dx(i,j)=dy(i,j)))||
((dy(i,j)>0)&&(dx(i,j)>=dy(i,j))))
delta=abs(dy(i,j)/dx(i,j));
grad1=dxy(i,j-1)*(1-delta)+dxy(i-1,j-1)*delta;
grad2=dxy(i,j+1)*(1-delta)+dxy(i+1,j+1)*delta;
end
if((dxy(i,j)>=grad1)&&(dxy(i,j)>=grad2))
if(dxy(i,j)>=hth) bmps(i,j)=1; r=[r;i]; c=[c;j];
elseif(dxy(i,j)>=lth) bmps(i,j)=1; end
end
end
end
bmps=bwselect(bmps,c,r,8);
workarr=double(bmps);
workarr=mat2gray(workarr);
axes(handles.axes6); %«ü©wÅã¥Ü¹Ï¤ùªº¦a¤è
imshow(workarr);
title('¸gCannyºâ¤l³B²z«áªº¹Ï§Î');

% --- Executes on button press in Tclose.


function Tclose_Callback(hObject, eventdata, handles)
% hObject handle to Tclose (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
result=questdlg('½T©wnÃö³¬ ??','Window closing','yes','no','no');
if strcmp(result,'yes')
close
end

Вам также может понравиться