Automatically Crop Registered Image Stack

Hi everybody,

I recently wrote a jython script for ImageJ/FIJI to automatically crop registered image stacks (to get rid of moving black borders around the edges). This is a problem that I come across pretty often in live microscopy, so I thought it might be useful to post it here. For example, say you have an image stack that needs to be registered, like this one:

To fix the drift in this image, you can register using one of a number of plugins in ImageJ/FIJI, such as StackReg:

Now it’s registered, which is great. But now you have these black edges around the outside that move around. This is not so nice for presenting data and can also interfere with later steps in your analysis pipeline. You could crop the image by hand, but if you have to do this for a lot of images it can be very time consuming and annoying. With this script, you can crop the images automatically to the largest rectangle that does not contain any black edges:

stk_adj_reg_crop

The script makes use of the largest rectangle/largest histogram algorithm, which is O(n), so it’s pretty fast to do the cropping. You can just drop it in the plugins folder and use it as a plugin or in a macro using run("Autocrop Black Edges"). You can find the script and instructions for use on my github.

Please let me know if this is useful or if you run into any problems or unexpected behavior. I tested this on some of my own images, but the testing was by no means exhaustive, so it may not work in all cases in its current form. Feel free to open an issue on github if it isn’t working on your images.

4 Likes