0

I'm trying to run through a folder in BatchMode but even when I set it to setBatchMode(true) (or setBatchMode("hide")) it still opens the image before setBatchMode("show") and takes forever to open and process.

It runs "faster" when I don't use BatchMode but it still takes forever to process one image... and i have many to get through

/*
 * Macro template to process prepare images in a folder
 */


#@ File (label = "image input directory", style = "directory") imageinput
#@ File (label = "Output", style = "directory") output

processFolder(imageinput);

table_name = "approval table";
Table.create(table_name);
setBatchMode(true)

// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(imageinput) {

    imagesList = getFileList(imageinput);
    
    imagesList = Array.sort(imagesList);
    
    for (i = 0; i < imagesList.length; i++) {
        imagesFilename = substring(imagesList[i], 0, lastIndexOf(imagesList[i], "."));
    
        currentimage = imageinput + File.separator + imagesList[i];
        
        processFile(imagesFilename);
    }
}

function processFile(imagesFilename) {

open(currentimage);
run("Select All");

run("Set... ", "zoom=200 x=8640 y=5400");
run("Subtract Background...", "rolling=1 light sliding disable");

run("Brightness/Contrast...");
setMinAndMax(186, 249);
call("ij.ImagePlus.setDefault16bitRange", 8);
run("Apply LUT");

setBatchMode("show")
waitForUser("User to Approve");   //here i check that that i'm okay with the contrast/brightness etc and click approve
     
current_image = getTitle(); //here i add image name to a list if i think i need to go back and manually review it
    approve_review = getBoolean("review?", "yes", "no");
    if (approve_review == true) {
        approve_review_text = "yes";
    }
    else {
        approve_review_text = "no";
    }
    s = Table.size; // gets the size (lines) of the current table
    Table.set("image", s, current_image);
    Table.set("review", s, approve_review_text);
    Table.update;
    
    wait(100) ;
    
setBatchMode("hide")

selectWindow("Results");
saveAs("Text", output + File.separator + "approval_table.csv");
 
saveAs("Tiff", output + File.separator + imagesFilename + ".tif");
close();

}

Edit: as someone kindly beat me to it - I was given the solution in the imageJ forum where setBatchMode(true) needs to go right after selecting the directory.

I've also solved the speed issue -

run("Collect Garbage");

after closing the image

DS14
  • 133
  • 9

1 Answers1

-1

This request was cross-posted to the "Image.sc-Forum" and received an answer: https://forum.image.sc/t/setbatchmode-true-still-opening-images-before-i-want-them-to/74023

In any case

setBatchMode(true);

must preced

processFolder(imageinput);

However, the OP experiences an additional slowdown by this change...

Herbie
  • 143
  • 5
  • A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it is there, then quote the most relevant part of the page you are linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted.](/help/deleted-answers) – Daniel Widdis Nov 17 '22 at 15:08