Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
ByteStatistics.java
1 package ij.process;
2 import ij.measure.Calibration;
3 
5 public class ByteStatistics extends ImageStatistics {
6 
11  this(ip, AREA+MEAN+MODE+MIN_MAX, null);
12  }
13 
16  public ByteStatistics(ImageProcessor ip, int mOptions, Calibration cal) {
18  histogram = bp.getHistogram();
19  setup(ip, cal);
20  double minT = ip.getMinThreshold();
21  int minThreshold,maxThreshold;
22  if ((mOptions&LIMIT)==0 || minT==ip.NO_THRESHOLD)
23  {minThreshold=0; maxThreshold=255;}
24  else
25  {minThreshold=(int)minT; maxThreshold=(int)ip.getMaxThreshold();}
26  float[] cTable = cal!=null?cal.getCTable():null;
27  if (cTable!=null)
28  getCalibratedStatistics(minThreshold,maxThreshold,cTable);
29  else
30  getRawStatistics(minThreshold,maxThreshold);
31  if ((mOptions&MIN_MAX)!=0) {
32  if (cTable!=null)
33  getCalibratedMinAndMax(minThreshold, maxThreshold, cTable);
34  else
35  getRawMinAndMax(minThreshold, maxThreshold);
36  }
37  if ((mOptions&ELLIPSE)!=0)
38  fitEllipse(ip);
39  else if ((mOptions&CENTROID)!=0)
40  getCentroid(ip, minThreshold, maxThreshold);
41  if ((mOptions&CENTER_OF_MASS)!=0)
42  getCenterOfMass(ip, minThreshold, maxThreshold, cTable);
43  }
44 
45  void getCalibratedStatistics(int minThreshold, int maxThreshold, float[] cTable) {
46  int count;
47  double value;
48  double sum = 0;
49  double sum2 = 0.0;
50  int isum = 0;
51 
52  for (int i=minThreshold; i<=maxThreshold; i++) {
53  count = histogram[i];
54  pixelCount += count;
55  value = cTable[i];
56  sum += value*count;
57  isum += i*count;
58  sum2 += (value*value)*count;
59  if (count>maxCount) {
60  maxCount = count;
61  mode = i;
62  }
63  }
64  area = pixelCount*pw*ph;
65  mean = sum/pixelCount;
66  umean = (double)isum/pixelCount;
67  dmode = cTable[mode];
68  calculateStdDev(pixelCount,sum,sum2);
69  histMin = 0.0;
70  histMax = 255.0;
71  }
72 
73  void getCentroid(ImageProcessor ip, int minThreshold, int maxThreshold) {
74  byte[] pixels = (byte[])ip.getPixels();
75  byte[] mask = ip.getMaskArray();
76  boolean limit = minThreshold>0 || maxThreshold<255;
77  int count=0, xsum=0, ysum=0,i,mi,v;
78  for (int y=ry,my=0; y<(ry+rh); y++,my++) {
79  i = y*width + rx;
80  mi = my*rw;
81  for (int x=rx; x<(rx+rw); x++) {
82  if (mask==null||mask[mi++]!=0) {
83  if (limit) {
84  v = pixels[i]&255;
85  if (v>=minThreshold&&v<=maxThreshold) {
86  count++;
87  xsum+=x;
88  ysum+=y;
89  }
90  } else {
91  count++;
92  xsum+=x;
93  ysum+=y;
94  }
95  }
96  i++;
97  }
98  }
99  xCentroid = ((double)xsum/count+0.5)*pw;
100  yCentroid = ((double)ysum/count+0.5)*ph;
101  }
102 
103  void getCenterOfMass(ImageProcessor ip, int minThreshold, int maxThreshold, float[] cTable) {
104  byte[] pixels = (byte[])ip.getPixels();
105  byte[] mask = ip.getMaskArray();
106  int v, i, mi;
107  double dv, count=0.0, xsum=0.0, ysum=0.0;
108  for (int y=ry,my=0; y<(ry+rh); y++,my++) {
109  i = y*width + rx;
110  mi = my*rw;
111  for (int x=rx; x<(rx+rw); x++) {
112  if (mask==null || mask[mi++]!=0) {
113  v = pixels[i]&255;
114  if (v>=minThreshold&&v<=maxThreshold) {
115  dv = ((cTable!=null)?cTable[v]:v)+Double.MIN_VALUE;
116  count += dv;
117  xsum += x*dv;
118  ysum += y*dv;
119  }
120  }
121  i++;
122  }
123  }
124  xCenterOfMass = (xsum/count+0.5)*pw;
125  yCenterOfMass = (ysum/count+0.5)*ph;
126  }
127 
128  void getCalibratedMinAndMax(int minThreshold, int maxThreshold, float[] cTable) {
129  min = Double.MAX_VALUE;
130  max = -Double.MAX_VALUE;
131  double v = 0.0;
132  for (int i=minThreshold; i<=maxThreshold; i++) {
133  if (histogram[i]>0) {
134  v = cTable[i];
135  if (v<min) min = v;
136  if (v>max) max = v;
137  }
138  }
139  }
140 
141 }