MATLAB Answers


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

Asked by Jasmina Pelivani on 20 Apr 2015
Latest activity Commented on by 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.


Sign in to comment.

1 Answer

Answer by Eric
on 20 Apr 2015
Edited by 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.


I discovered the problem in imregcorr after some time I posted the question. The change in the order fixed the picture which was wrong, but it resulted in some other images giving some weird transformations instead.
The MSE is relatively high, but it gets slowly lower after the iterations.
How can it be that some of the images do this? Is there any other parameter I can alter to get another result?
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.