File Exchange

image thumbnail

Automatic 2d Rigid Body Image Registration

version (112 KB) by Matthew Sochor
Automatic 2d rigid registration using improved MI kernel


Updated 19 Mar 2008

No License

Rigid body 2-D image co-registration (translation and rotation) is performed using maximization of mutual information.

Function is implemented in c-code and compiled using the Matlab compiler to minimize computation time. Current compilation will only run on a 32-Bit windows machine running 2007b. Matlab compiler is necessary to re-compile for other versions/platforms.

Mutual information joint entropy matrix is computed using the Hanning windowed sinc function as the kernel of interpolation, which is the HPV estimation method [1].

Maximization of the joint entropy matrix is carried out using Powel's Direction set method [2] with original c-code very slightly modified from J.P. Moreau's code[3].

[1] Lu X, et al., Mutual information-based multimodal image registration using a novel joint histogram estimation, Comput Med Imaging Graph (2008), doi:10.1016/j.compmedimag.2007.12.001

[2] Numerical Recipes, The Art of Scientific Computing By W.H. Press, B.P. Flannery, S.A. Teukolsky and W.T. Vetterling, Cambridge University Press, 1986


Cite As

Matthew Sochor (2020). Automatic 2d Rigid Body Image Registration (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (12)


Which modification I can make for use on a 64-Bit Windows Machine?

Compiling using Matlab Compiler 2012a and MS Visual Studio 2010 on Windows 7 worked well for me with no modifications to the code. Successful registration of two 1000x783 pixel images achieved. Thanks for the useful program!


Hi, All,
I had the same segment fault problem. But I found after did
the following it worked:
At the bottom of the file, in function void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { },
REMOVE this line:
pTranslate = (double *)mxGetPr(prhs[2]);

We ONLY have 2 input parameters. prhs is the array of pointers to the input mxArrays. The above line read the
third parameter and caused segment faults.

After remove the line, I compiled with Open Watcom 1.9
in Matlab Version 32-bit edition.
It worked nicely.



I have the same problem as Frank on Dec 02 2009. What compiler was originally used with this?


My way to fix this code:
1. when input image size<256, error occurs...
you can change 256 in the for loops to nX or nY
2. when image size is too small, registration accuracy gets worse..
when image size is too small. the 256-bin histogram gets less samples. We can use less intensity bins, say, change from uint8 (0-255) to(0-80).


Running Xp 32bit the mex files in the zip does not work (Segmentation violation detected) !
I tried to compile again the .cpp with "mex mi_hpv_2d.cpp" but it seems that lcc compiler is not able to compile it. Does someone already encountered this problem ? Is there any solution, installing visual c++ compiler ? other solutions ?

Thanks a lot !

Casey Ta

I found the following two bugs:

In the mi_hpv function. It seems like P_A and P_B should both be created with a size of 256. I was testing some small images and would keep getting seg faults since the second to last for loop would write past the bounds of the array when the image height or width is less than 256.

In mexFunction, the following line is unnecessary and caused exceptions when I was debugging in Visual Studio 2008 since there are only 2 input parameters:
pTranslate = (double *)mxGetPr(prhs[2]);

Also, for the problems noted by Yang Shuo above, replace the first line with the code that comes after the comment, and replace the second line with:
Float = imrotate(Ref, 30, 'bilinear', 'crop');

yang shuo

why it gives the result as follows:
??? Error using ==> imread at 315
File "sl_phantom.tif" does not exist.

Error in ==> d_mi_registration at 65
Ref = imread('sl_phantom.tif'); % same as: Ref = uint8(phantom.*255);

Lee Cooper

Would like to use this since it is probably fast but it seg faults on my machine. Recompiled with mex, still crashes. Using for 1000x1000 images. Maybe this is the problem?

Also, a script cannot begin with a number character.

yanjun cui

File "sl_phantom.tif" does not exist.

Matthew Fenty

Installed this program without problem and works fantastically. All of the other co-registration programs out there are nothing compared to this one.

Thanks a bunch


Added some flexibility in the code for older c compilers. Should be a little easier to re-compile for those users now.

Added screenshot.

Fixed error when using non-square input. Is now capable of handling any rectangle.

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