Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
ImageStatistics.java
1 package ij.process;
2 import ij.measure.*;
3 import java.awt.*;
4 
6 public class ImageStatistics implements Measurements {
7 
8  public int[] histogram;
9  public int pixelCount;
10  public int mode;
11  public double dmode;
12  public double area;
13  public double min;
14  public double max;
15  public double mean;
16  public double stdDev;
17  public double xCentroid;
18  public double yCentroid;
19  public double xCenterOfMass;
20  public double yCenterOfMass;
21  public double roiX, roiY, roiWidth, roiHeight;
23  public double umean;
25  public double major;
27  public double minor;
29  public double angle;
30 
31  public double histMin;
32  public double histMax;
33  public int maxCount;
34  public int nBins = 256;
35  public double binSize = 1.0;
36 
37  protected int width, height;
38  protected int rx, ry, rw, rh;
39  protected double pw, ph;
40 
41  EllipseFitter ef;
42 
43 
44  public static ImageStatistics getStatistics(ImageProcessor ip, int mOptions, Calibration cal) {
45  if (ip instanceof ByteProcessor)
46  return new ByteStatistics(ip, mOptions, cal);
47  else if (ip instanceof ShortProcessor)
48  return new ShortStatistics(ip, mOptions, cal);
49  else if (ip instanceof ColorProcessor)
50  return new ColorStatistics(ip, mOptions, cal);
51  else
52  return new FloatStatistics(ip, mOptions, cal);
53  }
54 
55  void getRawMinAndMax(int minThreshold, int maxThreshold) {
56  int min = minThreshold;
57  while ((histogram[min] == 0) && (min < 255))
58  min++;
59  this.min = min;
60 
61  int max = maxThreshold;
62  while ((histogram[max] == 0) && (max > 0))
63  max--;
64  this.max = max;
65  }
66 
67  void getRawStatistics(int minThreshold, int maxThreshold) {
68  int count;
69  double value;
70  double sum = 0.0;
71  double sum2 = 0.0;
72 
73  for (int i=minThreshold; i<=maxThreshold; i++) {
74  count = histogram[i];
75  pixelCount += count;
76  sum += i*count;
77  value = i;
78  sum2 += (value*value)*count;
79  if (count>maxCount) {
80  maxCount = count;
81  mode = i;
82  }
83  }
84  area = pixelCount*pw*ph;
85  mean = sum/pixelCount;
86  umean = mean;
87  dmode = mode;
88  calculateStdDev(pixelCount, sum, sum2);
89  histMin = 0.0;
90  histMax = 255.0;
91  }
92 
93  void calculateStdDev(int n, double sum, double sum2) {
94  //ij.IJ.write("calculateStdDev: "+n+" "+sum+" "+sum2);
95  if (n>0) {
96  stdDev = (n*sum2-sum*sum)/n;
97  if (stdDev>0.0)
98  stdDev = Math.sqrt(stdDev/(n-1.0));
99  else
100  stdDev = 0.0;
101  }
102  else
103  stdDev = 0.0;
104  }
105 
106  void setup(ImageProcessor ip, Calibration cal) {
107  width = ip.getWidth();
108  height = ip.getHeight();
109  Rectangle roi = ip.getRoi();
110  if (roi != null) {
111  rx = roi.x;
112  ry = roi.y;
113  rw = roi.width;
114  rh = roi.height;
115  }
116  else {
117  rx = 0;
118  ry = 0;
119  rw = width;
120  rh = height;
121  }
122 
123  if (cal!=null) {
124  pw = cal.pixelWidth;
125  ph = cal.pixelHeight;
126  } else {
127  pw = 1.0;
128  ph = 1.0;
129  }
130 
131  roiX = rx*pw;
132  roiY = ry*ph;
133  roiWidth = rw*pw;
134  roiHeight = rh*ph;
135  }
136 
137  void getCentroid(ImageProcessor ip) {
138  byte[] mask = ip.getMaskArray();
139  int count=0, xsum=0, ysum=0,mi;
140  for (int y=ry,my=0; y<(ry+rh); y++,my++) {
141  mi = my*rw;
142  for (int x=rx; x<(rx+rw); x++) {
143  if (mask==null||mask[mi++]!=0) {
144  count++;
145  xsum+=x;
146  ysum+=y;
147  }
148  }
149  }
150  xCentroid = ((double)xsum/count+0.5)*pw;
151  yCentroid = ((double)ysum/count+0.5)*ph;
152  }
153 
154  void fitEllipse(ImageProcessor ip) {
155  if (ef==null)
156  ef = new EllipseFitter();
157  ef.fit(ip, this);
158  double psize = (Math.abs(pw-ph)/pw)<.01?pw:0.0;
159  major = ef.major*psize;
160  minor = ef.minor*psize;
161  angle = ef.angle;
162  xCentroid = ef.xCenter*pw;
163  yCentroid = ef.yCenter*ph;
164  //if (ij.IJ.altKeyDown())
165  // ef.drawEllipse(ip);
166  }
167 
168  public void drawEllipse(ImageProcessor ip) {
169  if (ef!=null)
170  ef.drawEllipse(ip);
171  }
172 
173 }