Code covered by the BSD License  

Highlights from
Active Shape Model (ASM) and Active Appearance Model (AAM)


4.6 | 38 ratings Rate this file 337 Downloads (last 30 days) File Size: 15.1 MB File ID: #26706
image thumbnail

Active Shape Model (ASM) and Active Appearance Model (AAM)



16 Feb 2010 (Updated )

Cootes 2D/3D Active Shape & Appearance Model for automatic image object segmentation and recognition

| Watch this File

File Information

This is an example of the basic Active Shape Model (ASM) and also the Active Appearance Model (AAM) as introduced by Cootes and Taylor, 2D and 3D with multi-resolution approach, color image support and improved edge finding method. Very useful for automatic segmentation and recognition of biomedical objects.

Basic idea ASM:
The ASM model is trained from manually drawn contours (surfaces in 3D) in training images. The ASM model finds the main variations in the training data using Principal Component Analysis (PCA), which enables the model to automatically recognize if a contour is a possible/good object contour. Also the ASM modes contains matrices describing the texture of the lines perpendicular to the control point, these are used to correct the positions in the search step.

After creating the ASM model, an initial contour is deformed by finding the best texture match for the control points. This is an iterative process, in which the movement of the control points is limited by what the ASM model recognizes from the training data as a "normal" object contour.

Basic idea AAM:
PCA is used to find the mean shape and main variations of the training data to the mean shape. After finding the Shape Model, all training data objects are deformed to the main shape, and the pixels converted to vectors. Then PCA is used to find the mean appearance (intensities), and variances of the appearance in the training set.
Both the Shape and Appearance Model are combined with PCA to one AAM-model.
By displacing the parameters in the training set with a know amount, an model can be created which gives the optimal parameter update for a certain difference in model-intensities and normal image intensities. This model is used in the search stage.

- Ginneken B. et al. "Active Shape Model Segmentation with Optimal Features", IEEE Transactions on Medical Imaging 2002.
 - T.F. Cootes, G.J Edwards, and C,J. Taylor "Active Appearance Models", Proc. European Conference on Computer Vision 1998
 - T.F. Cootes, G.J Edwards, and C,J. Taylor "Active Appearance Models", IEEE Transactions on Pattern Analysis and Machine Intelligence 2001

Run the example "ASM_2D_example" and "AAM_AAM_2D_example", the examples are made from manually drawn contours in 10 hand photos (with DrawContourGui). After training the ASM / AAM model will automatically find the contour in the test hand image.

First train the classifier to recognize the Mandible "ASM_3D_train_example" or "AAM_3D_train_example", and then segment with "ASM_3D_apply_example", or "AAM_3D_apply_example".

Please leave a comment, if you like it, find a bug, or know (made) good improvements to the code.

Required Products Image Processing Toolbox
MATLAB release MATLAB 7.10 (R2010a)
Other requirements For the 3D examples, 64bit Matlab and c-compiler
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (105)
21 Nov 2014 zhixiang

There are somethings wrong about the results.Can you tell me how to promote the code?Thank you!

12 Aug 2014 subha

I am doing my project in lung nodule segmentation. I have to the segment lung alone, for that i am using ASM model. What is the value that i have to use for vertices? Thanks in advance.

12 Aug 2014 HEMALATHA P

I am trying to use this ASM model for creating a model of knee image.please i cant understand this coding.kindly explain it.

10 Aug 2014 Anum

Hello all !
I am using this code for the lung segmentation of xrays by defining my own landmark points but getting following error :

Subscripted assignment dimension mismatch.

Error in ASM_MakeShapeModel2D (line 20)
x(:,i)=[TrainingData(i).CVertices(:,1)' TrainingData(i).CVertices(:,2)']';

Error in ASM_2D_example (line 82)
[ShapeData TrainingData]= ASM_MakeShapeModel2D(TrainingData);

Can anyone help me out where I am doing wrong ?

04 Aug 2014 Anum

Hello all !
I am working on a project for which I have to segment lung regions using ASM from chest xrays. Can anyone help me out that what changes I should made for making this code usable for my project.
Thanks in advance.

