Shuffled Image overlay

I have been using macros for imageJ to perform image analysis for about a year now. After the last update, all of my codes that incorporated batch mode began shuffling up image orders when using the overlay command (for example: Image 1a is suddenly being overlaid with Image 3c).

It seems to be systematically switching up files too so that if I run two different codes on the same sets of images the mismatch is maintained between codes. However, if I switch between files (which contain sets of images), the pattern by which the images are shuffled changes.

For example, I have images from 9Feb18. Images 1a through 1c overlay perfectly. However, at image 2a, image 3c is overlaid. Image 2b is then overlaid with 3b, 2c with 3a, 3a with 2c and so on. Meanwhile for 13Oct17 the image 1a is mismatched while 1B is matched properly. I have verified that these patterns are maintained in two independent codes, although the coding for their batch processing is near identical.

I am running Java 1.8.0_66 and ImageJ 2.0.0 on a Mac OS X system.

Below is a copy of a minimized version of my code , which I have verified creates the same problem (although interestingly enough, it seems to create its own shuffling pattern, whereas my other two codes share a pattern). My apologies if it isn’t the prettiest or most logical. Everything I know was learned from writing the above mentioned codes.

I will also attach the same images that I have been using (new users can only add 5 images, the but the images sometimes stay in order if only three or less are used). The images will need to be put in a folder by category (background images: contain BF in their title; and overlay images: contain GFP in their title). A separate folder is also needed to save the overlaid images. They can then be compared to the images you overlaid for order.

overlayQ = (getBoolean("Would you like to Overlay images?"));

    if(overlayQ == true)
          
      {
          title = "Instructions";
          msg = "Select the background images";
          waitForUser(title, msg);

              base_image_directory = getDirectory("Choose Source Directory ");

          title = "Instructions";
          msg = "Select the File Containing the images you want to overlay";
          waitForUser(title, msg);

              overlay_image_directory = getDirectory("Choose Source Directory ");

          title = "Instructions";
          msg = "Select the File where you would like to save the images after you have edited them";
          waitForUser(title, msg);

              destination_directory = getDirectory("Choose Destination Directory ");


requires("1.34m");
    title = "Untitled";
    width=1050; height=512;
    Dialog.create("Title Adjustment");
    Dialog.addString("Create a label to distinguish the edited image from the original:", "Edited");
    Dialog.addCheckbox("Yes?", true);
    Dialog.show();

    name= Dialog.getString();


    base_images = getFileList(base_image_directory);
      overlay_images = getFileList(overlay_image_directory);

    setBatchMode(true);
      for (i=0; i < base_images.length; i++) 
  {
      showProgress(i+1, base_images.length);
             
        open(base_image_directory+base_images[i]);
        base_image_id = getImageID();
      
        open(overlay_image_directory+overlay_images[i]);
          overlay_image = overlay_images[i];

        selectImage(base_image_id);


        run("Add Image...", "image=&overlay_image x=0 y=0 opacity=75");
             
        run("Flatten");
      
                            saveAs("TIFF", destination_directory+name+base_images[i]);

              }
              }
1 Like

Background Images (1-5)

9feb18_HAP1_75K_18H_BF_20X_2a9feb18_HAP1_75K_18H_BF_20X_1c9feb18_HAP1_75K_18H_BF_20X_1b9feb18_HAP1_75K_18H_BF_20X_2b9feb18_HAP1_75K_18H_BF_20X_1a

Background Images (6-9)

9feb18_HAP1_75K_18H_BF_20X_3b9feb18_HAP1_75K_18H_BF_20X_3c9feb18_HAP1_75K_18H_BF_20X_3a9feb18_HAP1_75K_18H_BF_20X_2c

Overlay Images (1-5)

9feb18_HAP1_75K_18H_GFP_20X_1b9feb18_HAP1_75K_18H_GFP_20X_1c9feb18_HAP1_75K_18H_GFP_20X_2a9feb18_HAP1_75K_18H_GFP_20X_2b9feb18_HAP1_75K_18H_GFP_20X_1a

Overlay Images (6-9)

9feb18_HAP1_75K_18H_GFP_20X_2c9feb18_HAP1_75K_18H_GFP_20X_3b9feb18_HAP1_75K_18H_GFP_20X_3c9feb18_HAP1_75K_18H_GFP_20X_3a

Hi Ryan,

Welcome to the forums!

Can you insert the following code in to your macro after you use the getFileList function for both directories:

Array.show(base_images);
Array.show(overlay_images);
waitForUser("Check file lists");

I get the following:

As far as I’m aware, that is the order that your images will be loaded in starting from 0 (first file in the list). For me, the macro works as expected.

Maybe your file order is incorrect for some reason?

Best,

Rob

3 Likes

Hi Rob,

Thanks for the help, it seems to be that my files aren’t in order.

I’m not sure exactly how to fix this though. This is how the files appear in the directory:

This should be the correct order, although I suppose how this order is viewed can be changed easily. How to I alter the order in which the files are opened? I haven’t moved or changed any settings related to these files between the time that the code was working and now.

Sincerely,
McCool

Hi Ryan,

I always use the macro function Array.sort(array) after I use the getFileList function. My experience is that the fileorder is shuffled often especially when the folder contains many images.
I hope this will help you!

Regards, Peter

2 Likes

I tried using the array.sort(array) function, but I was still having the same issue. I ultimately decide to just delete my copy of FIJI and redownload it. The minimal program is working again and I will check the full program tomorrow. Something must have downloaded improperly when I updated last.

@PeterRZ and @7rebor thank you very much. I appreciate the help more than I can express over a screen.

Sincerely,
McCool

1 Like

It appears that this problem has also been encountered in the ImageJ mailing list here. Something to do with the new Mac OS not sorting files, updating your ImageJ might have fixed it.

Glad you solved it!

Rob

1 Like