File: StackStatistics.java

package info (click to toggle)
imagej 1.54g-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,520 kB
  • sloc: java: 132,209; sh: 286; xml: 255; makefile: 6
file content (359 lines) | stat: -rw-r--r-- 10,607 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package ij.process;
import ij.*;
import ij.gui.*;
import ij.measure.*;
import ij.plugin.filter.Analyzer;
import java.awt.*;

/** Statistics, including the histogram, of a stack. */
public class StackStatistics extends ImageStatistics {
	
	/** Creates a StackStatistics object from a stack, using 256 
		histogram bins and the entire stack pixel value range. */
	public StackStatistics(ImagePlus imp) {
		this(imp, 256, 0.0, imp.getBitDepth()==8||imp.getBitDepth()==24?256.0:0.0);
	}

	/** Creates a StackStatistics object from a stack, using the specified 
		histogram bin count and x-axis range (pixel value tange). */
	public StackStatistics(ImagePlus imp, int nBins, double xMin, double xMax) {
		int bits = imp.getBitDepth();
		stackStatistics = true;
		if ((bits==8||bits==24) && nBins==256 && xMin==0.0 && xMax==256.0)
			sum8BitHistograms(imp);
		else if (bits==16 && nBins==256 && xMin==0.0 && xMax==0.0 && !imp.getCalibration().calibrated())
			sum16BitHistograms(imp);
		else
			doCalculations(imp, nBins, xMin, xMax);
	}

    void doCalculations(ImagePlus imp,  int bins, double histogramMin, double histogramMax) {
       ImageProcessor ip = imp.getProcessor();
		boolean limitToThreshold = (Analyzer.getMeasurements()&LIMIT)!=0;
		double minThreshold = -Float.MAX_VALUE;
		double maxThreshold = Float.MAX_VALUE;
        Calibration cal = imp.getCalibration();
		if (limitToThreshold && ip.isThreshold()) {
			minThreshold=cal.getCValue(ip.getMinThreshold());
			maxThreshold=cal.getCValue(ip.getMaxThreshold());
		}
    	nBins = bins;
    	histMin = histogramMin;
    	histMax = histogramMax;
        ImageStack stack = imp.getStack();
        int size = stack.size();
        ip.setRoi(imp.getRoi());
        byte[] mask = ip.getMaskArray();
        float[] cTable = imp.getCalibration().getCTable();
        longHistogram = new long[nBins];
        double v;
        double sum = 0;
        double sum2 = 0;
        int width, height;
        int rx, ry, rw, rh;
        double pw, ph;
        
        width = ip.getWidth();
        height = ip.getHeight();
        Rectangle roi = ip.getRoi();
        if (roi != null) {
            rx = roi.x;
            ry = roi.y;
            rw = roi.width;
            rh = roi.height;
        } else {
            rx = 0;
            ry = 0;
            rw = width;
            rh = height;
        }
        
        pw = 1.0;
        ph = 1.0;
        roiX = rx*pw;
        roiY = ry*ph;
        roiWidth = rw*pw;
        roiHeight = rh*ph;
        boolean fixedRange = histMin!=0 || histMax!=0.0;
        
        // calculate min and max
		double roiMin = Double.MAX_VALUE;
		double roiMax = -Double.MAX_VALUE;
		for (int slice=1; slice<=size; slice++) {
			IJ.showStatus("Calculating stack histogram...");
			IJ.showProgress(slice/2, size);
			ip = stack.getProcessor(slice);
			//ip.setCalibrationTable(cTable);
			for (int y=ry, my=0; y<(ry+rh); y++, my++) {
				int i = y * width + rx;
				int mi = my * rw;
				for (int x=rx; x<(rx+rw); x++) {
					if (mask==null || mask[mi++]!=0) {
						v = ip.getPixelValue(x,y);
						if (v>=minThreshold && v<=maxThreshold) {
							if (v<roiMin)
								roiMin = v;
							if (v>roiMax)
								roiMax = v;
						}
					}
					i++;
				}
			}
		 }
		min = roiMin;
		max = roiMax;
		if (fixedRange) {
			if (min<histMin) min = histMin;
			if (max>histMax) max = histMax;
		} else {
			histMin = min; 
			histMax =  max;
		}
       
        // Generate histogram
        double scale = nBins/( histMax-histMin);
        pixelCount = 0;
        int index;
        boolean first = true;
        for (int slice=1; slice<=size; slice++) {
            IJ.showProgress(size/2+slice/2, size);
            ip = stack.getProcessor(slice);
            ip.setCalibrationTable(cTable);
            for (int y=ry, my=0; y<(ry+rh); y++, my++) {
                int i = y * width + rx;
                int mi = my * rw;
                for (int x=rx; x<(rx+rw); x++) {
                    if (mask==null || mask[mi++]!=0) {
                        v = ip.getPixelValue(x,y);
						if (v>=minThreshold && v<=maxThreshold && v>=histMin && v<=histMax) {
							longPixelCount++;
							sum += v;
							sum2 += v*v;
							index = (int)(scale*(v-histMin));
							if (index>=nBins)
								index = nBins-1;
							longHistogram[index]++;
						}
                    }
                    i++;
                }
            }
        }
        pixelCount = (int)longPixelCount;
        area = longPixelCount*pw*ph;
        mean = sum/longPixelCount;
        calculateStdDev(longPixelCount, sum, sum2);
        histMin = cal.getRawValue(histMin); 
        histMax =  cal.getRawValue(histMax);
        binSize = (histMax-histMin)/nBins;
        int bits = imp.getBitDepth();
        if (histMin==0.0 && histMax==256.0 && (bits==8||bits==24))
        	histMax = 255.0;
        dmode = getMode(cal);
		copyHistogram(nBins);
		median = getMedian(longHistogram, (int)minThreshold, (int)maxThreshold, cal);
        IJ.showStatus("");
        IJ.showProgress(1.0);
    }
    