24 Jul 2014 Wang Hongyu

Thank you very much for the sharing.I hanve a question about how to create the simulation data in the 3D ASM algorithm(eg:V 400*400*352 logical).

Thanks again.

05 Jul 2014 si wei

Hello all,I learned a lot for this model.But when I use this model for face property,It didn't works well.I didn't change the model.Are there any changes in the model?Thanks a lot.

03 Jul 2014 Faniya Mokhayeri  
16 Jun 2014 Lianli

Hi all, I am a bit confused about the use of "face" property. Why specify contour points (vertices) will not suffice? Many thanks!

03 Jun 2014 Harley  
03 Jun 2014 Harley  
27 May 2014 Luca

is it possible to train the software for multiple contours eg. face countour + nose countour?

09 Apr 2014 Taoufik

I need ASM for detection points on the frontal face recognition. I will use for facial expression recognition. My email is:
Thanks a lot!

04 Apr 2014 Sorath Asnani

Hello all. I tried ASM_2D_example.m
It is working perfectly. When I tried it for my own training data set (Images of Guns), after showing the gun images with landmark points upto figure 4, i got the following error.

Subscripted assignment dimension mismatch.

Error in ASM_MakeShapeModel2D (line 19)
x(:,i)=[TrainingData(i).CVertices(:,1)' TrainingData(i).CVertices(:,2)']';

Can anyone help me to resolve this issue?

21 Feb 2014 nguyen  
19 Feb 2014 Tang

thanks for your sharing, but there are 2 bugs as I run:
1 function SelectPosition doesn't correspond sometimes, keeping running: while(~data.done), pause(0.1); data=getMyData(); end
2 DrawContourGui get contours perpendicular with real images.

22 Jan 2014 Chaw Thim Vai

Hi everyone here, I am new to this image processing topic. I executed this program "AAM_2D_example.m" and there are some errors. I don't know how to solve it. Is anyone here can help me? Thanks
Error using zeros
Out of memory. Type HELP MEMORY for your options.

Error in AAM_MakeSearchModel2D (line 6)

Error in AAM_2D_example (line 154)

15 Dec 2013 Andre Agustin

Hi, I'm very new to this and image processing in general but I'm interested in using aam for object recognition/segmentation. It's quite amazing for me and so far AAM_ApplyModel2D works but when it goes to piecewise linear transformation (cp2tform) something goes wrong

it gives this error
'At least 4 non-collinear points needed to infer piecewise linear transform.'

The objects that I was trying to detect were eyeglasses with 7 training images.

06 Dec 2013 Ehsan golk

I encounter this error after applying for my own 3D training data please help me:
Error in ASM_align_data3D (line 17)

Error in ASM_MakeShapeModel3D (line 16)

The vectors of my have different dimensions because using marching-cube create different vertices sizes. please hep me if possible.thanks

21 Nov 2013 fatemeh

Hi everyone,I applied this code on my images but in part of"% Show some eigenvector variations" my shape shown in incorrect direction and then in part "% Apply the ASM model onm the test image" shape moved in incorrect direction.Does anyone know about this?

20 Nov 2013 moha  
31 Oct 2013 Laura

I would like to use this code with faces (face tracking). Do you know any set of training set images or demo that I can use?



17 Sep 2013 WONG

hello,evertyone.I want to apply this code on 3d segmentation.It works well.The only thing I want to know is how to put the trained shape on a initial position for segmenting like the example for 2d hand.I think it's necessary as updated points positions are limited by PCA.If we don't put the trained shape on a initial position like the example for 2d hand,the result should be uncorrect.Thanks.

15 Aug 2013 TU Darmstadt

@All with out of memory issues:
This happens because in AAM_MakeSearchModel the variable drdp is 4D, you can just split it into smaller matrices, such as i did it in 2 3D matrices,and did averaging by adding and dividing instead of using mean and squeeze function. Because in the end you are using this drdp for the calculation of matrix R which is 2D

19 Jul 2013 Yiping


