⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 measure.m

📁 matlab处理图像的一些基本方法。其中有一部分mex程序需要安装编译
💻 M
字号:
function measure(command, M)%MEASURE  Get the coordinates of the clicked pixels.%%       MEASURE(COMMAND)%%       The coordinates are stored in the global matrix Coords.%%       COMMAND: START      Begin the session %                MEASURE    Get the coordinates%                DRAW       Draws the lines and text%                EVALUATE   Re-evaluate all the angles and lengths  %                UNDO       Clear the last entry%                CLEAR      Clear all the entrys%                CLOSEIMAGE When the image is going to be closed%                LOAD       Load from file%                SAVE       Save in a file%                DONE       Close the session%%       Claudio  Oct. 17 1995%%% Copyright (c) 1995 by Claudio Rivetti and Mark Young% claudio@alice.uoregon.edu,    mark@alice.uoregon.edu%global I Handlefig fradio mm ed_width B_frame Uibgcolorglobal Coords handleline handletxt SelObj obj global counter Undofun readpath savepath Fname Fcoordextglobal CloseImageFun prev_tlen prev_tang textlen textangglobal autoname ascii prev_autoname prev_asciiif nargin==0  command='START';endZline=max(max(I))+1000;fmode=['   -mat';' -ascii'];ext=['*.mat';'*.txt'];	if ~isimage  setviewmode('TOPVIEW');  showimage;endif isempty(Coords), Coords=[0 0 0 0;NaN NaN NaN NaN]; endif strcmp(upper(command), 'START')	if isempty(prev_tang), prev_tang=1; end	if isempty(prev_tlen), prev_tlen=1; end	if isempty(prev_ascii), prev_ascii=0; end	if isempty(prev_autoname), prev_autoname=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.335 uiwidth+0.04 0.43];	mm_pos = [middle pos(2)+0.71 uiwidth, uithick];	txtc_pos = [middle pos(2)+0.62+uithick uiwidth, uithick];	counter_pos = [middle pos(2)+0.62 uiwidth, uithick];	textlen_pos = [middle pos(2)+0.56 uiwidth, 0.030];	textang_pos = [middle pos(2)+0.52 uiwidth, 0.030];	undo_pos = [middle pos(2)+0.40 uiwidth, uithick];	eval_pos = [middle pos(2)+0.46 uiwidth uithick];	clear_pos = [middle pos(2)+0.35 uiwidth, uithick];	f2pos= [middle-0.02 pos(2)+0.09 uiwidth+0.04 0.225];	load_pos = [middle pos(2)+0.26 uiwidth uithick];	save_pos = [middle pos(2)+0.20 uiwidth uithick];	autoname_pos = [middle pos(2)+0.15 uiwidth 0.030];	ascii_pos = [middle pos(2)+0.11 uiwidth 0.030];		%--------------------------------------------------------------			% CallBacks	measure_cbk='measure(''MEASURE'');';	undo_cbk='measure(''UNDO'');';	clear_cbk='measure(''CLEAR'');'; 	load_cbk='measure(''LOAD'');';	save_cbk='measure(''SAVE'');';	eval_cbk='measure(''EVALUATE'');'; 	whendone='measure(''DONE'');';	Undofun=undo_cbk;  figure(Handlefig);  obj=SelObj;  CloseImageFun='measure(''CLOSEIMAGE'');';  initbuttons('Measure', 'Done', whendone);  if isobj(handleline)    delete(handleline);  end  if any(isobj(handletxt))    delete(handletxt(find(isobj(handletxt))));	handletxt=[];  end    uicontrol(Handlefig, 'Style', 'frame',...		'Units', 'normalized',...		'Position',f1pos,...		'BackgroundColor', Uibgcolor);  mm=uicontrol(Handlefig, 'Style', 'push',...		'Units', 'normalized',...		'position', mm_pos,...		'String', 'Measure',...		'Value', 1,...		'Interruptible','yes',...		'CallBack', measure_cbk);  uicontrol(Handlefig, 'Style', 'text',...		'String', 'Counter',...		'Units', 'normalized',...		'Position',txtc_pos,...		'BackgroundColor', Uibgcolor);  counter=uicontrol(Handlefig, 'Style', 'text',...		'String', int2str(Coords(1,1)),...		'Units', 'normalized',...		'Position',counter_pos,...		'BackgroundColor', 'w');   textlen=uicontrol(Handlefig, 'Style', 'check', ...		'String', 'Lengths', ...		'Units', 'normalized', ...		'value', prev_tlen,...		'Position', textlen_pos,...		'CallBack', 'global prev_tlen;prev_tlen=get(gco, ''value'');');		     textang=uicontrol(Handlefig, 'Style', 'check', ...		'String', 'Angles', ...		'Units', 'normalized', ...		'value', prev_tang,...		'Position', textang_pos,...		'CallBack', 'global prev_tang;prev_tang=get(gco, ''value'');');  uicontrol(Handlefig, 'Style', 'push', ...		'String', 'Re-Evaluate', ...		'Units', 'normalized', ...		'Position', eval_pos, ...		'CallBack', eval_cbk);  uicontrol(Handlefig, 'Style', 'push', ...		'String', 'Undo', ...		'Units', 'normalized', ...		'Position', undo_pos, ...		'CallBack', undo_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', 'Load', ...		'inter', 'yes',...		'Units', 'normalized', ...		'Position', load_pos, ...		'CallBack', load_cbk);   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_autoname,...		'Position', autoname_pos,...		'CallBack', 'global prev_autoname;prev_autoname=get(gco, ''value'');');   ascii=uicontrol(Handlefig, 'Style', 'check', ...		'String', 'Ascii File', ...		'Units', 'normalized', ...		'value', prev_ascii,...		'Position', ascii_pos,...		'CallBack', 'global prev_ascii;prev_ascii=get(gco, ''value'');');   measure('DRAW');end  % STARTif strcmp(upper(command), 'MEASURE')  while 1    [x, y]=getline;    x=x(1:length(x)-1);    y=y(1:length(y)-1);    if length(x)<2, break; end    ax=axis;    if prod(x>=ax(1)) * prod(x<=ax(2)) * prod(y>=ax(3)) * prod(y<=ax(4))      [a,b]=nm2ind(x,y);      z=diag(I(b,a));      C=[zeros(length(x),1) x y z];      A=[];      L=[];      if get(textang, 'value')	ang=langle(x,y);	A=[zeros(length(ang),1)+6 x(2:length(x)-1) y(2:length(y)-1) ang];      end      if get(textlen, 'value')	L=[3 x(length(x)) y(length(y)) llength(x,y)];      end      Coords = [Coords; C; L; A;NaN NaN NaN NaN];      Coords(1,1)=Coords(1,1)+1;	        measure('DRAW', [A;L]);          end  % if inside the image  end    % whileend      % MEASUREif strcmp(upper(command), 'DRAW')  if ~isobj(handleline)    ax=axis;    handleline = line(ax(1),ax(3), 'erasemode','background',...	  'linestyle',getlinestyle,'Color',getpencolor,...      'linewidth', getlinewidth,'visible','on');  end    C = findcoords(Coords, 0);    if ~isempty(C)     set(handleline, 'xdata', C(2:size(C,1),2), 'ydata', C(2:size(C,1),3),...					 'zdata', C(2:size(C,1),4)*0+Zline);  else     set(handleline, 'xdata', [], 'ydata', [], 'zdata',[]);  end  if nargin==1	if any(isobj(handletxt))      delete(handletxt(find(isobj(handletxt)==1)));      handletxt=[];    end    M=[findcoords(Coords,3);findcoords(Coords,6)];	if ~isempty(M)	  M=M(2:size(M,1),:);	  g=find(isnan(M(:,1))==0);	  M=M(g,:);	end  end  if ~isempty(M)    for i=1:size(M,1)	  LAtxt=str2mat(LAtxt, sprintf('%.2f',M(i,4)));	end	LAtxt=LAtxt(2:size(LAtxt,1),:);	    t=text(M(:,2), M(:,3), M(:,2)*0+Zline, LAtxt, 'EraseMode', 'Background',...	   'Color', getpencolor, 'FontName', getfont, 'FontSize', getfontsize);	   if strcmp(getfontstyle, 'Oblique') | strcmp(getfontstyle, 'Italic')	     set(t, 'FontAngle', getfontstyle);	   elseif strcmp(getfontstyle, 'Normal')	     set(t, 'FontWeight', getfontstyle);		 set(t, 'FontAngle', getfontstyle);	   else	     set(t, 'FontWeight', getfontstyle);	   end    handletxt=[handletxt;t];  end  SelObj=[handleline;handletxt];  set(counter, 'string', int2str(Coords(1,1)));end %DRAWif strcmp(upper(command), 'UNDO')  if Coords(1,1) ~= 0;    qq=isnan(Coords(:,1));    n=find(qq==1);    l=length(n);    Coords = [Coords(1:n(l-1),1) Coords(1:n(l-1),2) Coords(1:n(l-1),3) Coords(1:n(l-1),4)];    Coords(1,1)=Coords(1,1)-1;    measure('DRAW');	refresh;  endend  % UNDOif strcmp(upper(command), 'CLEAR')    Coords=[0 0 0 0;NaN NaN NaN NaN];    if any(isobj(handletxt))      delete(handletxt(find(isobj(handletxt)==1)));      handletxt=[];	  refresh;    end    if isobj(handleline)      delete(handleline);	  refresh;    end    SelObj=obj;    set(counter, 'string', int2str(Coords(1,1)));    end  % CLEARif strcmp(upper(command), 'CLOSEIMAGE')    Coords=[];    SelObj=obj;end  % CLOSEIMAGEif strcmp(upper(command), 'DONE')  SelObj=obj;  CloseImageFun='';  if isobj(handleline)    delete(handleline);  end  if any(isobj(handletxt))    delete(handletxt(find(isobj(handletxt))));     handletxt=[];  end  clearstatusbar;  refresh;end  % DONEif strcmp(upper(command), 'LOAD')  if get(autoname, 'value')    fname = [readpath Fname Fcoordext];  else    [name, p] = uigetfile([readpath ext(get(ascii,'value')+1,:)]);    if ~isempty(name) & name ~= 0      fname = [p name];    end  end  if ~isempty(fname)    if ~exist(fname)      alexerror(['  File: ' fname '|        does not exist']);    else      f=watchon;      eval(['load ' fname fmode(get(ascii, 'value')+1,:)]);      if get(ascii, 'value')	p1=max(find(fname=='/'))+1;	if isempty(p1), p1==1; end	p2=min(find(fname(p1:length(fname))=='.'))-1;	if isempty(p2), p2=length(fname); end	eval(['Coords=' fname(p1:p1+p2-1) ';']);      end      statusbar(['X, Y and Z coordinates loaded from ' fname]);      watchoff(f);      measure('DRAW');	  clearstatusbar('WAIT');    end  endend  % LOADif strcmp(upper(command), 'SAVE')  if get(autoname, 'value')    fext=Fcoordext;    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 ' Coords' fmode(get(ascii, 'value')+1,:)]);    statusbar(['X, Y and Z coordinates saved in ' fname]);    watchoff(f);    clearstatusbar('WAIT');  endend  % SAVEif strcmp(upper(command), 'EVALUATE')  C=findcoords(Coords,0);  if isempty(C)    return;  end  n=find(isnan(C(:,1))==1);  l=length(n);  Q=[0 0 0 0;NaN NaN NaN NaN];  isang=find(get(textang, 'value')==1);  islen=find(get(textlen, 'value')==1);    for i=1:l-1      x=C((n(i)+1):n(i+1)-1,2);      y=C((n(i)+1):n(i+1)-1,3);      XYZ=C((n(i)+1):n(i+1)-1,:);	  ang=langle(x,y);	  A=[zeros(length(ang),1)+6 x(2:length(x)-1) y(2:length(y)-1) ang];      A=A*isang;	  L=[3 x(length(x)) y(length(y)) llength(x,y)];      L=L*islen;      Q = [Q; XYZ; L; A;NaN NaN NaN NaN];      Q(1,1)=Q(1,1)+1;  end  if l>=1    Coords=Q;    measure('DRAW');    refresh;  end  end      % EVALUATEreturn

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -