macrosimagejimage-thresholding

ImageJ macro help - store ROI labels to summary


Struggling a bit so decided to reach out to get some help. First, some background. I work with process monitoring of additive manufacturing of metals. I get gray-scale optical tomography images from every layer, and the intensity variations could indicate a defect (or clusters of defects across several layers indicate a defect). I am attempting to write a macro that will help me to do the following:

I manage with most points listed, but fail to get the ROI names to be incorporated in the summary (where the counts are listed) and to summarize amount of counts per ROI for all of the images. image of the summary window where I struggle to incorporate ROIs to that shows amount of countsMy extremely clunky way is now saving every summary per image, re-naming the ROIs manually, and writing an even more clunky Excel macro to summarize the counts across images.

Also here is a link to the small set of the raw dataset. 2

Would love to get some feedback on how this can be streamlined in a better way. Have searched and tried ChatGPT but without much success.

Here is a raw image of one layer. optical tomography from one layer. Here is the thresholded one showing the detected particles, and I want to count the number of particles within each ROI. thresholded image with ROIs. Finally, I want to get a text (csv, excel, or similar) file that summarizes the amount of counts per ROI over the images that are being analyzed. Here is an image of a previous Excel sheet where I have several ROIs, the corresponding name of the test piece, and summarized amount of counts for different thresholding operations. example of previous summary.

This is my current code:

#@ File (label = "Input directory", style = "directory") input
#@ File (label = "Output directory", style = "directory") output
#@ String (label = "File suffix", value = ".tif") suffix


processFolder(input);
setBatchMode(true);

// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
    list = getFileList(input);
    list = Array.sort(list);
    for (i = 0; i < list.length; i++) {
        if(File.isDirectory(input + File.separator + list[i]))
            processFolder(input + File.separator + list[i]);
        if(endsWith(list[i], suffix))
            processFile(input, output, list[i]);
    }
}

function processFile(input, output, file) {
    // Do the processing here by adding your own code.
    // Leave the print statements until things work, then remove them.
    print("Processing: " + input + File.separator + file);
    print("Saving to: " + output);

    //opening the image
    open(input + File.separator + file);
    filename_pure = File.nameWithoutExtension;
    
    //preparations
    roiManager("reset");
    run("Clear Results");
    saving_prefix = output + File.separator + filename_pure;
    
    //get the image name
    title = getTitle();
    
    //load ROI
    roiManager("Open", "C:\\Users\\riabov\\OneDrive - Chalmers\\höganäs and phd\\printing\\dynamiq\\al-case\\ot\\analysis\\dynamiq-al-roi.zip");
    
    //set an auto threshold and binarize
    setAutoThreshold("Yen dark no-reset");
    run("Convert to Mask");
    
    //run the ROI-macro
    c= roiManager("count");
        for (i = 0; i < c; i++) {
            roiManager("Select", i);
            run("Analyze Particles...", "  show=Nothing display summarize composite");
            }
            roiManager("show all without labels");

    //saving
    //save the results window as a comma-separated-value file
    saveAs("Results", saving_prefix + "_results.csv"); //use saveAs command to save results
    //save the image
    saveAs("tiff", saving_prefix + "_ROI.tif"); //use saveAs command to save an image
    
    
    //Clean-up
    run("Close All");
}

Solution

  • Here is an ImageJ-macro that computes what you like to obtain:

    run("Set Measurements...","display redirect=None decimal=3");
    n=roiManager("size");
    for ( i=0; i<n; i++ ) {
       roiManager("select",i);
       run("Analyze Particles...","summarize");
       Table.set("RoI",i,RoiManager.getName(i),"Summary");
    }
    exit();
    

    The thresholded image and the ROI-Manager with the set of RoIs must be open in ImageJ when you start this macro.

    Here is what I get for your sample image with an estimated threshold:Summary Results table