Microscope image analysis - automatation


I’m working on a macro to seperate cracks, voids and pores on microscope images.
At the moment I have a macro and I can start with a random image open. It makes some binary, noise filter and creates three images, each containing only cracks, voids and pores.
The macro:

//Variablen Titel des Bildes abgreifen
///path = dir+name;
//Bild duplizieren
run("Duplicate...", " ");
//Bild bekommt Dateinamen mit -1 
//Binary erstellen
setOption("BlackBackground", false);
run("Make Binary");
//Rauschen entfernen, >50 Pixel wird entfernt
//run("Subtract Background...", "rolling=1 light");
//Threshold wählen
run("Convert to Mask");
//Kopie und Benennung
run("Duplicate...", "title=crack.tif");
//Partikelanalyse Riss
run("Analyze Particles...", "  circularity=0.00-0.30 show=Outlines display summarize");
run("Duplicate...", "title=voids.tif");
//Partikelanalyse Anbindungsfehler
run("Analyze Particles...", "  circularity=0.30-0.70 show=Outlines display summarize");
run("Duplicate...", "title=pores.tif");
//Partikelanalyse Poren
run("Analyze Particles...", "  circularity=0.70-1 show=Outlines display summarize");

Now I want to polish this thing a bit.
My goal is now to safe the cracks.tif, voids.tif and pores.tif as files in the same directory, but named like the input file with the extension “-cracks”, “-voids” and “-pores” and then close these images automatically. I tried it with a variable but it doesn’t work. Also I want to safe the binary image after run(“Convert To Mask”).
In the end I want to select a folder, start the macro and the macro automatically exports the results table to an Excel sheet. But I thinks it’s a long way to got!

Appreciate any help!

Kind regards
biaxialerzugtest1.tif (1.2 MB)

This is my final result.
Any ideas how to make it run in an batch process?

//Skalierung festlegen
run("Set Scale...", "distance=312 known=0.2 unit=mm global");
//Festlegen welche Werte gemessen werden sollen
run("Set Measurements...", "area perimeter fit area_fraction redirect=None decimal=2");

//Dateipfad (dir) und Name (originalName) speichern
path = dir+originalName;

//Binary erstellen und speichern
setOption("BlackBackground", false);
run("Make Binary");
run("Fill Holes");
saveAs("tiff", path+"_binary.tiff");

selectWindow(originalName + "_binary.tiff");

//Partikelanalyse Risse
run("Duplicate...", "title=cracks.tiff");
run("Analyze Particles...", "  circularity=0.00-0.43 show=Masks display summarize");
saveAs("tiff", path+"_cracks.tiff")
//saveAs("tiff", path+"_cracks_colored.tiff");
selectWindow(originalName + "_binary.tiff");

//Partikelanalyse Anbindungsfehler
run("Duplicate...", "title=voids.tiff");
run("Analyze Particles...", "  circularity=0.43-0.86 show=Masks display summarize");
saveAs("tiff", path+"_voids.tiff")
//saveAs("tiff", path+"_voids_colored.tiff");
selectWindow(originalName + "_binary.tiff");

//Partikelanalyse Poren
run("Duplicate...", "title=pores.tiff");
run("Analyze Particles...", "  circularity=0.86-1.00 show=Masks display summarize");
saveAs("tiff", path+"_pores.tiff")
//saveAs("tiff", path+"_pores_colored.tiff");

close(originalName + "_binary.tiff");

//run("Merge Channels...", "c1=cracks.tiff c3=voids.tiff c7=pores.tiff create");
//run("Stack to RGB");
//saveAs("PNG", path+"_result");

//Ergebnisse speichern
saveAs("Results", path+"_result.csv");
saveAs("Results", path+"_measure.csv");

run("Close All")

Hi @biaxialerzug,

and welcome to the forum.

The easiest way in running your macro in batch on a single folder of images is to use the built-in batch plugin: →Process →Batch →Macro…
Copy your macro in the text window and specify the folder you want to run it on.

There are more elegant ways in running it in batch, but I tested it and for your macro it should work well. What I would still add at the end is a closing command for the tables with the two commands:


If you want to automate it also for subfolders then the following example might help you further as a skeleton which you can build around your macro: https://imagej.net/macros/ListFilesRecursively.txt

Thank you very much!
Works for me too, but it replaces the original image with a binary oone. Do you know why?

In my case it kept the original image. But if you specify a seperate output folder you can definitely avoid that any image will be overwritten.