# align time-lapse images to remove translation and rotation

I have a sequence of images that you can get as a stack from here:

https://www.dropbox.com/s/8ds08klvq9irgz9/imagestack.tif?dl=0

Now i have the following image sequence where we can see that the white blob moves a lot

I want to align the images with each other such that the translation and rotation is minimized. I used the following code below:

(* masks are the sequence of images *)
FoldList[ImageAlign[#1, #2, Method -> "Fourier", TransformationClass -> "Rigid"] &,


the result is:

I think there should be a way to improve it more such that translation and especially rotations are minimized. Would appreciate help !

• You're aligning each image with the next, thus accumulating alignment errors. Have you tried aligning all images with the same template, e.g. the first image? – Niki Estner Jun 5 '17 at 19:10
• @nikie thanks. Will try and let you know. Btw if your scheme works then please post as an answer that I can accept – Ali Hashmi Jun 5 '17 at 19:29
• @nikie btw the first image is pretty circular and the last pretty much like an egg. Will there be alignment issues? – Ali Hashmi Jun 5 '17 at 19:31
• @nikie actually setting the first image as reference did a very poor job. – Ali Hashmi Jun 5 '17 at 20:22

This is only a partial answer. (But I'm not convinced one can determine the rotation angle unless one knows more information about either the shape of the object or the rotation speed function.)

Edit: While I'm still not convinced there is enough information to determine the rotation angle - in part because the shape sometimes looks very circular - I've included the use of the "Orientation" option that assumes the shape of an ellipse.

(* Import the images *)
images = Import["imagestack.tif"];

(* Find the centroid and possible rotation angle (assuming an ellipse) *)
centroids =
Table[ComponentMeasurements[Binarize[images[[i]]], "Centroid"], {i, Length[images]}];
angles = Table[ComponentMeasurements[Binarize[images[[i]]], "Orientation"],
{i, Length[images]}];
a = 1 /. # & /@ angles;

(* Center the graphic at the centroid, trim, and rotate *)
w = 150;
centeredImages = Table[ImageRotate[ImageTrim[images[[i]],
{(1 /. centroids[[i]]) - w, (1 /. centroids[[i]]) + w}], -a[[i]]],
{i, Length[centroids]}];

(* Export as an animated gif *)
Export["objects.gif", Flatten[{centeredImages[[1 ;; Length[centeredImages] - 1]],
Reverse[centeredImages]}], "DisplayDurations" -> 0.5]


• +1 but close enough but the point is that ImageAlign should be used to remove the rotation – Ali Hashmi Jun 6 '17 at 8:55
• ImageAlign does do some odd (meaning "unexpected to me") things: ImageAlign[centeredImages[[1]], centeredImages[[2]]]. But maybe some other preprocessing would help. This is one of the many areas of Mathematica for which I'm just about clueless. – JimB Jun 6 '17 at 14:47