	void sum8BitHistograms(ImagePlus imp) {
		Calibration cal = imp.getCalibration();
		boolean limitToThreshold = (Analyzer.getMeasurements()&LIMIT)!=0;
		int minThreshold = 0;
		int maxThreshold = 255;
		ImageProcessor ip = imp.getProcessor();
		if (limitToThreshold && ip.isThreshold()) {
			minThreshold = (int)ip.getMinThreshold();
			maxThreshold = (int)ip.getMaxThreshold();
		}
		ImageStack stack = imp.getStack();
		Roi roi = imp.getRoi();
		longHistogram = new long[256];
		int n = stack.size();
		for (int slice=1; slice<=n; slice++) {
			IJ.showProgress(slice, n);
			ip = stack.getProcessor(slice);
			if (roi!=null) ip.setRoi(roi);
			int[] hist = ip.getHistogram();
			for (int i=0; i<256; i++)
				longHistogram[i] += hist[i];
		}
		pw=1.0; ph=1.0;
		getRawStatistics(longHistogram, minThreshold, maxThreshold);
		getRawMinAndMax(longHistogram, minThreshold, maxThreshold);
		copyHistogram(256);
		median = getMedian(longHistogram, minThreshold, maxThreshold, cal);
		IJ.showStatus("");
		IJ.showProgress(1.0);
	}
	
	private void copyHistogram(int nbins) {
		histogram = new int[nbins];
		for (int i=0; i<nbins; i++) {
			long count = longHistogram[i];
			if (count<=Integer.MAX_VALUE)
				histogram[i] = (int)count;
			else
				histogram[i] = Integer.MAX_VALUE;
		}
	}

	void getRawStatistics(long[] histogram, int minThreshold, int maxThreshold) {
		long count;
		long longMaxCount = 0L;
		double value;
		double sum = 0.0;
		double sum2 = 0.0;
		
		for (int i=minThreshold; i<=maxThreshold; i++) {
			count = histogram[i];
			longPixelCount += count;
			sum += (double)i*count;
			value = i;
			sum2 += (value*value)*count;
			if (count>longMaxCount) {
				longMaxCount = count;
				mode = i;
			}
		}
		maxCount = (int)longMaxCount;
		pixelCount = (int)longPixelCount;
		area = longPixelCount*pw*ph;
		mean = sum/longPixelCount;
		umean = mean;
		dmode = mode;
		calculateStdDev(longPixelCount, sum, sum2);
		histMin = 0.0;
		histMax = 255.0;
	}

