What is the correct way of using setBatchmode to process multiple images?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

What is the correct way of using setBatchmode to process multiple images?

Chun Han
Dear all,

I am new to ImageJ macro, so this question may seem simple to many of you.  I have a macro to process all images in a folder. It runs fine when setBatchMode(false). However, when I use setBatchMode(true), it stops before doing anything with my images. Below is a block of codes in this macro that may help diagnosis.

When I run this macro, I receive the message "The Field ROI does not exist. The macro will now quit!". My reasoning is that when setBatchMode(true), there is no active window. So in this case run("To ROI Manager") won't transfer overlays of the opened image to the ROI Manager, because ROI Manager is not even open. If I open the ROI Manager before I run the macro, it will continue until stopping at a later step where the macro selects a window. My macro has many steps of run("Duplicate...", "xxxx"), selectWindow("xxxx"), and run("Close"). So my feeling is that setBatchMode(true) makes some windows invisible to the program so that the processing cannot continue.

Please correct me if my reasoning is wrong. If it is right, how should I use the BatchMode correctly? Is there any way I can still use selectWindow command? If not, what is the best alternative?

Many thanks for your help!

Chun


----------------------------------------------------------------------------------------------------------
    ......
    setBatchMode(true);

    //Select the folder containing images.
    dir = getDirectory("Choose the folder containing preprocessed images.");
    list = getFileList(dir);
    print("-- Processing folder: " + dir + " --");


    //Processing loop.
    for (i=0; i<list.length; i++) {
        showProgress(i+1, list.length);
        print("-- Processing file: " + list[i] + " --");
        open(dir+list[i]);
        imageTitle= getTitle();
        newTitle = substring(imageTitle, 0, lengthOf(imageTitle)-4);

        //Importing and defining ROIs.
        run("To ROI Manager");
        FieldIndex = "null";
        ROIIndex = "null";
        ROICount = roiManager("count");
        OriginalROICount = ROICount;
        for (j=0; j<ROICount; j++) {
            roiManager("select", j);
            if (startsWith(Roi.getName, "Field")==1) FieldIndex = j;
            if (startsWith(Roi.getName, "ROI")==1) ROIIndex = j;
        }
        if (FieldOption == 1 &&amp; isNaN(FieldIndex)) {
            print("The Field ROI does not exist. The macro will now quit!");
            exit();
        }
        if (ROIOption == 1 &&amp; isNaN(ROIIndex)) {
            print("ROI does not exist. The macro will now quit!");
            exit();
        }
        if (FieldOption == 0) {
            run("Select All");
            roiManager("Add");
            FieldIndex = roiManager("count")-1;
        }
        ......

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: What is the correct way of using setBatchmode to process multiple images?

Rasband, Wayne (NIH/NIMH) [E]
> On Jun 16, 2016, at 2:29 PM, Chun Han <[hidden email]> wrote:
>
> Dear all,
>
> I am new to ImageJ macro, so this question may seem simple to many of you.  I have a macro to process all images in a folder. It runs fine when setBatchMode(false). However, when I use setBatchMode(true), it stops before doing anything with my images. Below is a block of codes in this macro that may help diagnosis.

The latest ImageJ daily build (1.51e4) fixes a bug that caused the run("To ROI Manager”) macro function to fail in batch mode. This code reproduces the bug:

  setBatchMode(true);
  newImage("Untitled","8-bit black",500,500,1);
   for (i=0; i<10; i++) {
     makeOval(i*40, i*40, 50, 50);
     Overlay.addSelection;
  }
  run("To ROI Manager");
  n = roiManager("count");
  print("count="+n+" (expected count=10)");

> When I run this macro, I receive the message "The Field ROI does not exist. The macro will now quit!". My reasoning is that when setBatchMode(true), there is no active window. So in this case run("To ROI Manager") won't transfer overlays of the opened image to the ROI Manager, because ROI Manager is not even open. If I open the ROI Manager before I run the macro, it will continue until stopping at a later step where the macro selects a window. My macro has many steps of run("Duplicate...", "xxxx"), selectWindow("xxxx"), and run("Close"). So my feeling is that setBatchMode(true) makes some windows invisible to the program so that the processing cannot continue.
>
> Please correct me if my reasoning is wrong. If it is right, how should I use the BatchMode correctly? Is there any way I can still use selectWindow command? If not, what is the best alternative?

The selectWindow() function works in batch mode even though windows are not visible.

-wayne

> ----------------------------------------------------------------------------------------------------------
>    ......
>    setBatchMode(true);
>
>    //Select the folder containing images.
>    dir = getDirectory("Choose the folder containing preprocessed images.");
>    list = getFileList(dir);
>    print("-- Processing folder: " + dir + " --");
>
>
>    //Processing loop.
>    for (i=0; i<list.length; i++) {
>        showProgress(i+1, list.length);
>        print("-- Processing file: " + list[i] + " --");
>        open(dir+list[i]);
>        imageTitle= getTitle();
>        newTitle = substring(imageTitle, 0, lengthOf(imageTitle)-4);
>
>        //Importing and defining ROIs.
>        run("To ROI Manager");
>        FieldIndex = "null";
>        ROIIndex = "null";
>        ROICount = roiManager("count");
>        OriginalROICount = ROICount;
>        for (j=0; j<ROICount; j++) {
>            roiManager("select", j);
>            if (startsWith(Roi.getName, "Field")==1) FieldIndex = j;
>            if (startsWith(Roi.getName, "ROI")==1) ROIIndex = j;
>        }
>        if (FieldOption == 1 &&amp; isNaN(FieldIndex)) {
>            print("The Field ROI does not exist. The macro will now quit!");
>            exit();
>        }
>        if (ROIOption == 1 &&amp; isNaN(ROIIndex)) {
>            print("ROI does not exist. The macro will now quit!");
>            exit();
>        }
>        if (FieldOption == 0) {
>            run("Select All");
>            roiManager("Add");
>            FieldIndex = roiManager("count")-1;
>        }
>        ......


--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html