imaging articles code

ImageJ macro to synchronize and combine image stacks

When I film embryos under the microscope, some will be younger and some will be older than others—they are never in perfect synchrony.

This is fine when watching the recordings of individual embryos, but becomes an issue when you want to watch two (or more) embryos developing side-by-side.

In my case, I want to identify the differences between a wild type and a mutant embryo, and make a video where these differences can be easily visualized by my peers.

To synchronize and combine image stacks, I use ImageJ. I can trim individual recordings so that the embryos begin at the same developmental stage, and can easily combine the synchronized stacks together.

Doing this manually is fine, but it gets time-consuming when you have to do it over and over again. So, I wrote a small macro to help me to synchronize and combine two image stacks together: SyncAndCombineStacks.ijm.

Hope it helps you too!

ImageJ macros save time

Combining movies in Fiji/ImageJ is straightforward. You can simply use the Combine... command. But synchronizing is way harder. It depends on human classification and involves some calculations and stack juggling that can (and will) become tedious. A macro save us time and sanity. Follow below to see how it works.

Combined movies without syncing

That’s what unsynchronized movies look like. I combined them fresh off the microscope without any synchronization:

Two embryos of the fruit fly Drosophila melanogaster. Both were acquired in the same microscopy session. The top embryo is older than the bottom embryo.

Combined movies after syncing

Here are the same two movies now synchronized by the embryonic stage:

The same two embryos are now synchronized.

How it works

The macro performs the hard work. It calculates how many frames to trim from each stack. Then it duplicates the selected range of frames common to both stacks. Finally, it combines the synchronized recordings into a single image stack. All you need to do is to select the corresponding frames between the two stacks.

Step-by-step instructions

Here are the instructions step-by-step:

  1. Open both image stacks in ImageJ.
  2. Adjust the contrast if needed (before running the macro).
  3. Select a reference frame in the top stack (e.g. stage easy to recognize).
  4. Select the correspondent frame in the bottom stack.
  5. Run the macro and fill in the dialog parameters.
  6. Click OK, wait a few seconds, and check if the synchronization is good. Otherwise, re-run with different parameters.


I’ve also recorded a small screencast:

Note! The macro does not touch the original stacks, but it outputs an RGB Color stack. There are a couple of reasons for that. Converting to RGB avoids contrast issues when the stacks have different pixel intensities. It also prevents quirks in video players that can’t handle 16-bit movies. But if you need to perform image analyses on the final stack, remove this option. I may add a checkbox for that in the future.

2 replies on “ImageJ macro to synchronize and combine image stacks”

Reply by Email


Leave a Comment

Your email address will not be published. Required fields are marked *