File Exchange

image thumbnail

ICAAM - Inverse compositional Active Appearance Models

version (60.8 KB) by Luca Vezzaro
An implementation of inverse-compositional Active Appearance Models capable of real-time performance

1 Download

Updated 29 Feb 2012

View License

This is an implementation of real-time inverse compositional Active Appearance Models (AAMs), as described in the "Active Appearance Models Revisited" paper by Iain Matthews and Simon Baker.

Experimental support is provided for the 3D extensions to inverse compositional AAMs, also developed by Matthews and Baker in their "2D vs. 3D Deformable Face Models" and "Real-Time Combined 2D+3D Active Appearance Models" papers.

As an aid to ease the task of building the 3D shape model needed by the 3D extensions, an implementation of the closed-form shape-from-motion algorithm by Jing Xiao and Takeo Kanade is provided. And while I can't guarantee the quality of the results given by this latter implementation, it shouldn't be far from correct as it's heavily based on Xiao's implementation.

Look up "icaam" on to get the latest release, the data required by the examples and more!

Cite As

Luca Vezzaro (2020). ICAAM - Inverse compositional Active Appearance Models (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (50)

Minh-Ha Le

Thank you for sharing the code!
Would you please tell me how can I use this code for fitting the 3D Morphable Model (vetter and blanz)?

Bingqing Qu

@Luca Vezzaro

Thx! It is quite helpful!


Annotation files are using cartesian coordinates:


+---> x

Each point is stored as a row:

|x1 y1|
|x2 y2|
|xn yn|

The code uses matlab convention for image coordinates:

+----> j

and are stored in the same way:

|i1 j1|
|i2 j2|
|in jn|

Check the xy2ij() and ij2xy() functions for converting between the two.

Bingqing Qu

Hi, Luca

Thanks for your sharing.

All the code are running correctly on my computer. Just got some logic questions.

Did you train the model that all coordinates related to shapes are in inverse order? shape = [Y X] as example.

Many thanks!

I've generated an initial shape. But the AAM still keeps diverging.

Please find more details at:


1. This is creating the annotations variable:

load(sprintf('cootes/%s', training_files(i).name));

Use the build_model_2d_from_files to not be exposed to such intricacies.

2. What do you mean? fit_2d is the function to fit. You need to provide a shape to start the fit. As AAMs don't do face detection you need to first apply a face detection algorithm to detect the approximate location of the face and use that to fit your AAM to the detected face.

I have a couple of queries:
1. The examples/fit2d_test.m loads shape using
shapes(:,:,i) = xy2ij(annotations, size(app,1));
but the variable 'annotations' is not declared previously. Where do you get the annotations and load to 'shapes'?

2. How do I fit the AAM to a file with an initial shape? (i.e. a non-annotated image). I am a beginner in this area and can't seem to make it work

I'll try to fix the issue, but first I need to get MATLAB 2012b. I'll see what I can do.

Please use a older MATLAB version, if you can.


Hi Luca,
I am using MATLAB 2012b at the moment (sorry for the typo). I just tried icaam-1.0-r17 with 2013-04-23 as last update, which is the latest version accoring to sourceforge. However, I still get the same error with PCA as before.

I also tried to replace princomp() with pca(), but this causes another error later in the code, so I assume that these two methods have different outputs.

Anika, do you mean Matlab R2011b?

Are you sure you're using the latest version from here?


When I try to run the code with MATLAB 1021b, I run into problems with PCA. the following error message pops up, when running tesi_train.m

Error using pca
Too many input arguments.

Error in princomp (line 29)

Error in var_pca (line 32)
[pc, score, eiv_] = princomp(data', 'econ');

Error in build_model_2d_from_files (line 230)
[pc eiv] = var_pca(shape_matrix, 0.98);

Thanks for the feedback Sid Rajaram. Good to know!

Sid Rajaram

Just to mention - I have all the examples working just fine on 64-bit Ubuntu 12.04...


can any one give the matlab code for 'facial feature extraction with DEPTH AAM algorithm'....

If you just consider each frame of the video as a separate image you shouldn't have any trouble


thank you and if i need to use your project on video , i have to fix or add anything??

i'm a beginner so sorry for any question

Yes, but you'll probably need to do some additional processing on the data as AAMs only give you the position of the recognized features.


can I use your project for facial expression recognition ??

I'm using the exact same version of Matlab, but my procrustes.m does not contain that line.
Please tell me where your procrustes.m is located (mine is at: C:\Program Files\MATLAB\R2011a\toolbox\stats\stats).

Check the Matlab path (File->Set Path...) to make sure you don't have a file named procustes.m that is being used instead of the one provided by the statistics toolbox.

As a last resort you could try reinstalling the statistics toolbox...

Thanks for the quick reply.
I use matlab 7.12.0(R2011a) and my OS is win 7 64bit. I am using the package that download from sourceforge and it contain all of files (codes and datasets).
Image processing toolbox works well.
At line 25 of procrustes.m file value of data(1) is 125.7963 and it does not contain marks property, so matlab throw error.

First of all what version of Matlab are you using, and what is your operating system?

Are you using the package from this site or the "official" one from sourceforge? The one on this site does not include the image files for the examples, so that may be your problem.

Also make sure you've installed the statistics and image processing toolbox as they are required.

Hi Mr Luca Vezzaro,
I try to use this codes but I catch this error in procrustes.m, when run example files (such as annotate_test.m, fit2d_test.m and etc):

??? Improper index matrix reference.

Error in ==> procrustes at 25
p = size(data(1).marks,2);

Error in ==> build_model_2d at 182
[ D Y ] = procrustes(mean_shape, aligned_data(:,:,i));

Error in ==> annotate_test at 29
AAM = build_model_2d(shapes, appearances, 'triangulation', triangulation);

that i think that marks is unknown.

What version are you using? Make sure you're using the latest version. Also please tell me if you're using Windows or Linux Matlab (no one ever really tested this under Linux).
To understand how to use the 3D algorithms, look at the tesi_train.m and tesi_sfm.m examples. The motion dataset has some low quality data (the files with the exclamation marks), and I don't remember if those should be used or not.
Finally, I never really got fantastic results with that algorithm and that data. Your best shot is to either run the shape-from-motion a lot of time (as it's randomized) or to use your own 2D motion data.

sohaib azed

thankyou for the project. i was able to train my data with the help of examples in the project. but i am having problem with the fitting algorithm fit_3d(). the function either gives an error or gives an output with high error(the model does not fit on the face) when i provide my own pic.

i have tried 'tesi/motion' to make the shape model as well. still i get incorrect results.

when i give (dataset/IMM) to make the shape model. i get an error

""Number of landmarks in init_shape and/or number of colors in image_data are not consistent with the model.""

sohaib azed

i have tried 'tesi/motion' to make the shape model as well. still i get incorrect results.

when i give (dataset/IMM) to make the shape model. i get an error

""Number of landmarks in init_shape and/or number of colors in image_data are not consistent with the model.""

That could be caused by a lot of reasons.
The only way to find out is by trial and error, unfortunately...

Try using smaller training sets and try using images from the training set to do the fitting, instead of doing leave-one-out trials.

i can't not compile the fit2d_test.m using my own data annotated to fit AAM ,he always do the catch : Fitting diverged..
what i should do please.
thanks a lot

Thanks a lot Mr Luca Vezzaro.

Firstly, make sure all your images are annotated and use the same amount of landmarks.

I recommend that after you create the first annotation file, to use that as a start for annotating the other images.

If you are sure the annotations are consistent, see the example "fit2d_test.m". It shouldn't be hard to understand how to use build_model_2d.

it's the function ij2xy(in, maxy) ? because i have some compilation error so i don't know if my steps are correcte or no.
thanks a lot

to constructe AAM model we use function build_model_2d(shape_data, app_data)it's right ? i don't understand how to compute shape data and app-data from my Data set.
Tanks a lot for help.

Perfect, it work now with gray scale images and png form.
Thanks a lot

I fixed the bug in the latest release (r17). You can download it here:

perfect, it's work now, after converting images. i will try after that to annotate my data , and constructe AAM model.
Thanks a lot

Ok now I understand. The image command is interpreting the gray scale image as color indexed images!
When I get back from work I'll fix this bug.
In the meantime, you can use this work-around to convert single-channel gray scale images to RGB gray scale images (assuming I still remember well how repmat works):

img = imread('image_path_gray_scale')
img_rgb = repmat(img, [1 1 3])
imwrite(img_rgb, 'image_path_rgb')

ans =
ans =
ans =

Thanks a lot.

As I expected, the gray scale PNG image data is not in range 0-255.
Please copy and paste the output of the following commands:

img = imread('image_path_gray_scale')




Sorry I'm at work and I don't have MATLAB here so I can't do this myself.

images are .png format (cohen kanade data).
if i do image(imread('image_path_gray_scale')) i get a good picture with some color for face, but if i do image(double(imread('image_path_gray_scale')) / 255) the out put is different , is a blue window with nothing.
thanks a lot

It's strange, annotate should work with gray scale images... In what kind of format are your images stored?

If you type:


What do you see?

What if you type:

image(double(imread('image_path_gray_scale')) / 255)

Is the output the same, or different?

Thanks for your answer,
But my data set is a gray scale images, and i wan't to annotate it. when i use annotate('image_path_gray_scale')it give me a blue window, without any image.
Thanks a lot

You can annotate the images anyway, just use rgb2gray ( after imread to convert the image data to gray scale. You don't need to change the annotation data.

hello, all
i've tried your code. thanks for the project . i would like to train my data set in gray scale, i want to know if it's possible, because i cant not do the annotation.m with gray scale image. thanks a lot.

Have you tried the suggestions I gave to mashmallows (second comment)?
Please try that and also try to read the documentation.
If after that you still can't manage to build the training set I'll be happy to help.


I've tried you code. Thanks for the project.I would like to train my own data set (gris scay image . png)in order to extract my facial features. How should I do? How could I generate my photos to the .mat and the triangulation.mat?
Tnanks a lot

Hello! Thanks for your interest in the project. First of all, be sure to use the latest version from sourceforge ( as there has been recent bug fixes.
Also reading the README.txt file inside the package should give you a start.
Anyway, you generate the annotation data using the annotate.m script (type 'help annotate'). The triangulation.mat is not mandatory, but you can generate it using delaunay ('help delaunay'), that's what the training code uses anyway.


Hello, I've tried you code. Thanks for the project. You have done a great job! I would like to train my own data set in order to extract my facial features. How should I do? How could I generate my photos to the .mat and the triangulation.mat? Thanks


Update to latest revision, adding optimizations and fixing bugs.

MATLAB Release Compatibility
Created with R2008b
Compatible with any release
Platform Compatibility
Windows macOS Linux