📄 example12_2.m
字号:
function edgedemo(action, varargin)
% 子函数:
% InitializeEDGEDEMO - 初始化图像、控件、轴
% ComputeEdgeMap - 调用edge.m计算原始图像的边缘图
% SelectMethod - 选取边缘检测算法,并使相应的控件有效或无效
% LoadNewImage - 读入选取的图像
% UpdateThreshCtrl - 从编辑框中获取门限值,并使【Apply】按钮有效
% UpdateDirectionality - 根据弹出菜单中的内容显示指示字符串
% Radio - 设置Radio Buttons中的值,并使得门限编辑框有效或无效
% UpdateLOGSize - 从编辑框中获取LOG过滤器的大小
% UpdateLOGSigma - 从编辑框中获取LOG过滤器的Sigma值
% ActivateSPRControls - 打开Sobel, Prewitt, Roberts方法中用到的控件
% ActivateLOGControls - 打开LOG方法中用到的控件
if nargin<1,
action='InitializeEDGEDEMO';
end;
feval(action,varargin{:});
%注意,这里没有用前面推荐的程序结构。而是巧用feval函数来调用回调函数相应
%用户的界面操作,这也是一种比较常用的程序结构,往往用在回调函数比较复杂
%的情况下。
return;
%%% Sub-function - InitializeEDGEDEMO
function InitializeEDGEDEMO()
% 如果程序已经运行,将它放到前台
h = findobj(allchild(0), 'tag', 'Edge Detection Demo');
% 0代表根对象,在整个对象树中处于最顶层
if ~isempty(h)
figure(h(1))
return
end
screenD = get(0, 'ScreenDepth');
if screenD>8
grayres=256;
else
grayres=128;
end
%下面的代码主要完成对界面对象的描述,包括大小、位置及其属性
%由于篇幅所限,这里只给出Figure的设置
EdgeDemoFig = figure( ...
'Name','Edge Detection Demo', ...
'NumberTitle','off', 'HandleVisibility', 'on', ...
'tag', 'Edge Detection Demo', ...
'Visible','off', 'Resize', 'off',...
'BusyAction','Queue','Interruptible','off', ...
'Color', [.8 .8 .8], ...
'IntegerHandle', 'off', ...
'DoubleBuffer', 'on', ...
'Colormap', gray(grayres));
figpos = get(EdgeDemoFig, 'position');
% 调整Figure窗口的大小
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]; % don't use Inf because of vms
end
if (((figpos(3) + horizDecorations) > screenSize(3)) | ...
((figpos(4) + vertDecorations) > screenSize(4)))
% Screen size is too small for this demo!
delete(EdgeDemoFig);
error(['Screen resolution is too low ', ...
'(or text fonts are too big) to run this demo']);
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(EdgeDemoFig, 'position', figpos);
rows = figpos(4); cols = figpos(3);
hs = (cols-512) / 3; % Horizantal Spacing
bot = rows-2*hs-256; % Bottom of the images
%====================================
% 接下来是控件和菜单的设置,省略
……
%====================================
set(EdgeDemoFig, 'Userdata', hdl, 'Visible', 'on');
%初始化中创建的所有对象的句柄都放入hdl中,然后又将hdl放入图形Figure
%的“Userdata”,由此实现了句柄的传递。这是一种很好的共享数据的方法。请读者
%注意,
drawnow
LoadNewImage(EdgeDemoFig);
drawnow
set(EdgeDemoFig, 'HandleVisibility', 'Callback');
set([hdl.Apply hdl.Help hdl.Close] , 'Enable', 'on');
return
%%%
%%% Sub-Function - ComputeEdgeMap
%%%
function ComputeEdgeMap(DemoFig)
if nargin<1
callb = 1;
DemoFig = gcbf;
else
callb = 0;
end
set(DemoFig,'Pointer','watch');
setstatus(DemoFig, 'Computing the edge map...');
hdl=get(DemoFig,'Userdata');
img = getimage(hdl.Image);
autothresh = get(hdl.RadioAutomatic, 'Value');
switch hdl.Method
case {'Sobel','Roberts','Prewitt'}
if autothresh
[edgemap,thresh]=edge(img,hdl.Method, dl.Directionality);
setstatus(['The threshold is ' num2str(thresh) '.']);
else
edgemap = edge(img, ...
hdl.Method, hdl.Threshold, hdl.Directionality);
setstatus(DemoFig, '');
end
case 'Laplacian of Gaussian'
if autothresh
[edgemap,thresh] = edge(img, 'log', [], hdl.LogSigma);
setstatus(DemoFig, ['The threshold is ' num2str(thresh) '.']);
else
edgemap = edge(img, 'log', hdl.Threshold, hdl.LogSigma);
setstatus(DemoFig, '');
end
case 'Canny'
if autothresh
[edgemap,thresh] = edge(img, 'canny', [], hdl.LogSigma);
setstatus(DemoFig, ['High threshold is ' num2str(thresh(2)) '.']);
else
[edgemap,thresh] = edge(img, 'canny', hdl.Threshold, hdl.LogSigma);
setstatus(DemoFig, '');
end
otherwise
error('EDGEDEMO: Invalid edge detection method.');
end
set(hdl.Edge, 'CData', edgemap);
set(hdl.Apply, 'Enable', 'off');
set(DemoFig,'Pointer','arrow');
drawnow
%%%
%%% Sub-Function - SelectMethod
%%%
function SelectMethod
DemoFig = gcbf;
hdl = get(DemoFig, 'userdata');
v = get(hdl.MethodPop,{'value','String'});
hdl.Method = deblank(v{2}(v{1},:));
switch hdl.Method
case {'Sobel','Prewitt'}
ActivateSPRControls(DemoFig);
set(hdl.sprDirPop, 'Enable', 'on');
case 'Laplacian of Gaussian'
ActivateLOGControls(DemoFig);
set(hdl.logSigmaCtrl, 'String', '2');
hdl.LogSigma = 2;
case 'Canny'
ActivateLOGControls(DemoFig);
set(hdl.logSigmaCtrl, 'String', '1');
hdl.LogSigma = 1;
case 'Roberts'
ActivateSPRControls(DemoFig);
set(hdl.sprDirPop, 'Enable', 'off', 'value', 1);
otherwise
error('EDGEDEMO: invalid method specifier.');
end
set(hdl.Apply, 'Enable', 'on');
set(DemoFig, 'userdata', hdl);
% BlankOutEdgeMap(DemoFig);
setstatus(DemoFig, ['Press ''Apply'' to compute edges.']);
%%%
%%% Sub-Function - LoadNewImage
%%%
function LoadNewImage(DemoFig)
if nargin<1
callb = 1; % We're in a callback
DemoFig = gcbf;
else
callb = 0; % We're in the initialization
end
set(DemoFig,'Pointer','watch');
%由于图像加载时间可能比较长,在加载前把鼠标的形状改成沙漏形
hdl=get(DemoFig,'Userdata');
v = get(hdl.ImgPop,{'value','String'});
name = deblank(v{2}(v{1},:));
setstatus(DemoFig, ['Loading the ' name ' image...']);
drawnow
switch name
case 'Aluminum',
alumgrns2 = []; % Parser hint
load imdemos alumgrns2
%注意这里图像是放在工作空间imdemos中,所以使用了load。否则,需要使用imread
%的函数。具体使用请参照前面的介绍
img = alumgrns2;
case 'Blood',
blood2 = [];
load imdemos blood2
img = blood2;
case 'Rice',
rice3 = [];
load imdemos rice3
img = rice3;
case 'Saturn',
saturn2 = [];
load imdemos saturn2
img = saturn2;
case 'Eight Bit',
eight = [];
load imdemos eight
img = eight;
case 'Circuit',
circuit4 = [];
load imdemos circuit4
img = circuit4;
case 'Vertigo',
vertigo2 = [];
load imdemos vertigo2
img = vertigo2;
case 'Bone Marrow',
bonemarr2 = [];
load imdemos bonemarr2
img = bonemarr2;
otherwise
error('EDGEDEMO: Unknown Image Option!');
end
set(hdl.Image, 'Cdata', img);
set(get(hdl.ImageAxes,'title'),'string',['Original ' name ' Image']);
set(DemoFig,'Pointer','arrow');
if callb
set(hdl.Apply, 'Enable', 'on');
end
drawnow
if ~strcmp(hdl.Method, 'Laplacian of Gaussian')
if get(hdl.RadioAutomatic, 'Value')==0
Radio('auto',DemoFig);
end
end
ComputeEdgeMap(DemoFig);
return;
%%%
%%% Sub-function - UpdateThreshCtrl
%%%
function UpdateSprThresh()
DemoFig = gcbf;
hdl = get(DemoFig, 'UserData');
v = hdl.Threshold;
s = get(hdl.ThreshCtrl,'String');
vv = real(evalin('base',['[' s ']'],num2str(v)));
% Validate the threshold which was passed in
if isempty(vv) | ~isreal(vv) | vv(1)<0
vv = v;
set(gcbo,'String',num2str(vv));
return
end
vv = round(vv(1)*1000000)/1000000;
set(gcbo,'String',num2str(vv));
hdl.Threshold = vv;
set(hdl.Apply, 'Enable', 'on');
setstatus(DemoFig, 'Press ''Apply'' to compute edges.');
set(DemoFig, 'UserData', hdl);
return
%%%
%%% Sub-function - UpdateDirectionality
%%%
function UpdateDirectionality()
DemoFig = gcbf;
hdl = get(DemoFig, 'UserData');
v = get(hdl.sprDirPop,{'value','String'});
dir = deblank(v{2}(v{1},:));
set(hdl.sprDirPop, 'userdata', dir);
hdl.Directionality = dir;
set(hdl.Apply, 'Enable', 'on');
setstatus(DemoFig, 'Press ''Apply'' to compute edges.');
set(DemoFig, 'UserData', hdl);
return
%%%
%%% Sub-function - Radio
%%%
unction Radio(control, DemoFig)
if nargin<2
DemoFig = gcbf;
end
hdl = get(DemoFig, 'UserData');
if strcmp(control, 'auto')
set(hdl.RadioAutomatic, 'Value', 1);
set(hdl.RadioManual, 'Value', 0);
set(hdl.ThreshCtrl, 'Enable', 'off');
set(hdl.Apply, 'Enable', 'on');
setstatus(DemoFig, 'Press ''Apply'' to compute edges.');
elseif strcmp(control, 'manual')
set(hdl.RadioAutomatic, 'Value', 0);
set(hdl.RadioManual, 'Value', 1);
set(hdl.ThreshCtrl, 'Enable', 'on');
set(hdl.Apply, 'Enable', 'on');
setstatus(DemoFig, 'Press ''Apply'' to compute edges.');
end
return
%%%
%%% Sub-function - UpdateLOGSigma
%%%当用户输入SIGMA值后,一方面需要验证取值的有效性,
%%%另一方面需要更新界面
function UpdateLOGSigma()
DemoFig = gcbf;
hdl = get(DemoFig, 'UserData');
v = hdl.LogSigma;
s = get(hdl.logSigmaCtrl,'String');
vv = real(evalin('base',s,num2str(v)));
if isempty(vv) | ~isreal(vv) | vv(1)<0
% 验证输入的SIGMA值是否有效
vv = v;
set(hdl.logSigmaCtrl,'String',num2str(vv));
return
end
vv = round(vv(1)*100)/100;
set(hdl.logSigmaCtrl,'String',num2str(vv));
hdl.LogSigma = vv;
set(hdl.Apply, 'Enable', 'on');
setstatus(DemoFig, 'Press ''Apply'' to compute edges.');
set(DemoFig, 'UserData', hdl);
return
%下面两个子程序都是通过改变控件的可见属性来完成界面的切换
%%%
%%% Sub-function - ActivateSPRControls
%%%该子程序激活SPR三种算法使用的公共控件
function ActivateSPRControls(DemoFig)
hdl = get(DemoFig, 'UserData');
set([hdl.sprDirPop hdl.sprDirLbl], 'Visible', 'on');
set([hdl.logSigmaCtrl hdl.logSigmaLbl], 'Visible', 'off');
%%%
%%%Sub-function – ActivateLOGControls
%%%该子程序激活LOG算法中使用的控件
function ActivateLOGControls(DemoFig)
hdl = get(DemoFig, 'UserData');
set([hdl.logSigmaCtrl hdl.logSigmaLbl],'Visible', 'on');
set([hdl.sprDirPop hdl.sprDirLbl], 'Visible', 'off');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -