0

I'm currently trying to batch process a lot of lab images at once using ImageJ/Fiji but I'm having a hard time making it process the images. Whenever a file is pulled up, there are three images which are split into channels and open in separate windows . During the processing, each window is selected and needs to be processed differently. I am currently trying to figure out how to make the program select each different window based on a specific parameter rather than the exact name of the window. Each of the windows will end in "C=0", "C=1", or "C=2". I want the code to select the windows that contain the string "C=0", "C=1", or "C=2" in them but I can't seem to get it to work. As of now, it only runs through the first file but not the rest. The current code I'm running looks like this.

open("/Users/name/Desktop/name of file");
selectWindow("name of window - C=1");
setOption("ScaleConversions", true);
run("8-bit");
setAutoThreshold("Default");
//run("Threshold...");
//setThreshold(0, 10);
setOption("BlackBackground", true);
run("Convert to Mask");
run("Convert to Mask");
run("Analyze Particles...");
close();
run("Close");
selectWindow("name of window - C=0");
setOption("ScaleConversions", true);
run("8-bit");
setAutoThreshold("Default");
//run("Threshold...");
//setThreshold(0, 20);
run("Convert to Mask");
run("Convert to Mask");
run("Analyze Particles...", "size=20-700 show=Overlay display summarize add composite");
run("Analyze Particles...");
roiManager("Show None");
roiManager("Show All");
run("Close");
close();
run("Close");
selectWindow("name of window - C=2");
setOption("ScaleConversions", true);
run("8-bit");
setAutoThreshold("Default");
//run("Threshold...");
//setThreshold(0, 4);
run("Convert to Mask");
run("Convert to Mask");
run("Analyze Particles...");
saveAs("Results", "/Users/name/Desktop/results/Summary.csv"); 

Any and all help would be super appreciated. Thank you.

  • I don't completely understand with what you need help. So the code works fine you just want to run it on multiple files? – Petra Jul 22 '22 at 07:01

1 Answers1

0

If you want to process several files without manually opening each, it's convenient to put all files in the same folder, and then write a for loop.

Here's an example in the ImageJ macro language, based on the code you provided:

// Edit these variables to have your folder paths
inputfolder = "MY_INPUT_FOLDER";
imagefolder = "MY_IMAGE_FOLDER";
outputfolder = "MY_OUTPUT_FOLDER";

// Find out how many files are in the folder
list = getFileList(inputfolder + imagefolder);
num2process = list.length;

// Loop through all files in the folder
for (l = 0; l < num2process; l++) 
{
    open(inputfolder + imagefolder + list[l]);
    
    // After opening, the image is in focus, so get its properties
    filename = getInfo("image.filename");
    current_title = getTitle(); 
    current_image = getImageID();
    selectImage(current_image);   // this puts the image in focus
    
    // With the image selected, process it
    run("Split Channels");
    
    // The channels will have the following names:
    channel1 = "C1-" + current_title; 
    channel2 = "C2-" + current_title; 
    channel3 = "C3-" + current_title;

    // Put them in an array for convenience
    array = newArray(channel1, channel2, channel3); 
    
    // If the processing steps are the same, it's convenient to loop through each channel
    for (channel = 0; channel <=2; channel++) 
    {
        // Select the right channel
        image = array[channel];
        selectWindow(image);
        
        // Apply image processing
        setOption("ScaleConversions", true);
        run("8-bit");
        setAutoThreshold("Default");
        run("Convert to Mask");
        run("Analyze Particles...", "size=20-700 show=Overlay display summarize add composite");    
        run("Close");
    }
    
    // Save the results and name the file appropriately
    string = outputfolder + filename + "_summary.csv";
    saveAs("Results", string);
}

Otri
  • 78
  • 4