Making a macro or batch process

Hi guys!

I’m really new to imagej, so i kindly ask for your help.

I wanted to make a macro/batch which processes all images in one folder.
These are the steps i want it to do:

  1. Open one image (simple jpeg image)
  2. Perform a weka analysis, with a classifier i made before
  3. Make a result with the weka analysis
  4. Save the result as a jpeg in a specified folder with the name of the input file + ‘overlay’ after it.
  5. Enhance the contrast of the jpeg
  6. Convert it to a mask
  7. Perform a particle analysis with the following parameters
  • size: 1000-infinity,
  • circularity 0.00-1.00,
  • show: outlines,
  • exclude on edges,
  • include holes,
  • in situ show
  1. Save the output in an excel sheet named 'name of the input picture, + ‘results’ after it.

I’ve been trying for days but i can’t seem to get any decent result out of it.

Can anybody help me with making the macro/batch?

Thanks in Advance

Hi,

First, have you read the introduction into macro programming? Then, open the macro recorder, do your analysis with the GUI, and in the macro editor open the template “IJ1 Macro>Process Folder”. Replace the content of the function processFile by what you recorded. You need to make a few changes to have it working (change the image name by the variable file etc).

I’m not at all familiar with the Weka analysis, but I guess it should look like that:


input = getDirectory("Input directory");
output = getDirectory("Output directory");

suffix = ".tif";  //you only want to apply to tiff images, no need to ask

processFolder(input);

function processFolder(input) {
    list = getFileList(input);
    for (i = 0; i < list.length; i++) {
        if(File.isDirectory(input + list[i]))   //if it's a directory, go to subfolder
            processFolder("" + input + list[i]);
        if(endsWith(list[i], suffix))   //if it's a tiff image, process it
            processFile(input, output, list[i]);
        //if it's neither a tiff nor a directory, do nothing
    }
}

function processFile(input, output, file) {
    //here, you put the real processing
    print("Processing: " + input + file);

    open(input + file);  //open image

    /* Weka Segmentation, I don't know what I'm doing, you better use the macro recorder!
    * From http://imagej.net/Trainable_Weka_Segmentation*/
    run("Trainable Weka Segmentation");
    wait(3000);            // wait for the plugin to load
    selectWindow("Trainable Weka Segmentation v2.3.0");  //you might need to change version number

    call("trainableSegmentation.Weka_Segmentation.loadClassifier", "/path/to/your/classifier.model");
    call("trainableSegmentation.Weka_Segmentation.applyClassifier", input, file, "showResults=true", "storeResults=false", "probabilityMaps=false", "");    //you may want to change the parameters
    call("trainableSegmentation.Weka_Segmentation.getResult");
    
    saveAs("Jpeg", output + file +"_overlay.jpg");
    run("Enhance Contrast...", "saturated=0.4");
    setAutoThreshold("Huang dark");    ///use your favorite thresholding method here
    run("Convert to Mask");

    //you didn't mention it, but you should first set measurements to be sure to have the right results
    run("Set Measurements...", "area mean shape redirect=None decimal=3");
    run("Analyze Particles...", "size=1000-Infinity show=Outlines exclude include in_situ");
    saveAs("results", output + file + "_results.xls");

    print("Saved to: " + output);
}

If you don’t want to process subfolders, you can delete the two lines in the first function.

Hope this helps!

3 Likes

Hi Alex!

I am actually trying to do the same as waterfles, i.e. running a macro that applies the trainable weka segmentation to all the images in the same folder. I tried the code you are giving here but instead of saving the segmented images (i.e. the results), it saves the input images…
Do you have a solution?

Thank you in advance for your help.

Welcome to the forum, @barbara01630!

It looks like @AlexW has not been active on the forum in a while, so I am using the @ mention syntax here to increase the chances of him seeing this question.

I did not test, but I have one idea: the saveAs function saves the active image. So maybe the wrong image is selected for you? You could try closing the input images once they are no longer needed, before saving. Or you could use the selectWindow macro function to switch the active window before saving.