ImageJ Macro for converting multichannel Tiffs to Tiffs with only specified channels

2k views Asked by At

I have a quite simple programming question I was hoping somebody could help me with.

I'm working with Tiff files with several channels (all contained in a .lif file, which is Leica format). I want a way to easily convert all my Tiffs to Tiffs containing only a few of the channels (which I specify). Right now I'm doing it manually, for each image and it is tedious. I have no experience in writing macros and some help or a starting point would be much appreciated. I'm sure it's not a complicated macro to write.

As of now I'm using the following manual routine and commands after I have opened all my Tiffs:

  1. Image > Stacks > Stack to Images - separates the stacked imaged into individual images
  2. Close images I dont wan't to be in stack.
  3. Image > Stacks > Images to Stack - Returns the remaining images to a stack and renames it.
  4. Image > Hyperstacks > Stack to Hyperstack - here I change it so that the image has 3 channels.
  5. Save the new Tiff with the desired channels and name.
  6. Close the Tiff and repeat for all Tiffs.

What I want is a Macro that loops the above steps for all open Tiffs, letting the user specify the channels (eg. keep channels: 2,3 and 5). I know it's a very simple programming task, but I could really use some help getting it done.

Thanks! Johannes

2

There are 2 answers

1
Jan Eglinger On

There are several less complex possibilities to create a stack with only a subset of channels:

  • Image > Stacks > Tools > Make Substack..., which lets you specify the channels/slices, and gets recorded as:

    run("Make Substack...", "channels=1,3-5");
    
  • Image > Duplicate..., where you can select a continuous range of channels, such as:

    run("Duplicate...", "duplicate channels=1-5");
    

To apply this procedure to all images in a folder, have a look at the Process Folder template in the Script Editor (Templates > IJ1 Macro > Process Folder) and at the documentation on the Fiji wiki:

1
Johannes Holm On

Thanks for the help Jan Eglinger, back from vacation I managed to write the macro, which was simple with your help :) Based on the template it looks like this (I just gave them incremental names which is fine for my purpose, but could be made more allround i guess):

/*
 * Macro to for converting multichannel Tiffs to Tiffs with only specified channels, processes multiple images in a folder
 */

input = getDirectory("Input directory");
output = getDirectory("Output directory");

Dialog.create("File type");
Dialog.addString("File suffix: ", ".tif", 5);
Dialog.show();
suffix = Dialog.getString();

processFolder(input);

function processFolder(input) {
    list = getFileList(input);
    for (i = 0; i < list.length; i++) {
        if(File.isDirectory(input + list[i]))
            processFolder("" + input + list[i]);
        if(endsWith(list[i], suffix))
            processFile(input, output, list[i]);
    }
}

function processFile(input, output, file) {
    open(input + file);
    print("Processing: " + input + file);
    run("Make Substack...", "channels=1,2,4"); //Specify which channels should be in the final tif
    print("Saving to: " + output);
    saveAs("Tiff", output + i);
    close("*");
}