imagej / ImageJ

Public domain software for processing and analyzing scientific images
http://imagej.org
Other
513 stars 218 forks source link

Macro commands getTitle() does not work properly when setBatchmode(true) is active #196

Closed Sethur closed 1 year ago

Sethur commented 1 year ago

When opening two or more images in a macro loop and doing any operation on them, such as getTitle(), or run("Reslice..."), only the first image is selected by selectImage(i) when batch mode is entered before the loop.

Here is a short macro the reproduce the problem.

newImage("First", "8-bit noise", 512, 512, 10);
newImage("Second", "8-bit noise", 512, 512, 10);
newImage("Third", "8-bit noise", 512, 512, 10);

setBatchMode(true); // Works as intended if this line is commented out.

orgImgNo = nImages
for (i = 1; i <= orgImgNo; i++) {
    selectImage(i);
    title = getTitle();

    IJ.log("Title of image " + i + ": " + title);

    run("Reslice [/]...", "start=Top avoid");  // This is needed to reproduce the problem
}

setBatchMode("exit and display");

Edit: Updated code and description to provide an closed, working example, that does not need any already opened images. As stated in the comments, it is important for reproduction that the images are already open when the batch mode is entered.

rasband commented 1 year ago

I am not able to reproduce this problem. Could you please share a runnable macro that reproduces the problem. Something like the following macro, which works as expected.

  setBatchMode(true);
  newImage("First", "8-bit noise", 500, 500, 1);
  newImage("Second", "8-bit noise", 500, 500, 1);
  for (i = 1; i <= nImages; i++) {
     selectImage(i);
     IJ.log( getTitle() );
  }
Sethur commented 1 year ago

@rasband I found that the error only happens when you do some operation to the image inside the loop. For me, it happened when trying to reslice all images. A minimum runnable example to reproduce this error is:

newImage("First", "8-bit noise", 500, 500, 10);
newImage("Second", "8-bit noise", 500, 500, 10);
newImage("Third", "8-bit noise", 500, 500, 10);

setBatchMode(true);

orgImgNo = nImages
for (i = 1; i <= orgImgNo; i++) {
    selectImage(i);
    title = getTitle();

    IJ.log("Title of image " + i + ": " + title);

    run("Reslice [/]...", "start=Top avoid");
}

setBatchMode("exit and display");

This works as indented if the first line is commented out, but if setBatchMode(true) is active, both getTitle() and the reslice operation work on the first image, instead of the i-th image.

Edit: Fixed the formatting of the code and included newImage for convenience. Edit 2: Moved the newImage commands before the setBatchMode(true) line. If the newImage commands are after setBatchMode(true), the error is not happening and everything works fine.

rasband commented 1 year ago

This bug is fixed in the ImageJ 1.54c2 daily build.