Hi, I had the same error as yours, when it tried to allocate a matrix of 13*6*10*468249 = 365234220 double precision values.
I've added a "single" parameter to the "zero" statement that make it use single precision values instead, and it uses approx. 1.3GB of memory now. Before the change it might take 2.6GB, which will cause a "Out of memory" on my PC.
I don't know whether this change will have an impact on accuracy.


04 Jun 2013 Ardeal

Hi, All,

when I run the code without any changes after I download it, I get the following error messages:
??? Error using ==> zeros
Out of memory. Type HELP MEMORY for your options.

Error in ==> AAM_MakeSearchModel2D at 6

Error in ==> AAM_2D_example at 154

The RAM of my PC is 4GB, and I have already enlarged the virtual RAM to 3G. I think I have enough memory.
why did that error happen still?

04 Jun 2013 Le Hien  
11 May 2013 Ming Jin

goodwork, thanks

07 Apr 2013 liao lulu

how can i use the ASM_2D_example.m to train my own shapes? my email :
thanks a lot!

11 Mar 2013 Zuying Wo

is this sample permitted in commercial use or any development for some commercial purpose?

19 Feb 2013 Isabel  
27 Nov 2012 Jack Sparrow  
12 Nov 2012 snoorwal Noorwali

I ran the "AAM_2D_example" in my MAC computer
it is work but it is stuck in some point ! ... I just see the 7 Figures that apeares then it is stuck I am not sure why !!

I have question , how can I implement this code to work in something else (segmenting the vertebra for example)

12 Nov 2012 snoorwal Noorwali

I ran the "AAM_2D_example" in my MAC computer
it is work but it is stuck in some point ! ... I just see the 7 Figures that apeares then it is stuck I am not sure why !!

I have question , how can I implement this code to work in something else (segmenting the vertebra for example)

06 Nov 2012 LIU WEIHUA  
18 Aug 2012 huang  
28 Jul 2012 ainleuh-89

j ia pas compris le principe de asm si quilqun m'idee pour comprendre le principe d asm

18 Jul 2012 Ana

Dear all,

can any one help me with how to make the .mat file for the new training image for 3D?

22 Jun 2012 Lacey

Does anybody have code to apply this to face images? I'm looking to use it to extract the full contour of a face. I can't seem to get the model trained properly. It is always too far rotated one direction or the other, but my training images are all well aligned.

20 Jun 2012 Ming

Very Good

07 Jun 2012 saman abdanan

could you please explain to me why you plus and minus 0.5 to the following line of ASM_MAkeAppearanceModel2D
% Get the pixel profiles of every landmark perpendicular to the contour
for i=1:s
P = (TrainingData(i).Vertices-0.5)*scale+0.5;

30 Apr 2012 shweta

Hi all,
I need some help urgently. I have been trying to implement the AAM_2D_example model for fetal ultrasound images. It works perfectly for some training data but generates the below mentioned error in other data sets. I am of the opinion if it is so because of change in image scales for the training sets. So i ran the code for certain similar scale images too still i get stuck sometimes with this error. Although I have made the contours keeping the number of major Landmarks same across all the training images. Please will be grateful if any one of u could give some suggestion to get rid of this error.

??? Error using ==> minus
Matrix dimensions must agree.

Error in ==> AAM_align_data2D at 25

Error in ==> AAM_MakeShapeModel2D at 19

Error in ==> AAM_2D_US at 95
[ShapeData,TrainingData] = AAM_MakeShapeModel2D(TrainingData,options);

Also does the training contours drawn using DrawContourGui need to have same number of major land marks and have to have closed loops?
Will be looking forward to some early replies.

30 Apr 2012 shweta  
02 Apr 2012 Manoj Murali

See the image with feature points or landmark points marked on an image as shown in the link below:

Now my problem is that I need to write a program which could identify and plot the landmark points on any face image given as the test/input image to the program.I think we need to first train the program with a set of database images with the land mark points.Then I need to use some logic to exactly fit those points on the specific positions in the input image to identify the feature points on the face.So frns how to do this program.What methodology cud be used to do this coding.I need some suggessions..

02 Apr 2012 Manoj Murali

