📄 image_contour.m
字号:
function image_contour(command)
%IMAGE_CONTOUR Analyze the Image contours.
%
% IMAGE_CONTOUR(COMMAND)
%
% The contours values are stored in the global matrix C.
% See CONTOUR for a description of the contour matrix.
%
% COMMAND: START Begin the session
% CONTOUR Computes the image contour
% FILL Fill the contour polygons
% CLABEL Display the contour labels
% MODE Set the Automatic or Manual mode
% CLEAR Clear the lines, the labels and the C matrix
% SETEDIT Set the edit controls
% SETIMAGE Set the image visible on/off
% SAVE Save the contours in a file
% DONE Close the session
%
% Claudio Oct. 28 1995
%
%
% Copyright (c) 1995 by Claudio Rivetti and Mark Young
% claudio@alice.uoregon.edu, mark@alice.uoregon.edu
%
% Note: The code to draw the contour labels was partially
% copied from the CLABEL (MathWorks) function.
global I Handlefig Handleimg Handleimgax B_frame Uibgcolor SelObj Extramap
global C Chline Chtext obj savepath Fname Fcontourext Chpatch prev_Cfill
global autoname ascii prev_Cautoname prev_Cascii prev_CV prev_CN prev_Cmode
global prev_Clabels Cmode Cedit1 Cedit2 Cmode_txt chk_labels chk_image chk_fill
if nargin==0
command='START';
end
Zline=max(max(I))+1000;
fmode=[' -mat';' -ascii'];
ext=['*.mat';'*.txt'];
Ctext=['Contours Levels';'Contours Number'];
if ~isimage
setviewmode('TOPVIEW');
showimage;
end
if strcmp(upper(command), 'START')
if isempty(prev_Cmode), prev_Cmode=1; end
if isempty(prev_CN), prev_CN=10; end
if isempty(prev_CV), prev_CV=[]; end
if isempty(prev_Cautoname), prev_Cautoname=0; end
if isempty(prev_Cascii), prev_Cascii=0; end
if isempty(prev_Clabels), prev_Clabels=0; end
if isempty(prev_Cfill), prev_Cfill=0; end
% position variables---------------------------------------------------
pos=get(B_frame, 'Position');
uiwidth=0.12;
uithick=0.035;
middle=pos(1)+(pos(3)-uiwidth)/2;
%---------------------------------------------------------------------
% Positions of buttons -----------------------------------------------
f1pos= [middle-0.02 pos(2)+0.22 uiwidth+0.04 0.545];
chk_mode1_pos = [middle pos(2)+0.72 uiwidth, 0.030];
chk_mode2_pos = [middle pos(2)+0.68 uiwidth, 0.030];
Ctxt_pos = [(pos(1)+(pos(3)-0.15)/2) pos(2)+0.60+uithick 0.15, uithick];
Cedit1_pos = [middle pos(2)+0.605 uiwidth, uithick];
Cedit2_pos = [middle pos(2)+0.43 uiwidth, 0.21];
chk_image_pos = [middle pos(2)+0.39 uiwidth, 0.030];
chk_labels_pos = [middle pos(2)+0.36 uiwidth, 0.030];
chk_fill_pos = [middle pos(2)+0.33 uiwidth, 0.030];
exe_pos = [middle pos(2)+0.28 uiwidth, uithick];
clear_pos = [middle pos(2)+0.23 uiwidth, uithick];
f2pos= [middle-0.02 pos(2)+0.07 uiwidth+0.04 0.135];
save_pos = [middle pos(2)+0.16 uiwidth uithick];
autoname_pos = [middle pos(2)+0.115 uiwidth 0.030];
ascii_pos = [middle pos(2)+0.08 uiwidth 0.030];
%--------------------------------------------------------------
% CallBacks
exe_cbk ='image_contour(''CONTOUR'');';
Cmode_cbk ='image_contour(''MODE'');';
clear_cbk='image_contour(''CLEAR'');';
Cedit_cbk='image_contour(''SETEDIT'');';
image_cbk='image_contour(''SETIMAGE'');';
fill_cbk='image_contour(''FILL'');';
save_cbk ='image_contour(''SAVE'');';
whendone ='image_contour(''DONE'');';
figure(Handlefig);
obj=SelObj;
Cmode=[0 0];
initbuttons('Contour Plot', 'Done', whendone);
uicontrol(Handlefig, 'Style', 'frame',...
'Units', 'normalized',...
'Position',f1pos,...
'BackgroundColor', Uibgcolor);
Cmode(1)=uicontrol(Handlefig, 'Style', 'check', ...
'String', 'Automatic', ...
'Horiz', 'left',...
'Units', 'normalized', ...
'value', prev_Cmode,...
'Position', chk_mode1_pos,...
'CallBack', Cmode_cbk);
Cmode(2)=uicontrol(Handlefig, 'Style', 'check', ...
'String', 'Manual', ...
'Horiz', 'left',...
'Units', 'normalized', ...
'value', ~prev_Cmode,...
'Position', chk_mode2_pos,...
'CallBack', Cmode_cbk);
Cmode_txt=uicontrol(Handlefig, 'Style', 'text',...
'String', Ctext(prev_Cmode+1,:),...
'Units', 'normalized',...
'Position',Ctxt_pos,...
'BackgroundColor', Uibgcolor);
Cedit1=uicontrol(Handlefig, 'Style', 'edit',...
'String', prev_CN,...
'Units', 'normalized',...
'Position',Cedit1_pos,...
'Visible', onoff(get(Cmode(1), 'value')),...
'CallBack', Cedit_cbk);
Cedit2=uicontrol(Handlefig, 'Style', 'edit',...
'String', prev_CV,...
'Units', 'normalized',...
'min', 1,...
'max', 20,...
'Position',Cedit2_pos,...
'Visible', onoff(get(Cmode(2), 'value')),...
'CallBack', Cedit_cbk);
chk_image=uicontrol(Handlefig, 'Style', 'check', ...
'String', 'Image', ...
'Units', 'normalized', ...
'value', 1,...
'Horiz', 'left',...
'Position', chk_image_pos,...
'CallBack', image_cbk);
chk_labels=uicontrol(Handlefig, 'Style', 'check', ...
'String', 'Labels', ...
'Units', 'normalized', ...
'Horiz', 'left',...
'value', prev_Clabels,...
'Position', chk_labels_pos,...
'CallBack', image_cbk);
chk_fill=uicontrol(Handlefig, 'Style', 'check', ...
'String', 'Filled', ...
'Units', 'normalized', ...
'Horiz', 'left',...
'value', prev_Cfill,...
'Position', chk_fill_pos,...
'CallBack', fill_cbk);
uicontrol(Handlefig, 'Style', 'push', ...
'String', 'Execute', ...
'Units', 'normalized', ...
'Position', exe_pos, ...
'CallBack', exe_cbk);
uicontrol(Handlefig, 'Style', 'push', ...
'String', 'Clear', ...
'Units', 'normalized', ...
'Position', clear_pos, ...
'CallBack', clear_cbk);
uicontrol(Handlefig, 'Style', 'frame',...
'Units', 'normalized',...
'Position',f2pos,...
'BackgroundColor', Uibgcolor);
uicontrol(Handlefig, 'Style', 'push', ...
'String', 'Save', ...
'Units', 'normalized', ...
'inter', 'yes',...
'Position', save_pos, ...
'CallBack', save_cbk);
autoname=uicontrol(Handlefig, 'Style', 'check', ...
'String', 'Auto Name', ...
'Units', 'normalized', ...
'value', prev_Cautoname,...
'Position', autoname_pos,...
'CallBack', 'global prev_Cautoname;prev_Cautoname=get(gco, ''value'');');
ascii=uicontrol(Handlefig, 'Style', 'check', ...
'String', 'Ascii File', ...
'Units', 'normalized', ...
'value', prev_Cascii,...
'Position', ascii_pos,...
'CallBack', 'global prev_Cascii;prev_Cascii=get(gco, ''value'');');
image_contour('SETEDIT');
end % START
if strcmp(upper(command), 'CONTOUR')
image_contour('CLEAR')
oldfNx=get(Handlefig, 'NextPlot');
set(Handlefig, 'NextPlot', 'add');
oldaNx=get(Handleimgax, 'NextPlot');
set(Handleimgax, 'NextPlot', 'add');
f=watchon;
statusbar('Computing Contours, please wait');
axes(Handleimgax);
[zf, zc]=getzoom(Handleimgax);
zoom(-1000);
ax=axis;
x=ax(1):(ax(2)-ax(1))/(size(I,2)-1):ax(2);
y=ax(3):(ax(4)-ax(3))/(size(I,1)-1):ax(4);
if get(Cmode(1), 'value')
V=get(Cedit1, 'UserData');
else
V=get(Cedit2, 'UserData');
if length(V)==1
V=[V;Zline];
end
end
if isempty(V), V=10;end
[C,Chline]=imcontour(x,y,I,V);
for i=1:length(Chline)
set(Chline(i), 'Zdata', get(Chline(i), 'Zdata')+Zline,...
'LineStyle', getlinestyle, 'LineWidth', getlinewidth, 'color', Extramap(i+1,:));
end
image_contour('CLABEL');
Chpatch=fillc(Chline);
image_contour('FILL');
eval('zoom(zf, zc);');
SelObj=[Chline;Chtext;Chpatch];
if get(Cmode(1), 'value')==0 & isempty(get(Cedit2, 'UserData'))
[mcs, ncs] = size(C);
n=0; k=0; cc=[];
while(1)
k = k + n + 1; if k > ncs, break, end
c = C(1, k); n = C(2, k);
cc=[cc;c];
end
cc=unique(cc);
set(Cedit2, 'string', sprintf('%.5f|',cc));
image_contour('SETEDIT');
end
set(Handlefig, 'NextPlot', oldfNx);
set(Handleimgax, 'NextPlot', oldaNx);
clearstatusbar;
watchoff(f);
end % CONTOUR
if strcmp(upper(command), 'FILL')
set(Chpatch, 'visible', onoff(get(chk_fill, 'value')));
end %FILL
if strcmp(upper(command), 'CLABEL')
[mcs, ncs] = size(C);
% Find range of levels.
crange = 0;
k = 1;
while k <= ncs
crange = max(crange,abs(C(1,k)));
k = k + C(2,k) + 1;
end
n=0;
k=0;
flip=0;
cl=1;
oldc=NaN;
while(1)
k = k + n + 1; if k > ncs, break, end
c = C(1, k); n = C(2, k);
% put next line in in case rand('normal') is set - and to avoid
% obsolescence message
r = rand; while (r > 1 | r < 0) r = rand; end
j = fix(r.* (n - 1)) + 1;
if flip, j = n - j; end
flip = ~flip;
if n == 1 % if there is only one point
xx = C(1, j+k); yy = C(2, j+k);
else
x1 = C(1, j+k); y1 = C(2, j+k);
x2 = C(1, j+k+1); y2 = C(2, j+k+1);
xx = (x1 + x2) ./ 2; yy = (y1 + y2) ./ 2;
end
% Label the point.
% Set tiny labels to zero.
if abs(c) <= 10*eps*crange, c = 0; end
s = sprintf('%0.3g', c);
zz=xx*0+Zline;
if oldc~=c, cl=cl+1;end
oldc=c;
t=text(xx, yy, zz, s, 'verticalalignment', 'bottom', 'horizontalalignment',...
'left', 'Clipping', 'on', 'color', Extramap(cl,:));
Chtext=[Chtext;t];
end %while
set(Chtext, 'FontName', getfont, 'FontSize', getfontsize, 'visible', onoff(get(chk_labels, 'value')));
if strcmp(getfontstyle, 'Oblique') | strcmp(getfontstyle, 'Italic')
set(Chtext, 'FontAngle', getfontstyle);
elseif strcmp(getfontstyle, 'Normal')
set(Chtext, 'FontWeight', getfontstyle);
set(Chtext, 'FontAngle', getfontstyle);
else
set(Chtext, 'FontWeight', getfontstyle);
end
end %CLABEL
if strcmp(upper(command), 'MODE')
set(Cmode(find(Cmode ~= gco)), 'value', 0);
set(gco, 'value', 1);
prev_Cmode=get(Cmode(1), 'value');
set(Cmode_txt, 'string', Ctext(prev_Cmode+1,:));
set(Cedit1, 'visible', onoff(prev_Cmode));
set(Cedit2, 'visible', onoff(~prev_Cmode));
end % MODE
if strcmp(upper(command), 'SETEDIT')
num=round(str2num(get(Cedit1, 'string')));
if isempty(num), num=10;end
num=num+(num>10)*(10-num);
num=num-(num<1)*(num-1);
prev_CN=int2str(num);
set(Cedit1, 'string', prev_CN, 'UserData', num);
num=[];
s=get(Cedit2, 'string');
for i=1:size(s,1);
num=[num;sscanf(s(i,:), '%f')];
end
if length(num)>10, num=num(1:10);end
num=sort(num);
prev_CV=sprintf('%.5f|',num);
set(Cedit2, 'string', prev_CV, 'UserData', num);
end % SETEDIT
if strcmp(upper(command), 'SETIMAGE')
set(Handleimg, 'visible', onoff(get(chk_image, 'value')));
if any(isobj(Chtext))
set(Chtext(find(isobj(Chtext)==1)), 'visible', onoff(get(chk_labels, 'value')));
end
prev_Clabels=get(chk_labels, 'value');
end % SETIMAGE
if strcmp(upper(command), 'CLEAR')
statusbar('Deleting lines, labels and patches...');
C=[];
r=0;
if any(isobj(Chtext))
delete(Chtext(find(isobj(Chtext)==1)));
Chtext=[];
r=1;
end
if any(isobj(Chline))
delete(Chline(find(isobj(Chline)==1)));
Chline=[];
r=1;
end
if any(isobj(Chpatch))
delete(Chpatch(find(isobj(Chpatch)==1)));
Chpatch=[];
r=1;
end
SelObj=obj;
if r, refresh;end
clearstatusbar;
end % CLEAR
if strcmp(upper(command), 'DONE')
image_contour('CLEAR');
set(Handleimg, 'visible', 'on');
clearstatusbar;
end % DONE
if strcmp(upper(command), 'SAVE')
if get(autoname, 'value')
fext=Fcontourext;
if ~isempty(findstr(fext,'.mat')) & get(ascii,'value')==1
fext=strrep(fext, '.mat', '.txt');
end
fname = [savepath Fname fext];
else
[name, p] = uiputfile([savepath ext(get(ascii, 'value')+1,:)]);
if ~isempty(name) & name ~= 0
fname = [p name];
end
end
if ~isempty(fname)
f=watchon;
eval(['save ' fname ' C ' fmode(get(ascii, 'value')+1,:)]);
statusbar(['Contours saved in ' fname]);
watchoff(f);
clearstatusbar('WAIT');
end
end % SAVE
return
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -