function EdgeDetection(action, varargin)
% 基于差分运算的数字图像边缘检测软件主要函数说明
% InitializeEdgeDetection:检测程序相关功能初始化.
% ComputeEdgeMap :计算原始图像边缘
% SelectMethod :选择边缘检测方法
% LoadNewImage :读入被选择的图像
% UpdateThreshCtrl :数据更新
% UpdateLOGSize - :更新与LOG滤波器相关的数据
if nargin<1,
action='InitializeEdgeDetection';
end;
feval(action,varargin{:});
return;
%%% 函数InitializeEdgeDetection
function InitializeEdgeDetection()
h = findobj(allchild(0), 'tag', 'EdgeDetectionSoft');
if ~isempty(h)
figure(h(1))
return
end
screenD = get(0, 'ScreenDepth');
if screenD>8
grayres=256;
else
grayres=128;
end
EdgeDetectionFig = figure( ...
'Name','基于差分运算的数字图像边缘检测软件', ...
'NumberTitle','off', 'HandleVisibility', 'on', ...
'tag', '基于差分运算的数字图像边缘检测软件', ...
'Visible','off', 'Resize', 'off',...
'BusyAction','Queue','Interruptible','off', ...
'Color', [.8 .8 .8], ...
'IntegerHandle', 'off', ...
'DoubleBuffer', 'on', ...
'Colormap', gray(grayres));
figpos = get(EdgeDetectionFig, 'position');
% 调节图像窗口尺寸
figpos(3:4) = [560 420];
horizDecorations = 10; % resize controls, etc.
vertDecorations = 45; % title bar, etc.
screenSize = get(0,'ScreenSize');
if (screenSize(3) <= 1)
% No display connected (apparently)
screenSize(3:4) = [100000 100000];
end
if (((figpos(3) + horizDecorations) > screenSize(3)) | ...
((figpos(4) + vertDecorations) > screenSize(4)))
% 窗口尺寸太小
delete(EdgeDetectionFig);
error(['Screen resolution is too low ', ...
'(or text fonts are too big) to run this edgesoft']);
end
dx = screenSize(3) - figpos(1) - figpos(3) - horizDecorations;
dy = screenSize(4) - figpos(2) - figpos(4) - vertDecorations;
if (dx < 0)
figpos(1) = max(5,figpos(1) + dx);
end
if (dy < 0)
figpos(2) = max(5,figpos(2) + dy);
end
set(EdgeDetectionFig, 'position', figpos);
rows = figpos(4); cols = figpos(3);
hs = (cols-512) / 3; % 垂直间距
bot = rows-2*hs-256; % 图像底部
% 按钮和菜单参数
ifs = hs/2;
Std.Interruptible = 'off';
Std.BusyAction = 'queue';
% 原始图像轴线
hdl.ImageAxes = axes(Std, ...
'Units', 'Pixels', ...
'Parent',EdgeDetectionFig,...
'ydir', 'reverse', ...
'XLim', [.5 256.5], ...
'YLim', [.5 256.5],...
'CLim', [0 255], ...
'Position',[hs bot 256 256], ...
'XTick',[],'YTick',[]);
set(get(hdl.ImageAxes, 'title'), 'string', '原始图像');
% 边缘图像轴线
hdl.EdgeAxes = axes(Std, ...
'Units', 'Pixels', ...
'Parent',EdgeDetectionFig,...
'ydir', 'reverse', ...
'XLim', [.5 256.5], ...
'YLim', [.5 256.5],...
'CLim', [0 1], ...
'Position',[cols-hs-256 bot 256 256], ...
'XTick',[],'YTick',[]);
set(get(hdl.EdgeAxes, 'title'), 'string', '边缘图像');
% 原始图像
hdl.Image = image(Std, ...
'CData', [], ...
'CDataMapping', 'scaled', ...
'Parent',hdl.ImageAxes,...
'Xdata', [1 256],...
'Ydata', [1 256],...
'EraseMode', 'none');
% 边缘图像
hdl.Edge = image(Std, ...
'CData', [], ...
'CDataMapping', 'scaled', ...
'Parent',hdl.EdgeAxes,...
'Xdata', [1 256],...
'Ydata', [1 256],...
'EraseMode', 'none');
% Background color for frames
bgcolor = [0.45 0.45 0.45];
fgcolor = [1 1 1]; % For text
% 菜单框架
mfleft=hs;
mfbot=hs;
mfwid=(3*cols/8)-1.5*hs; % 2*cols/7
mfht=bot-2*hs;
hdl.MenuFrame = uicontrol(Std, ...
'Parent', EdgeDetectionFig, ...
'Style', 'frame', ...
'Units', 'pixels', ...
'Position', [mfleft mfbot mfwid mfht], ...
'BackgroundColor', bgcolor);
% 图像下拉菜单
ipwid = mfwid-2*ifs;
ipht = 21;
ipleft = mfleft+ifs;
ipbot = mfbot+1.7*ifs + 2*ipht;
hdl.ImgPop=uicontrol(Std, ...
'Parent', EdgeDetectionFig, ...
'Style','popupmenu', ...
'Units','pixels', ...
'Position',[ipleft ipbot ipwid ipht], ...
'Enable','on', ...
'String','米粒|土星|钱币1|电路|轮盘|飞船|钱币2|玻璃', ...
'Tag','ImagesPop',...
'Callback','EdgeDetection(''LoadNewImage'')');
% 图像菜单标签
uicontrol( Std, ...
'Parent', EdgeDetectionFig, ...
'Style','text', ...
'Units','pixels', ...
'Position',[ipleft ipbot+ipht ipwid 18], ...
'Horiz','left', ...
'Background',bgcolor, ...
'Foreground',fgcolor, ...
'String','选择原始图像:');
% 算子选择菜单
hdl.Method = 'Sobel';
mpwid = ipwid;
mpht = ipht;
mpleft = ipleft;
mpbot = mfbot+1.2*ifs;
hdl.MethodPop=uicontrol(Std, ...
'Parent', EdgeDetectionFig, ...
'Style','popupmenu', ...
'Units','pixels', ...
'Position',[mpleft mpbot mpwid mpht], ...
'Enable','on', ...
'String','Sobel|Prewitt|Roberts|Laplacian of Gaussian|Canny', ...
'Tag','MethodPop',...
'Callback','EdgeDetection(''SelectMethod'')');
% 算子选择菜单标签文本
uicontrol( Std, ...
'Parent', EdgeDetectionFig, ...
'Style','text', ...
'Units','pixels', ...
'Position',[mpleft mpbot+mpht mpwid 18], ...
'Horiz','left', ...
'Background',bgcolor, ...
'Foreground',fgcolor, ...
'String','选择边缘检测算子:');
% 框架参数
pfleft =(3*cols/8)+0.5*hs;
pfbot = 1.5*hs;
pfwid =(3*cols/8)-hs;
pfht = bot-2.5*hs;
hdl.ParamFrame = uicontrol(Std, ...
'Parent', EdgeDetectionFig, ...
'Style', 'frame', ...
'Units', 'pixels', ...
'Position', [ pfleft pfbot pfwid pfht ], ...
'BackgroundColor', bgcolor);
% 域值控制文本标签
labelleft = pfleft+ifs;
labelwid = pfwid/2-hs;
labelbot = pfbot+2*pfht/3;
hdl.sprThLbl = uicontrol(Std,...
'Parent', EdgeDetectionFig, ...
'Style','text', ...
'Units','pixels', ...
'Position',[labelleft labelbot labelwid 18], ...
'Horiz','left', ...
'String','域 值:', ...
'BackgroundColor',bgcolor, ...
'ForegroundColor',fgcolor);
hdl.Threshold = 0; % Initial value
raleft = pfleft + pfwid/2 - hs/2;
rabot = pfbot+2*pfht/3+hs/6;
rawid = pfwid/2;
raht = ipht;
hdl.RadioAutomatic=uicontrol(Std, ...
'Parent', EdgeDetectionFig, ...
'Style','radiobutton', ...
'Units','pixels', ...
'Position',[raleft rabot rawid raht], ...
'String','自动匹配', ...
'value',1,'Userdata',1, ...
'Callback','EdgeDetection(''Radio'',''auto'')');
rmleft = pfleft + pfwid/2 - hs/2;
rmbot = pfbot+pfht/3+hs/3;
rmwid = hs*1.5;
rmht = ipht;
hdl.RadioManual=uicontrol(Std, ...
'Parent', EdgeDetectionFig, ...
'Style','radiobutton', ...
'Units','pixels', ...
'Position',[rmleft rmbot rmwid rmht], ...
'String','', ...
'value',0,'Userdata',0, ...
'Callback','EdgeDetection(''Radio'',''manual'')');
thleft = rmleft+rmwid;
thwid = rawid-rmwid;
thbot = rmbot;
thht = rmht;
hdl.ThreshCtrl = uicontrol(Std, ...
'Parent', EdgeDetectionFig, ...
'Enable', 'off', ...
'Style','edit', ...
'Units','pixels', ...
'Position',[thleft thbot thwid thht], ...
'Horiz','right', ...
'Background','white', ...
'Foreground','black', ...
'String','0',...
'callback','EdgeDetection(''UpdateSprThresh'')');
% 算子方向选择菜单
dpwid = pfwid/2;
dpht = ipht;
dpleft = pfleft + pfwid/2 - hs/2;
dpbot = pfbot+.4*hs;
hdl.sprDirPop=uicontrol(Std, ...
'Parent', EdgeDetectionFig, ...
'Style','popupmenu', ...
'Units','pixels', ...
'Position',[dpleft dpbot dpwid dpht], ...
'Enable','on', ...
'String','水平垂直|水平方向|垂直方向', ...
'Tag','DirectionPop',...
'Callback','EdgeDetection(''UpdateDirectionality'')');
% 算子方向选择文本标签
labelleft = pfleft+ifs;
labelwid = pfwid/2-hs;
labelbot = dpbot;
hdl.sprDirLbl = uicontrol( Std, ...
'Parent', EdgeDetectionFig, ...
'Style','text', ...
'Units','pixels', ...
'Position',[labelleft labelbot labelwid 18], ...
'Horiz','left', ...
'Background',bgcolor, ...
'Foreground',fgcolor, ...
'S