import ij.ImagePlus; import ij.io.FileSaver; import ij.io.Opener; import ij.process.ByteProcessor; import ij.process.ColorProcessor; /** ImageJ's actual byte-HSB conversion with explicit, inclusive thresholds. */ class ImageJMask { public static void main(String[] args) { ImagePlus input = new Opener().openImage(args[0]); if (input == null) throw new IllegalArgumentException("Unreadable image"); ColorProcessor rgb = (ColorProcessor) input.getProcessor().convertToRGB(); int size = rgb.getWidth() * rgb.getHeight(); byte[] h = new byte[size], s = new byte[size], b = new byte[size], mask = new byte[size]; rgb.getHSB(h, s, b); int[] limits = new int[6]; for (int i = 0; i < 6; i++) limits[i] = Integer.parseInt(args[i + 2]); for (int i = 0; i < size; i++) { int hue = h[i] & 255, sat = s[i] & 255, bri = b[i] & 255; if (hue >= limits[0] && hue <= limits[1] && sat >= limits[2] && sat <= limits[3] && bri >= limits[4] && bri <= limits[5]) mask[i] = (byte) 255; } ImagePlus output = new ImagePlus("Threshold mask", new ByteProcessor(rgb.getWidth(), rgb.getHeight(), mask)); if (!new FileSaver(output).saveAsPng(args[1])) throw new IllegalStateException("Cannot save mask"); System.out.println("ImageJ " + ij.IJ.getVersion()); } }