Friends my project is to perfom what is shown in the below youtube link using Matlab.Plz help me how to start form the begiining from creation of databases step by step..plz help me..

27 Mar 2012 Long

The error proclaimed itself: your computer does not have enough memory. Try a better computer.

22 Mar 2012 amany

hi everybody >>> i need help plz,,,,

i get this error when i applied ASM_3D_train_example.m:

Error using ==> rand
Out of memory. Type HELP MEMORY for your options.

Error in ==> ASM_3D_train_example at 62
I=single(V)-0.5*single(imerode(V,ones(5,5,5))); I=I+rand(size(I))*0.1;

and get the same on applying AAM_3D_train_example.m

i used matlab 2009a and visual studio 2008

21 Mar 2012 R

In 2D DrawContourGui works, does anyone has a solution how a trainingset can be created for a 3D dataset?

21 Mar 2012 R

example=training dataset

21 Mar 2012 researcher

to robert,and any one needs to apply this program on his own images, you should use DRawContureGui.m to get the mat file. you will find the program in functions folder.

save the mat files and use them in the example program.

15 Mar 2012 robert

Dear all,
can any one help me with how to make the .mat file for the new training image?

14 Mar 2012 Chen

In the file ASM_align_data2D.m, the position and rotation are considered during aligning the training set, but the scale is not considered. It seems that the align process is simplified very much.


If anyone has applied it for face recognition please share the code. It will be much appreciated.

17 Jan 2012 Filka Yaneva

Dear Fati,
I have the same quenstion. Did you get some answer?

Thank you very much,

17 Jan 2012 Fatima laghrafi

Dear all,
Ineed facial expression recognition AAM or ASM ? how can I use this code to train my own databse?.
my email :
Thanks in advance.

16 Jan 2012 Fatima laghrafi

Dear all,
Can I use this demo for Facial expression recognition? If so, could you please specify what parts need to be modified ?.

Thanks in advance.


06 Jan 2012 Ray  
02 Jan 2012 alps jia

Dear all
when I was running "ASM_2D_example.m",
I have some questions about "ASM Functions\ASM_ApplyModel2D.m ":
In the file
code number:66-70
"for k=1:size(Itest,3),
I have made some changes about "coffset2" as follows:
But it does not get a correct resoult,
So I want to know how to align "gt",

07 Dec 2011 marshmallow

I need a Face recognition model too, how could I train my own face as dataset? my email is:
Thanks a lot!

24 Nov 2011 Sriraj

Anybody tried this code for Face recognition pls share Face shape model with me at

22 Oct 2011 divya

Thanx for d have done a great job.i am doing my thesis on sign language recognition. i have segmented the hand and done with fingertips detection and tracking.i want to do feature extraction using PCA(principal component analysis). please suggest me which features should i take to match signs and how to do this. I shall be very thankful to you

10 Oct 2011 yayun


When I executed the ASM_2D_example.m, I got this information

"can't find interp2fast_double.mexw32.manifest" , and I check the folder, it only produced a file called interp2fast_double.mex not interp2fast_double.mexw32.manifest, so how can I do to solve this problem? By the way, my computer system is Windows7 and Matlab version is (R2009b)7.9.0.
Thank you very much!!!

23 Aug 2011 shruti saraswat

Hello,How can we rotate the landmarking image? is there any coding or just a key function? Please help I am learning this landmarking.

15 Aug 2011 Jiahao Qiu  
13 Aug 2011 Jiahao Qiu

??? Referance to non-existent field 'offsetv'.
Error in ==>AAM_align_data_inverse2D at 15 Vertices(:,1) = Vertices(:,1)-tform.offsetv(1).
Error in==>AAM_2D_example at 188
pos = AAM_align_data_inverse2D(pos,tform)
How can i maintain the code,which will run successully?

30 Jul 2011 Jiahao Qiu

Dear Mr, Kroon,
When i run the 'AAM_2D_example.m' on matlab 2010a,the command window shows 'compile c-files failed :example will be slow'.How can i compile c-files successful?
Thanks in advance.

29 Jul 2011 samia dop

please i need a help i need just someone how can send me training data wich he used just 10 images tks for all

