MATLAB Answers

How can I force imregister to use the parameter 'InitialTransformation'?

19 views (last 30 days)
Jasmina Pelivani
Jasmina Pelivani on 20 Apr 2015
Commented: Eric on 21 Apr 2015
I am trying to register a stack of images to one fixed image, using imregister. I have 9 pictures and sometimes, the registration seem totally wrong. I use the following code:
tformEstimate = imregcorr(img_1, img_2);
[optimizer, metric] = imregconfig('monomodal');
movingRegistered = imregister(img_2, img_1, 'affine', optimizer, metric,'InitialTransformation',tformEstimate);
Some of the images totally flip, which is weird, since the images are almost the same. I notice if I do not use the transformation matrix, I get the result of the "flipped" image. So my thought is that maybe the transformation is not used. Is there any way to check for this?
I am doing this in order to perform a focus stacking after aligning the images.

Answers (1)

Eric on 20 Apr 2015
Edited: Eric on 20 Apr 2015
One obvious problem: You have flipped the order of img_1 and img_2 in your two registration function calls. In your usage of imregcorr you are registering to img_2. In your usage of imgregister you are registering to img_1. So the initial estimate you are providing is completely wrong.
You might also try setting the 'DisplayOptimization' parameter of imregister to true. My guess is that the first objective function value is quite high. After correcting the problem I described in the last paragraph, the initial objective function value should be considerably lower. That would be an indication that the tformEstimate is being used.
Eric on 21 Apr 2015
One thing you might try is registering the gradient magnitudes of the images rather than the images themselves. I've generally done simple registrations (usually just translation and sometimes rotation), but this has often helped. You can calculate the gradient magnitude of an image via
[FX,FY] = gradient(image);
gradient_magnitude = sqrt(FX.^2 + FY.^2);
Alternatively, since you clearly have the Image Processing Toolbox, you can use imgradient(). In my experience the gradient magnitude works better than Canny edge detection, but you can try that as a pre-processing step as well.
You might also try using the routine at to get the translation close initially, then run more advanced routines.
Another approach entirely is available if you have the Computer Vision System Toolbox. See the functions at The idea here would be to call a function to detect features and then use the matchFeatures function followed by the estimageGeometricTransform function. Depending on the nature of your imagery this may be a better approach.
Good luck,

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!