Batch mode macro for particle size distribution to csv

Hi Forum.

I have a working macro that will automatically save multiple particle size distribution bins as csv files in the source folder.

It works fine for a single image. However, I’d like it to analyze all images in a folder automatically, without me needing to manually re-run the process. I’d also like the macro to add all of the values together to return a single set of csv files - as opposed to a set of csv files for each image.

I’ve tried using the various batch mode macros I can find on here and on other sites and have read the ImageJ help documents. However, I can’t get anything to work.

I’ve also tried adding some lines to the start of my macro to first open all of the images in the folder and then use “select all” so that processing is applied to all images. The images all open fine, but the processing is only applied to one image since “select all” only applies to one image.

An example of my macro is below as well as a screenshot of the folder output for a single image.

Any help greatly appreciated!


open("");

run("8-bit");
setOption("BlackBackground", false);
run("Convert to Mask");

run("Measure");
saveAs("Results", File.directory + "Porosity.csv");
close ("Results");
run("Set Measurements...", "area area_fraction display redirect=None decimal=4");
run("Set Scale...", "distance=4 known=1 unit=Micron global");
run("Analyze Particles...", " ");

run("Distribution...", "parameter=Area or=4 and=0.00-0.99");
Plot.getValues( x, y );
str = "X\tY\n"; // header
for ( i=0; i<x.length; i++ ) {
        str += "" + x[i] + "\t" + y[i] + "\n"; }
File.saveString( str, File.directory + "/0.00-0.99.csv" ); 

close();

run("Distribution...", "parameter=Area or=1 and=0.99-4.99");
Plot.getValues( x, y );
str = "X\tY\n"; // header
for ( i=0; i<x.length; i++ ) {
        str += "" + x[i] + "\t" + y[i] + "\n"; }
File.saveString( str, File.directory + "/0.99-4.99.csv" ); 

close();

run("Distribution...", "parameter=Area or=1 and=4.99-9.99");
Plot.getValues( x, y );
str = "X\tY\n"; // header
for ( i=0; i<x.length; i++ ) {
        str += "" + x[i] + "\t" + y[i] + "\n"; }
File.saveString( str, File.directory + "/4.99-9.99.csv" ); 

close();

run("Distribution...", "parameter=Area or=9 and=9.99-99.99");
Plot.getValues( x, y );
str = "X\tY\n"; // header
for ( i=0; i<x.length; i++ ) {
        str += "" + x[i] + "\t" + y[i] + "\n"; }
File.saveString( str, File.directory + "/9.99-99.99.csv" ); 

close();

run("Distribution...", "parameter=Area or=9 and=99.99-999.99");
Plot.getValues( x, y );
str = "X\tY\n"; // header
for ( i=0; i<x.length; i++ ) {
        str += "" + x[i] + "\t" + y[i] + "\n"; }
File.saveString( str, File.directory + "/99.99-999.99.csv" );

 close();

run("Distribution...", "parameter=Area or=1 and=999.99-100000000000000000");
Plot.getValues( x, y );
str = "X\tY\n"; // header
for ( i=0; i<x.length; i++ ) {
        str += "" + x[i] + "\t" + y[i] + "\n"; }
File.saveString( str, File.directory + "/999.99-end.csv" ); 

 close();
 close("*");

Just to add:

All of my images are different sizes so I can’t work with them as a stack - this adds a black border to the images which are already binary b&w and so would impact the analyse particles results.

Thanks

Apologies for replying to my own post again…

However, I now have a partial solution.

Before running the distribution analysis, if I use “Open Next” and then measure, analyze particles, it will add the data from the next image to the Results Table. Once all images in the folder have been processed this way, I can then run the distribution and get the data I want.

I’ve done a test, and this method returns the correct values.

Now my problem is,
how can I add “Open Next” to my macro so that it will repeat continually until all images have been opened, and then cease and run the distribution?

Thanks

Added the ImageJ tag since this was currently not in a forum.