
// BinaryMask.java
// Andrew Davison, ad@coe.psu.ac.th, April 2023

/* Convert a color image to a mask (black and white).
   If the flag "adjust" is included then the threshold
   is adjusted on the intermediate grayscale image so
   that the mask includes both squares.

   Usage:
     > compile BinaryMask.java
     > run BinaryMask [adjust]
   e.g.
     > run BinaryMask
     > run BinaryMask adjust

*/

import ij.*;
import ij.io.*;
import ij.gui.*;
import ij.process.*;


public class BinaryMask
{

  public static void main(String[] args)
  {
    boolean adjust = false;
    if (args.length == 1)
      adjust = true;

    ImagePlus imp = new ImagePlus("squares.png");
    imp.show();
    IJ.wait(2000);   // so the changes can be observed

    // convert to grayscale
    // ImageConverter ic = new ImageConverter(imp);
    // ic.convertToGray8();
    IJ.run("8-bit");   // less coding!
    IJ.wait(2000);

    if (adjust)
      IJ.setRawThreshold(imp, 69, 180); 
         // set range to include both grayscale squares
         // Image >> Adjust >> Threshold

    IJ.run(imp, "Convert to Mask", "");
  } /* main */


}  // end of BinaryMask class
