?? a86.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 + -