Something that has driven me nuts for a while is the bug in FIJI/ImageJ when making montages of image stacks. This post is about a solution to this problem.
What’s a montage?
You have a stack of images and you want to array them in m rows by n columns. This is useful for showing a gallery of each frame in a movie or to separate the channels in a multichannel image.
What’s the bug/feature in ImageJ?
If you select Image>Stacks>Make Montage… you can specify how you want to layout your montage. You can specify a “border” for this. Let’s say we have a stack of 12 images that are 300 x 300 pixels. Let’s arrange them into 3 rows and 4 columns with 0 border.
So far so good. We have an image that is 1200 x 900. But it looks a bit rubbish, we need some grouting (white pixel space between the images). We don’t need a border, but let’s ignore that for the moment. So the only way to do this in ImageJ is to specify a border of 8 pixels.
Looks a lot better. Ok there’s a border around the outside, which is no use, but it looks good. But wait a minute! Check out the size of the image (1204 x 904). This is only 4 pixels bigger in x and y, yet we added all that grouting, what’s going on?
The montage is not pixel perfect.
So the first image is not 300 x 300 any more. It is 288 x 288. Hmmm, maybe we can live with losing some data… but what’s this?
The next image in the row is not even square! It’s 292 x 288. How much this annoys you will depend on how much you like things being correct… The way I see it, this is science, if we don’t look after the details, who will? If I start with 300 x 300 images, it’s not too much to ask to end up with 300 x 300 images, is it? I needed to fix this.
Solutions
I searched for a while for a solution. It had clearly bothered other people in the past, but I guess people just found their own workaround.
ImageJ solution for multichannel array
So for a multichannel image, where the grayscale images are arrayed next to the merge, I wrote something in ImageJ to handle this. These macros are available here. There is a macro for doing the separation and arraying. Then there is a macro to combine these into a bigger figure.
Igor solution
For the exact case described above, where large stacks need to be tiled out into and m x n array, I have to admit I struggled to write something for ImageJ and instead wrote something for IgorPRO. Specifying 3 rows, 4 columns and a grout of 8 pixels gives the correct TIFF 1224 x 916, with each frame showing in full and square. The code is available here, it works for 8 bit greyscale and RGB images.
I might update the code at some point to make sure it can handle all data types and to allow labelling and adding of a scale bar etc.
—
The post title is taken from “Everything In Its Right Place” by Radiohead from album Kid A.
After sharing this blog post, a couple of people told me that Wayne Rasband (creator of ImageJ) would happily patch it. I should just write to the list.
Sure enough:
“This bug that caused the Image>Stacks>Make Montage command to not work as expected if “Border Width” was greater than zero is fixed in the latest ImageJ daily build (1.51e19).
-wayne”
If you want to join the ImageJ list the link is https://imagej.nih.gov/ij/list.html