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

📄 asm.m

📁 cootes提出的ASM算法的matlab实现。在cootes用于课堂使用的源代码的基础上改写。改正了原来代码中的一些错误
💻 M
字号:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Based on (unless otherwise stated):%%%%%% Active Contour Models--Their Training & Application%%%     T.F.Cootes, C.J.Taylor, et.al.%%%        Computer Vision and Image Understanding,%%%        Vol.61 No.1,January pp.38-59, 1995%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  and for the multiresolution part:%%% %%%  Active Shape Models: Evaluation of a Multi-Resolution Method%%%  for Improving Image Search%%%%%%  T.F.Cootes, C.J.Taylor, A.Lanitis%%%  Proc. British Machine Vision Conference, vol. 1 ,%%%  1994, Ed.E.Hancock   BMVA Press.  pp327-336%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Programming done by:%%% Ghassan Hamarneh%%% Image Analysis Group%%% CHALMERS UNIVERSITY OF TECHNOLGY%%% Feng Xuetao%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Using uiwait and helpdlg%uiwait(helpdlg({'A C T I V E     S H A P E      M O D E L S' ...%      '        with  Multi-Resolution statistics ' ''...%      'By ... ' '' 'Ghassan Hamarneh' '' ['done: 11-Aug-1997 -- today: ...',date] },'ASM'));clear;clc;%help information% uiwait(helpdlg({'HELP INFORMATION:',...%       '',...%       '  .I. This Program is divided into 3 main stages:',...%       '        you can choose to skip any one of them',...%       '        1 TRAINING        ',...%       '        2 TRYING WEIGHTS  ',...%       '        3 APPLICATION     ',...%       '',...%       ' .II. Follow the instructions in the dialog boxes.',...%       '',...%       '.III. Click Cancel in any dialog box to quit.',...%       '',...%    },'ASM'));   ButtonName=questdlg('Do you want to train?','ASM');DidTrain=ButtonName;if(strcmp(ButtonName,'Cancel'))   msgbox('Terminating: cancelled request to train.','ASM');   return; elseif  (strcmp(ButtonName,'Yes'))   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%   %%     TRAINING STAGE      %%   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%      %Number of shapes in training set = NumTrnSetImgs   %Number of landmark points = NumLandMarkPts   %Number of dimensions = D = 2%   uiwait(helpdlg('THIS IS THE FIRST STAGE: TRAINING STAGE.','ASM'));   %   uiwait(helpdlg('Now, you will enter some initialization information used for training.','ASM'));   %Using the inputdlg   prompt={'Enter Contours Ending Points. (Last entry is no. of landmarks, ex. [8 16 27])',...         'Enter Percentage of explained variance (0-1):',...         'Enter Number of points along training profile above landmark (1,2,3,...):',...         'Enter Number of points along training profile below landmark (1,2,3,...):'};   def={'[24 48]','0.90','5','5'};   TheTitle='ASM';   lineNo=[1,1,1,1];   answer=inputdlg(prompt,TheTitle,lineNo,def,'on');   if isempty(answer) msgbox('Terminating: cancelled inputing initialization info. for training','ASM'); return; end;      ContoursEndingPoints=str2num(answer{1});   ExplainPercent= str2num(answer{2});   TrnPntsAbove  = str2num(answer{3});   TrnPntsBelow  = str2num(answer{4});         NumLandMarkPts=ContoursEndingPoints(end);         %STEP 1 -----------------   %Obtain landmark coordinates for each shape in the training set   %Result: 'unaligined training set shape coordinates' matrix Xu with rows=2*NumLandMarkPts & cols=NumTrnSetImgs      ButtonMark=questdlg('标点?  Yes:对样本进行标点  No:读入已标点文件(前面输入的ContoursEndingPoints被取代)','ASM');   if(strcmp(ButtonMark,'Cancel'))      msgbox('Terminating: 训练过程中断','ASM');      return;    elseif  (strcmp(ButtonMark,'Yes'))     % 对样本进行标点                    [Xu,TrnImgFiles]=GetTrnSetCoor(NumLandMarkPts,ContoursEndingPoints);       NumTrnSetImgs=length(TrnImgFiles);       if NumTrnSetImgs==0            msgbox('no bmp file'); return;       end       if Xu==0  msgbox('Terminating: cancelled loading images','ASM'); return; end          elseif (strcmp(ButtonMark,'No'))    % 读入已标点文件(前面输入的ContoursEndingPoints被取代)      clear ContoursEndingPoints NumLandMarkPts;      [fname,pname]=uigetfile('*.mat','读入已标点文件');       if fname==0; msgbox('Terminating: cancelled loading data (.mat) file','ASM'); return; end	  load([pname,fname]);      if ~(exist('ContoursEndingPoints') & exist('NumLandMarkPts') & exist('Xu') & exist('TrnImgFiles'))          msgbox('选择了错误的标点文件');          return;      end   end           figure   PlotShapes(Xu,'ASM: unaligined training set',ContoursEndingPoints);         %STEP 1.1 -----------------   %Calculate the Mean Normalized Derivative Profile for each landmark point   %RESULT: a 2D array of NumLandMarkPts Profiles   %Calculate the covariance matrix of the Mean Normalized Derivative Profiles for each landmark   %RESULT: a cell array of squares arrays of length (1+TrnPntsAbove+TrnPntsBelow)-1      MaxNumPyramidLevels=GetMaxNumPyramidLevels(TrnImgFiles);      uiwait(msgbox(['Using ',num2str(MaxNumPyramidLevels),' levels in image pyramid'],'ASM'));            [MnNrmDrvProfiles,ProfilesCov]=GetProfileStatistics(TrnImgFiles,Xu,TrnPntsAbove,TrnPntsBelow,ContoursEndingPoints,MaxNumPyramidLevels);         %STEP 2 -----------------   %Find the weighting matrix to give more significance to those points which tend to be stable   %RESULT: 'landmark points weighting matrix' square matrix W with rows=cols=NumLandMarkPts      uiwait(helpdlg({'Now, the weighting matrix will be calculated,'...         'the shapes will be aligned and' ...         'the training set statistics will be obtained.'},'ASM'));   W=GetWeights(Xu);         %STEP 3 -----------------   %Align the shapes of the training set using the obtained coordinates   %RESULT: 'aligned training set shape coordinates' matrix Xa         Xa=AlignTrnSetCoor(Xu,W,ContoursEndingPoints);   figure   PlotShapes(Xa,'ASM: aligined training set',ContoursEndingPoints);            %STEP 4 -----------------   %Obtaining the statistical description of the training set shape coordinates   %REUSLT: 'Statistical Model x=xm + P*b'  where,   %           xm: coordinates of mean shape -> 2*NumLandMarkPts x 1   %            P: matrix of first t eigenvectors of covariance matrix -> 2*NumLandMarkPts x t   %            b: vector of weights -> t x 1      [MeanShape,tEigenvectors,tEigenValues]=GetShapeStatistics(Xa,ExplainPercent);            ButtonName=questdlg('Do you want to save training results','ASM');   if(strcmp(ButtonName,'Cancel'))      msgbox('Terminating: cancelled request to save training results','ASM');      return;    elseif  (strcmp(ButtonName,'Yes'))            [newmatfile, newpath] = uiputfile('*.mat', 'ASM: Save as...');      if(newmatfile==0)msgbox('Terminating: cancelled entering .mat file name for save','ASM'); return; end      save([newpath,newmatfile],'MeanShape','tEigenvectors','tEigenValues','W','ContoursEndingPoints',...         'MnNrmDrvProfiles','ProfilesCov','TrnPntsBelow','TrnPntsAbove','MaxNumPyramidLevels');   endendif (~strcmp(DidTrain,'Yes'))  %if did not do training   uiwait(helpdlg('Since you chose no training, you will have to load a previous data (.mat) file.','ASM'));   [fname,pname]=uigetfile('*.mat','ASM: choose *.mat file to load');   if fname==0; msgbox('Terminating: cancelled loading data (.mat) file','ASM'); return; end      load([pname,fname])   end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    TRY WEIGHTS          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%ButtonName=questdlg('Do you want to try weights?','ASM');if(strcmp(ButtonName,'Cancel'))   msgbox('Terminating: cancelled request to try weights.','ASM');   return; elseif  (strcmp(ButtonName,'Yes'))   %trying different weights b in X=M+Pb%   uiwait(helpdlg('THIS IS THE SECOND STAGE: TRYING WEIGHTS.','ASM'));%   uiwait(helpdlg('Now, you will enter different weight values for b in X = Xmean + P*b.','ASM'));   TerminateNotContinue = TryWeights(MeanShape,tEigenvectors,ContoursEndingPoints);   %TerminateNotContinue = TryWeights_XT(MeanShape,tEigenvectors,ContoursEndingPoints);   if TerminateNotContinue msgbox('Terminating: cancelled trying weights','ASM'); return; endend%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    APPLICATION STAGE    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%ButtonName=questdlg('Do you want to search for a shape in an image?','ASM');if(strcmp(ButtonName,'Cancel'))   msgbox('Terminating: cancelled request to search for a shape.','ASM');   return; elseif  (strcmp(ButtonName,'Yes'))%   uiwait(helpdlg('THIS IS THIRD STAGE: APPLICATION STAGE.','ASM'));   uiwait(helpdlg('Now, you will choose an image to search and enter some initialization information','ASM'));%   TerminateNotContinue=FindShapeInImage(MeanShape,tEigenvectors,tEigenValues,W,ContoursEndingPoints,...%      MnNrmDrvProfiles,ProfilesCov,TrnPntsBelow,TrnPntsAbove,MaxNumPyramidLevels);   TerminateNotContinue=iFindShapeInImage(MeanShape,tEigenvectors,tEigenValues,W,ContoursEndingPoints,...      MnNrmDrvProfiles,ProfilesCov,TrnPntsBelow,TrnPntsAbove,2);      if TerminateNotContinue msgbox('Terminating: cancelled searching image','ASM'); return; endendmsgbox('Done!','ASM');

⌨️ 快捷键说明

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