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

📄 ellipse.m

📁 gaussian_model,color based segmentation,draw ellipse may used in face detection
💻 M
字号:
function he=ellipse(varargin)
%ELLIPSE Ellipse Grapics Object Using Line or Patch.
% He=ELLIPSE(Axes,Position,Angle,Type) draws an ellipse
% on the current axes where
% Axes=[Minor Major] is a vector containing the minor and major axes,
% Position=[Xcenter Ycenter] is a vector containing the ellipse center,
% Angle is the angle in degrees of the Major axis with respect to the X axis,
% Type specifies the type of graphics object created either 'line' or 'patch'
% All input arguments are optional, with the default ellipse being a line
% object forming a circle centered at the origin.
%
% He is the handle of the graphics object created.
%
% ELLIPSE(He,'PName','PValue',...) where He is the handle of an ellipse,
% sets ellipse properties according to the property name-value pairs:
% NAME         VALUE
% Axes         [Minor Major]
% Position     [Xcenter Ycenter]
% Angle        [degrees]
% Type         'line' or 'patch' (read only after created)
% Tag          String identifying the created object
%
% ELLIPSE(He,'PName') returns the associated ellipse property.
%
% ELLIPSE(He) returns a structure whose field names are property names
% and whose corresponding values are the property values of the ellipse
% having handle He.
%
% ELIPSE(He,S) where S is a structure having one or more ellipse object
% property names as fieldnames, sets the ellipse properties using the
% associated structure values.
%
% Properties of the line or patch object created can be manipulated by
% using set and get with the returned handle He.
%
% 'axis square' must be applied correct axis distortion.

% D.C. Hanselman, University of Maine, Orono, ME  04469-5708
% Mastering MATLAB 6, Prentice Hall, ISBN 0-13-019468-9
% www.eece.maine.edu/mm

% parse inputs
[he,S,cmd]=local_parse_inputs(varargin{:});
% now do the work based on cmd string
switch cmd
case 'create'
   theta=linspace(0,2*pi,500);
   Xdata=max(S.Axes)*cos(theta);
   Ydata=min(S.Axes)*sin(theta);
   Ydata(end)=0;
   d=S.Angle*pi/180;
   x=cos(d)*Xdata-sin(d)*Ydata + S.Position(1);
   y=sin(d)*Xdata+cos(d)*Ydata + S.Position(2);
   if strcmpi(S.Type,'line')
      he=line(x,y,'Color','k','Tag',S.Tag);
   else
      he=patch('Xdata',x,'Ydata',y,...
         'EdgeColor','k','FaceColor','none',...
         'Tag',S.Tag);
   end
   setappdata(he,'Ellipse',S)
   if ~nargout  % only return handle if asked
      clear he
   end  
case 'getall'
   he=S;
case 'getone'
   % no work, he contains desired property name
case 'set'
   theta=linspace(0,2*pi,500);
   Xdata=max(S.Axes)*cos(theta);
   Ydata=min(S.Axes)*sin(theta);
   Ydata(end)=0;
   d=S.Angle*pi/180;
   x=cos(d)*Xdata-sin(d)*Ydata + S.Position(1);
   y=sin(d)*Xdata+cos(d)*Ydata + S.Position(2);   
   set(he,'Xdata',x,'Ydata',y,'Tag',S.Tag);
   setappdata(he,'Ellipse',S)
   clear he
end
%--------------------------------------------------------
function [he,S,cmd]=local_parse_inputs(varargin)
% figure out how ELLIPSE was called
narg=nargin;
if narg==0
   varargin{1}=[1 1];   % default axes
   narg=1;
end
tmp=varargin{1}(:)';
if isempty(tmp)
   tmp=[1 1];  % default axes if first argument is empty
end
if length(tmp)>1 % ellipse(Axes,Position,Angle,Type)
   S.Axes=tmp;
   if narg>1 & ~isempty(varargin{2})
      S.Position=varargin{2}(:)';
   else
      S.Position=[0 0]; % default center
   end
   if narg>2 & ~isempty(varargin{3})
      S.Angle=varargin{3};
   else
      S.Angle=0;  % default angle
   end
   if narg>3 & ~isempty(varargin{4})
      S.Type=varargin{4};
   else
      S.Type='line'; % default type
   end
   S.Tag='Ellipse';
   error(local_check_inputs(S))
   he=nan;
   cmd='create';
   return
end
he=varargin{1};   % first arument must be an ellipse
if ~ishandle(he)
   error('He Must be a Valid Handle.')
end
S=getappdata(he,'Ellipse');
if isempty(S)
   error('Ellipse Handle Required.')
end
if narg==1                             % Ellipse(He) Get all
   cmd='getall';
   
elseif narg==2 & ischar(varargin{2})   % Ellipse(He,'PName') get one
   pname=local_get_pname(varargin{2});
   he=getfield(S,pname);
   cmd='getone';
   
elseif narg==2 & isstruct(varargin{2}) % Ellipse(He,S) set via structure
   Snew=varargin{2};
   Snames=fieldnames(Snew);
   for i=1:size(Snames,1)
      pname=local_get_pname(Snames{i});
      S=setfield(S,pname,getfield(Snew,Snames{i}));
   end
   error(local_check_inputs(S))
   cmd='set';
   
else  % ELLIPSE(He,'PName','PValue',...) set via names and values
   if rem(narg,2)~=1
      error('Pairs of Property Names and Values Required.')
   end
   Snames=fieldnames(S);
   for i=2:2:narg
      pname=local_get_pname(varargin{i});
      S=setfield(S,pname,varargin{i+1});
   end
   error(local_check_inputs(S))
   cmd='set';
   
end
%--------------------------------------------------------
function pname=local_get_pname(pname)
% get and check property name
PNames={'Axes','Position','Angle','Type','Tag'};
idx=strmatch(lower(pname),lower(PNames));
if isempty(idx)   % no matches
   error(['Unknown Property Name: ' pname])
elseif length(idx)>1 % more than one match
   error(['Property Name Not Unique: ' pname])
elseif idx==4  % type
   warning('''Type'' is a Read Only Property.')
end
pname=PNames{idx};
%--------------------------------------------------------
function msg=local_check_inputs(S)
% check validity of ellipse data
msg='';
if length(S.Axes)~=2 | ~isnumeric(S.Axes)
   msg='Ellipse Axes Must Contain [Minor Major].';
end
if length(S.Position)~=2 | ~isnumeric(S.Position)
   msg='Ellipse Position Must Contain [XCenter YCenter].';
end
if length(S.Angle)~=1 | ~isnumeric(S.Angle)
   msg='Ellipse Angle Must be a Scalar Angle in Degrees';
end
if ~ischar(S.Type)
   msg='Ellipse Type Must be ''line'' or ''patch''.';
end
if ~ischar(S.Tag)
   msg='Ellipse Tag Must be a String.';
end

⌨️ 快捷键说明

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