Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
Thresholder.java
1 package ij.plugin;
2 import ij.*;
3 import ij.gui.*;
4 import ij.process.*;
5 import ij.measure.*;
6 import ij.plugin.frame.Recorder;
7 import ij.plugin.filter.PlugInFilter;
8 import java.awt.*;
9 
11 public class Thresholder implements PlugIn, Measurements {
12 
13  private int slice;
14  private double minThreshold;
15  private double maxThreshold;
16  boolean autoThreshold;
17  boolean skipDialog;
18  static boolean fill1 = true;
19  static boolean fill2 = true;
20  static boolean useBW = true;
21 
22 
23  public void run(String arg) {
24  skipDialog = arg.equals("skip");
25  ImagePlus imp = IJ.getInstance().getImagePlus();
26  if (imp==null)
27  {IJ.noImage(); return;}
28  //if (imp.getType()==ImagePlus.COLOR_RGB)
29  // {IJ.error("RGB images are not supported."); return;}
30  Undo.reset();
31  applyThreshold(imp);
32  }
33 
34  void applyThreshold(ImagePlus imp) {
35  if (!imp.lock())
36  return;
37  imp.killRoi();
38  ImageProcessor ip = imp.getProcessor();
39  double saveMinThreshold = ip.getMinThreshold();
40  double saveMaxThreshold = ip.getMaxThreshold();
41  double saveMin = ip.getMin();
42  double saveMax = ip.getMax();
43  if (ip instanceof ByteProcessor)
44  {saveMin =0; saveMax = 255;}
45  autoThreshold = saveMinThreshold==ImageProcessor.NO_THRESHOLD;
46 
47  boolean useBlackAndWhite = true;
48  if (skipDialog)
49  fill1 = fill2 = useBlackAndWhite = true;
50  else if (!autoThreshold) {
51  GenericDialog gd = new GenericDialog("Apply Lut");
52  gd.addCheckbox("Thresholded pixels to foreground color", fill1);
53  gd.addCheckbox("Remaining pixels to background color", fill2);
54  gd.addMessage("");
55  gd.addCheckbox("Black foreground, white background", useBW);
56  gd.showDialog();
57  if (gd.wasCanceled())
58  {imp.unlock(); return;}
59  fill1 = gd.getNextBoolean();
60  fill2 = gd.getNextBoolean();
61  useBW = useBlackAndWhite = gd.getNextBoolean();
62  } else
63  fill1 = fill2 = true;
64 
65  if (!(imp.getType()==ImagePlus.GRAY8))
66  convertToByte(imp);
67  ip = imp.getProcessor();
68  if (autoThreshold)
69  autoThreshold(imp);
70  else {
71  if (Recorder.record)
72  Recorder.record("setThreshold", (int)saveMinThreshold, (int)saveMaxThreshold);
73  minThreshold = ((saveMinThreshold-saveMin)/(saveMax-saveMin))*255.0;
74  maxThreshold = ((saveMaxThreshold-saveMin)/(saveMax-saveMin))*255.0;
75  }
76 
77  int fcolor, bcolor;
78  ip.resetThreshold();
79  int savePixel = ip.getPixel(0,0);
80  if (useBlackAndWhite)
81  ip.setColor(Color.black);
82  else
83  ip.setColor(Toolbar.getForegroundColor());
84  ip.drawPixel(0,0);
85  fcolor = ip.getPixel(0,0);
86  if (useBlackAndWhite)
87  ip.setColor(Color.white);
88  else
89  ip.setColor(Toolbar.getBackgroundColor());
90  ip.drawPixel(0,0);
91  bcolor = ip.getPixel(0,0);
92  ip.setColor(Toolbar.getForegroundColor());
93  ip.putPixel(0,0,savePixel);
94 
95  int[] lut = new int[256];
96  for (int i=0; i<256; i++) {
97  if (i>=minThreshold && i<=maxThreshold)
98  lut[i] = fill1?fcolor:(byte)i;
99  else {
100  lut[i] = fill2?bcolor:(byte)i;
101  }
102  }
103  int result = IJ.setupDialog(imp, 0);
104  ip.applyTable(lut);
105  imp.updateAndDraw();
106  imp.unlock();
107  }
108 
109  void convertToByte(ImagePlus imp) {
110  ImageProcessor ip = imp.getProcessor();
111  imp.setProcessor(null, ip.convertToByte(true));
112  }
113 
114  void autoThreshold(ImagePlus imp) {
115  ImageStatistics stats = imp.getStatistics(MIN_MAX+MODE);
116  ImageProcessor ip = imp.getProcessor();
117  int threshold = ((ByteProcessor)ip).getAutoThreshold();
118  if ((stats.max-stats.mode)<(stats.mode-stats.min)) {
119  minThreshold = stats.min;
120  maxThreshold = threshold;
121  } else {
122  minThreshold = threshold;
123  maxThreshold = stats.max;
124  }
125  }
126 
127 }