	void getRawMinAndMax(long[] histogram, int minThreshold, int maxThreshold) {
		int min = minThreshold;
		while ((histogram[min]==0L) && (min<255))
			min++;
		this.min = min;
		int max = maxThreshold;
		while ((histogram[max]==0L) && (max>0))
			max--;
		this.max = max;
	}

	void sum16BitHistograms(ImagePlus imp) {
		Calibration cal = imp.getCalibration();
		boolean limitToThreshold = (Analyzer.getMeasurements()&LIMIT)!=0;
		int minThreshold = 0;
		int maxThreshold = 65535;
		ImageProcessor ip = imp.getProcessor();
		if (limitToThreshold && ip.getMinThreshold()!=ImageProcessor.NO_THRESHOLD) {
			minThreshold = (int)ip.getMinThreshold();
			maxThreshold = (int)ip.getMaxThreshold();
		}
		ImageStack stack = imp.getStack();
		Roi roi = imp.getRoi();
		long[] hist16 = new long[65536];
		int n = stack.size();
		for (int slice=1; slice<=n; slice++) {
			IJ.showProgress(slice, n);
			IJ.showStatus(slice+"/"+n);
			ip = stack.getProcessor(slice);
			if (roi!=null) ip.setRoi(roi);
			int[] hist = ip.getHistogram();
			for (int i=0; i<65536; i++)
				hist16[i] += hist[i];
		}
		pw=1.0; ph=1.0;
		getRaw16BitMinAndMax(hist16, minThreshold, maxThreshold);
		get16BitStatistics(hist16, (int)min, (int)max);
		median = getMedian(hist16, minThreshold, maxThreshold, cal);
		histogram16 = new int[65536];
		for (int i=0; i<65536; i++) {
			long count = hist16[i];
			if (count<=Integer.MAX_VALUE)
				histogram16[i] = (int)count;
			else
				histogram16[i] = Integer.MAX_VALUE;
		}
		IJ.showStatus("");
		IJ.showProgress(1.0);
	}
	
	void getRaw16BitMinAndMax(long[] hist, int minThreshold, int maxThreshold) {
		int min = minThreshold;
		while ((hist[min]==0) && (min<65535))
			min++;
		this.min = min;
		int max = maxThreshold;
		while ((hist[max]==0) && (max>0))
			max--;
		this.max = max;
	}

	void get16BitStatistics(long[] hist, int min, int max) {
		long count;
		double value;
		double sum = 0.0;
		double sum2 = 0.0;
		nBins = 256;
		histMin = min; 
		histMax = max;
		binSize = (histMax-histMin)/nBins;
		double scale = 1.0/binSize;
		int hMin = (int)histMin;
		longHistogram = new long[nBins]; // 256 bin histogram
		int index;
        maxCount = 0;
		for (int i=min; i<=max; i++) {
			count = hist[i];
			longPixelCount += count;
			value = i;
			sum += value*count;
			sum2 += (value*value)*count;
			index = (int)(scale*(i-hMin));
			if (index>=nBins)
				index = nBins-1;
			longHistogram[index] += count;
		}
		copyHistogram(nBins);
		pixelCount = (int)longPixelCount;
		area = longPixelCount*pw*ph;
		mean = sum/longPixelCount;
		umean = mean;
		dmode = getMode(null);
		calculateStdDev(longPixelCount, sum, sum2);
	}

   double getMode(Calibration cal) {
        long count;
        long longMaxCount = 0L;
        for (int i=0; i<nBins; i++) {
            count = longHistogram[i];
            if (count>longMaxCount) {
                longMaxCount = count;
                mode = i;
            }
        }
		if (longMaxCount<=Integer.MAX_VALUE)
			maxCount = (int)longMaxCount;
		else
			maxCount = Integer.MAX_VALUE;
        double tmode = histMin+mode*binSize;
        if (cal!=null) tmode = cal.getCValue(tmode);
        return tmode;
    }
    
    double getMedian(long[] hist, int first, int last, Calibration cal) {
		//ij.IJ.log("getMedian: "+first+"  "+last+"  "+hist.length+"  "+pixelCount);
		if (pixelCount==0 || first<0 || last>hist.length)
			return Double.NaN;
		double sum = 0;
		int i = first-1;
		double halfCount = pixelCount/2.0;
		do {
			sum += hist[++i];
		} while (sum<=halfCount && i<last);
		return cal!=null?cal.getCValue(i):i;
	}

   
}