Merge image different to done by hand


I am trying to merge images of a dead/live coloring.
I am doing it, based on the guide shown by Duke University:


  1. Open the images in ImageJ
  2. Adjust the contrast if neceesary: Image/Adjust/BrightnessConstrast
  3. Change or check all Images are in 8-bit format. Image/Type/8-bit
  4. Image/Color/Merge Channels and the Merge Channels box will appear
  5. Select the fluorescent images in the appropriate R, G and B channels and the DIC or similar image in gray channel. Select “Create Composite” and click “OK”
  6. Click on the new ‘composite’ image to activate it
  7. Image/Type/RGB Color
  8. Save it as “Tiff” file

When I do it by hand like the guide tells, I get as result the following image:

Composite (RGB).tif (14.4 MB)

I found a script in the forum Topic that I modified a little.

macro "batch_merge_channels"{
    file1 = getDirectory("Dead");
    //you can change the name of the inquiry to whatever wavelength you need, e.g. CY5 
    list1 = getFileList(file1);
    //gets the list of files in the folder DAPI
    n1 = lengthOf(list1);
    //gets the number of files in folder DAPI, it should be the same as the number 
    //of files in folder GFP
    file2 = getDirectory("Live");
    list2 = getFileList(file2); 
    file3 = getDirectory("Live-And-Dead");
    //the output folder. When started first the number of files is 0
    list3 = getFileList(file3);
    n2 = lengthOf(list3);

    small = n1;
    //condition for for-loop

    for(i = n2 + 1; i < small; i++) {
      //i will always follow the aborted number of merges, you might not have the problem, 
      //but with small memory and a huge set of images it is useful
      name = list2[i];
      //not to lose your track, though you can change it to anything else
      open(file1 + list1[i]);
      open(file2 + list2[i]);
      run("Merge Channels...", "c1=[" + list1[i] + "] c2=[" + list2[i] + "] keep ignore");
      run("RGB Color");
      saveAs("tiff", file3 + name);

The folder “Dead” should be on the red and “Live” on the green channel.
When I let run the script, I get as result the following image:

007__2.tif (14.4 MB)

Can someone tell me why I got different pictures as a result, and how I can modify the script, that the result is the same as doing it by hand?

Here are the basic images:
for red:
007__2.tif (9.3 MB)
for green
007__2.tif (5.9 MB)

By the way, there is a second problem in the script. The first result of each folder (Dead or Live) will not be saved. e.g. in each folder are 4 images (1.tif, 2.tif… ) the result will only be 2.tif, 3.tif and 4.tif. The number 1 in that case will not be done. Any ideas?

Thanks for your help.

1 Like

Hey @peleroform ,

when you record macro code (Menu Plugins > Macros > Record) while doing the procedure manually, you can read the command which correspond to your actions and compare them to the other script. For example, the brightness-contrast adaptation step is not part of the macro you posted.

Check the starting condition in the for loop: Why does it start at i = n2+1 and not at n = 0?

Let us know if this helps!


Thanks @haesleinhuepf .

I did the modification based on the record macro code.

The code is:

run("Merge Channels...", "c1=007__2.tif c2=007__2-1.tif create keep");
run("RGB Color");
saveAs("Tiff", "e:/imgs/Composite (RGB).tif");

The result is shown in the first post.

I did not modify anything on the image like contrast or so.

for the second… the i=n2+1 and not n=0 I will test later. Thanks for the tip.


I found the problem for the different results.

In the two folders are the names of the files the same. In that case ImageJ opens one file with the prefix “-1”. I do not know why the script brings a different result if the files are named the same - and in different folders, but, if I rename one set of files in one folder… the result is the same as doing it by hand.

and if there is i = n2 and not i=n2+1, all images are be handled.

Thanks for the help.

1 Like