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