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


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

enter image description here

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"] &,
First@masks, Rest@masks]

the result is:

enter image description here

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

  • $\begingroup$ 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? $\endgroup$ – Niki Estner Jun 5 '17 at 19:10
  • $\begingroup$ @nikie thanks. Will try and let you know. Btw if your scheme works then please post as an answer that I can accept $\endgroup$ – Ali Hashmi Jun 5 '17 at 19:29
  • $\begingroup$ @nikie btw the first image is pretty circular and the last pretty much like an egg. Will there be alignment issues? $\endgroup$ – Ali Hashmi Jun 5 '17 at 19:31
  • $\begingroup$ @nikie actually setting the first image as reference did a very poor job. $\endgroup$ – 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]

Centered trimmed and rotated images

  • $\begingroup$ +1 but close enough but the point is that ImageAlign should be used to remove the rotation $\endgroup$ – Ali Hashmi Jun 6 '17 at 8:55
  • $\begingroup$ 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. $\endgroup$ – JimB Jun 6 '17 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.