Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
ShortStatistics.java
1 package ij.process;
2 import ij.measure.Calibration;
3 
5 public class ShortStatistics extends ImageStatistics {
6 
11  this(ip, AREA+MEAN+MODE+MIN_MAX, null);
12  }
13 
17  public ShortStatistics(ImageProcessor ip, int mOptions, Calibration cal) {
18  this.width = ip.getWidth();
19  this.height = ip.getHeight();
20  setup(ip, cal);
21  nBins = 256;
22  double minT = ip.getMinThreshold();
23  int minThreshold,maxThreshold;
24  if ((mOptions&LIMIT)==0 || minT==ip.NO_THRESHOLD)
25  {minThreshold=0; maxThreshold=65535;}
26  else
27  {minThreshold=(int)minT; maxThreshold=(int)ip.getMaxThreshold();}
28  int[] hist = ip.getHistogram(); // 65536 bin histogram
29  float[] cTable = cal!=null?cal.getCTable():null;
30  getRawMinAndMax(hist, minThreshold, maxThreshold);
31  histMin = min;
32  histMax = max;
33  getStatistics(hist, (int)min, (int)max, cTable);
34  if ((mOptions&MODE)!=0)
35  getMode();
36  if ((mOptions&ELLIPSE)!=0)
37  fitEllipse(ip);
38  else if ((mOptions&CENTROID)!=0)
39  getCentroid(ip, minThreshold, maxThreshold);
40  if ((mOptions&CENTER_OF_MASS)!=0)
41  getCenterOfMass(ip, minThreshold, maxThreshold);
42  if ((mOptions&MIN_MAX)!=0 && cTable!=null) {
43  getCalibratedMinAndMax(hist, (int)min, (int)max, cTable);
44  }
45  }
46 
47  void getRawMinAndMax(int[] hist, int minThreshold, int maxThreshold) {
48  int min = minThreshold;
49  while ((hist[min]==0) && (min<65535))
50  min++;
51  this.min = min;
52 
53  int max = maxThreshold;
54  while ((hist[max]==0) && (max>0))
55  max--;
56  this.max = max;
57  }
58 
59  void getStatistics(int[] hist, int min, int max, float[] cTable) {
60  int count;
61  double value;
62  double sum = 0.0;
63  double sum2 = 0.0;
64  binSize = (histMax-histMin)/nBins;
65  double scale = 1.0/binSize;
66  int hMin = (int)histMin;
67  histogram = new int[nBins]; // 256 bin histogram
68  int index;
69  int maxCount = 0;
70 
71  for (int i=min; i<=max; i++) {
72  count = hist[i];
73  if (count>maxCount) {
74  maxCount = count;
75  dmode = i;
76  }
77  pixelCount += count;
78  value = cTable==null?i:cTable[i];
79  sum += value*count;
80  sum2 += (value*value)*count;
81  index = (int)(scale*(i-hMin));
82  if (index>=nBins)
83  index = nBins-1;
84  histogram[index] += count;
85  }
86  area = pixelCount*pw*ph;
87  mean = sum/pixelCount;
88  umean = mean;
89  calculateStdDev(pixelCount, sum, sum2);
90  if (cTable!=null)
91  dmode = cTable[(int)dmode];
92  }
93 
94  void getMode() {
95  int count;
96  maxCount = 0;
97  for (int i=0; i<nBins; i++) {
98  count = histogram[i];
99  if (count > maxCount) {
100  maxCount = count;
101  mode = i;
102  }
103  }
104  //ij.IJ.write("mode2: "+mode+" "+dmode+" "+maxCount);
105  }
106 
107 
108  void getCentroid(ImageProcessor ip, int minThreshold, int maxThreshold) {
109  short[] pixels = (short[])ip.getPixels();
110  byte[] mask = ip.getMaskArray();
111  boolean limit = minThreshold>0 || maxThreshold<65535;
112  int count=0, xsum=0, ysum=0,i,mi,v;
113  for (int y=ry,my=0; y<(ry+rh); y++,my++) {
114  i = y*width + rx;
115  mi = my*rw;
116  for (int x=rx; x<(rx+rw); x++) {
117  if (mask==null||mask[mi++]!=0) {
118  if (limit) {
119  v = pixels[i]&0xffff;
120  if (v>=minThreshold&&v<=maxThreshold) {
121  count++;
122  xsum+=x;
123  ysum+=y;
124  }
125  } else {
126  count++;
127  xsum+=x;
128  ysum+=y;
129  }
130  }
131  i++;
132  }
133  }
134  xCentroid = ((double)xsum/count+0.5)*pw;
135  yCentroid = ((double)ysum/count+0.5)*ph;
136  }
137 
138  void getCenterOfMass(ImageProcessor ip, int minThreshold, int maxThreshold) {
139  short[] pixels = (short[])ip.getPixels();
140  byte[] mask = ip.getMaskArray();
141  int i, mi, v;
142  double dv, count=0.0, xsum=0.0, ysum=0.0;
143  for (int y=ry,my=0; y<(ry+rh); y++,my++) {
144  i = y*width + rx;
145  mi = my*rw;
146  for (int x=rx; x<(rx+rw); x++) {
147  if (mask==null || mask[mi++]!=0) {
148  v = pixels[i]&0xffff;
149  if (v>=minThreshold&&v<=maxThreshold) {
150  count += v;
151  xsum += x*v;
152  ysum += y*v;
153  }
154  }
155  i++;
156  }
157  }
158  xCenterOfMass = (xsum/count+0.5)*pw;
159  yCenterOfMass = (ysum/count+0.5)*ph;
160  }
161 
162  void getCalibratedMinAndMax(int[] hist, int minValue, int maxValue, float[] cTable) {
163  min = Double.MAX_VALUE;
164  max = -Double.MAX_VALUE;
165  double v = 0.0;
166  for (int i=minValue; i<=maxValue; i++) {
167  if (hist[i]>0) {
168  v = cTable[i];
169  if (v<min) min = v;
170  if (v>max) max = v;
171  }
172  }
173  }
174 
175 }