27 Jul 2011 fpuspitasari

hi, when training the data, should the image contour be located in roughly the same location? what if I have several hand images with different orientation, e.g. horizontal and 45 degree angle. should I do some pre-processing before training the data. thanks

10 Jul 2011 samia dop

first u choose ur image for example u do that:
im2=lirimage;u read ur training data then u save it
save('samia','im2');ur training data now is called samia.mat

06 Jul 2011 Pasindu Abeysundera

How do we create the .Mat files in the training data. Can some one please enlighten me?

03 Jul 2011 samia dop

When I execute either ASM_example.m or AAM_example.m
I met an error:
??? Undefined variable "p" or class "p.x".

Error in ==> efface at 21
pointsx=interp(p.x,r); pointsx=pointsx(1:end-r+1);

07 Jun 2011 Zheng

I read the code again. I find that the function AAM_NormalizeAppearance.m is used to compensate for illumination, and plays the similar role to the texture transform parameter u.

07 Jun 2011 Zheng

The classical aam model contains three parts of paramters: c,t,and u. u is the texture transformation. But in the function AAM_MakeSearchModel.m, u is not included. Why? and how to include it?

07 Jun 2011 Munie Ahmad

Hi SDU, now it works perfectly! Thank u so much for your solution :)

05 Jun 2011 sdu

Hi,Munie Ahmad,I meet the same trouble like yours.I do this like below:Add the function'save' after AppearanceData.It you want to use training data, then 'load'.
AppearanceData = ASM_MakeAppearanceModel(TrainingData,options);

05 Jun 2011 Munie Ahmad

Hi there, I've tried with my image database (chest x-ray, for lung segmentation), it works, many thanks! But how can I run the algorithm for batch processing without repeating the 'training process'? Thank u in advance..

08 May 2011 sdu

With my training data,It works,Thank you!

22 Apr 2011 Joy King

I'm using ASM and AAM to train and segment carotid ultrasound images. ASM_example.m works very well, But when I execute AAM_example.m, I met an error:
??? Error using ==> cgprechecks at 47
Not enough unique points specified.

Error in ==> delaunayn at 48
cgprechecks(x, nargin, cg_opt);

Error in ==> delaunay at 50
tri = delaunayn([x(:) y(:)]);

Error in ==> PreProcessCp2tform at 43
tri = delaunay(xy(:,1),xy(:,2));

Error in ==> AAM_Appearance2Vector at 20
[xy uv]=PreProcessCp2tform(xy,uv);

