
// CountBlobs.java
// Andrew Davison, ad@coe.psu.ac.th, April 2023

/* Convert the Blobs.png image into a mask and
   apply a particle analysis. Display the result, and 
   access the table of results.
 
   Usage:
     > compile CountBlobs.java
     > run CountBlobs

*/

import ij.*;
import ij.plugin.*;
import ij.plugin.filter.*;
import ij.measure.*;
import ij.text.*;


public class CountBlobs
{

  public static void main(String[] args)
  {
    ImagePlus imp = new ImagePlus("blobs.png");
    IJ.run(imp, "8-bit", "");
        // Image >> Type >> 8-bit
    IJ.run(imp, "Convert to Mask", "");
    imp.show();

    int options = ParticleAnalyzer.SHOW_OUTLINES + 
                  ParticleAnalyzer.CLEAR_WORKSHEET;

    int measurements = Measurements.AREA + Measurements.CENTROID +
                       Measurements.SHAPE_DESCRIPTORS;
                    // measure area, centroid, shape features

    ResultsTable rt = new ResultsTable();

    ParticleAnalyzer pa = new ParticleAnalyzer(options, 
               measurements, rt, 100, Double.MAX_VALUE, 0.8, 1.0);
                         // size=100-infinity, circularity= 0.8-1.0

    pa.analyze(imp);
    ImagePlus partImp = pa.getOutputImage();  // shown automatically
    rt.show("Particles");  // display results table as a window

    // acess the results as text
    System.out.println("Particles (" + rt.size() + ")");
    System.out.println(rt.getColumnHeadings());
    for (int i=0; i < rt.size(); i++)
       System.out.println(rt.getRowAsString(i));

    ImageUtils.waitGone(partImp); 
    System.exit(0);
  } /* main */


}  // end of CountBlobs class