Error in ==> AAM_MakeAppearanceModel at 32
g(:,i)=AAM_Appearance2Vector(TrainingData(i).I,TrainingData(i).x,TrainingData(i).y, base_points,

Error in ==> AAM_example at 90

If I use your dataset, it works very well. I really don't know why, hoping you can help me or give directions. Thanks in advance.

18 Apr 2011 Saidani

thesis a good work
can i use this work for face base
if yes how
please help me

17 Mar 2011 mirs

Here is maybe the solution, by I can´t tell exactly, didn´t try yet.

17 Mar 2011 Agnieszka

I have the same problem with function DrawContourGui.m. Have anyone knows how to run this function without error. Thanks in advance

16 Mar 2011 mirs

Thanks a lot! I would like to use your work as a part of my school project, which deals with facial detection (nose, eyes, mouth). For face detection, I am going to use Viola-Jones detector, for it´s parts AAM and ASM. But I have a little problem, when I´m trying to run DrawCountoutGui, there is some kind of bug "Segmentation violation". Could you please tell me, how to run it properly?

Best regards

08 Mar 2011 Manuel  
03 Mar 2011 Green_Apple

I have researched about this.Can you share your code with me for reference, Weiyuan?

Thanks and best regards!

01 Mar 2011 Fettah Light


i want just know how i can get the train.mat from an image, so if u have code source of this function ,can u give it to me

With my best regards

28 Feb 2011 Nslakshmi

Anybody tried this code for Face recognition pls share Face shape model with me at

27 Feb 2011 Weiyuan

Very good.

I made some modifications for faces, and it works!! Thanks a lot.

04 Feb 2011 Aakash


24 Jan 2011 Qiang Li

I applied it on faces with little revisions:
First, in building of the combine model, 'dpdr' may be too large for a real application. I replaced it by averaging submatrix in the big loop.
Second, 'svds' in pca may cause unexpected mistake... I use svd instead, though it may slower...

02 Jan 2011 A.Rahim

Dear all

I want to apply this script on my own images. Could guide me how I can do this (in which order I should use the function) ?

18 Dec 2010 Muhammad

"??? Subscripted assignment dimension mismatch.

Error in ==> ASM_MakeShapeModel at 20

I am using this code for ear. I trained 10 images of ear. but I am getting this error. can any one help me plzzzz

15 Sep 2010 xudong cao

very good!

13 Sep 2010 Mofakharul

Do we need to convert screen coordinates into image coordinates? If yes, then how it could be done?


07 Aug 2010 Iqbal Nouyed

Dear Mr, Kroon,
Can I use this demo for Facial expression recognition? If so, could you please specify what parts need to be modified ? I am trying to train and test it with the data set given with the cootes' aam tools.

Thanks in advance.


16 Jul 2010 Dirk-Jan Kroon

* Zhi
Interp is part of the "Signal Processing Toolbox" of Matlab.

15 Jul 2010 Zhi

When I execute either ASM_example.m or AAM_example.m
I met an error:
??? Undefined function or method 'interp' for input arguments of type 'double'.

Error in ==> LoadDataSetNiceContour at 21
pointsx=interp(p.x,r); pointsx=pointsx(1:end-r+1);

So where is interp.m?

19 Jun 2010 Dirk-Jan Kroon

*ISI Purkait

The size of a dataset will not cause an error. Try to display some in between results, maybe you find the error

18 Jun 2010 Greg B

Is it possible to have multiple contours in a model with this program? Imagine two separate eyes or distinct image features that are not connected. If it can't do multiples, I'll just double back when drawing contour lines or attempt to add multiple contour on my own :)
Thanks for the program, I've been looking for it for a long time and it works great!

10 Jun 2010 ISI Purkait

Why this program is not working for large dataset??....I made some hand images on my own....there are 34 images ....but it's working but texture is not comming well...!!

01 Apr 2010 Ashwith Rego

In the function AAM_align_data.m, you have chosen the base to always be the first shape. According to Procrustes analysis, isn't the base supposed to be the mean of the current set of aligned shapes? Am I missing something in your code or are you using some sort of modified Procrustes analysis which aligns all shapes to the first shape? If it is the latter how much difference would it make in the result?

25 Mar 2010 Ashwith Rego

I figured it out. Took me some time to realize what exactly you meant. Thanks a ton again! :-)

25 Mar 2010 Ashwith Rego

Thanks for your reply.
How do you convert between screen coordinates and image coordinates?

25 Mar 2010 Dirk-Jan Kroon

* Ashwith ,
The x and y are indeed the row and column indexes, but if you click the mouse the coordinates are converted from screen coordinates to image coordinates, which gives the .1234 numbers, and also of course the linear interpolation between the landmarks.

The Matrix I, is the training image.

The vector t, specifies if a point is a major well defined landmark (like the top of a finger), or a minor point along an edge.

25 Mar 2010 Ashwith Rego

Could you tell me how one selects the landmark points. And how do you set the coordinates of these points? Till now I was under the impression that the coordinates are the row and column indexes of the pixel in the image matrix. I saw x and y having values like 216.8761?
Secondly, for the database of training images, what are vector t and matrix I for? I understand x and y give the coordinates of each point. What are t and I?
This is my first project in image processing so I really have no clue.
Thanks in advance!

24 Mar 2010 M Ladderman


02 Mar 2010

PCA bug fix.

17 Mar 2010

Added the Active Appearance Model

22 Mar 2010

Added Color support, new edge finding method in ASM, and contour starting point GUI.

20 Jul 2011

Added 3D examples.
Added LinearWarp function

26 Jan 2012

Many bugfixes and ASM and AAM optimizations

Contact us