File: StackReducer.java

package info (click to toggle)
imagej 1.52j-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,604 kB
  • sloc: java: 120,017; sh: 279; xml: 161; makefile: 6
file content (99 lines) | stat: -rw-r--r-- 3,177 bytes parent folder | download | duplicates (3)
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
package ij.plugin;
import ij.*;
import ij.process.*;
import ij.gui.*;
import ij.measure.Calibration;
import java.awt.*;

/** This plugin implements the Image/Stacks/Tools/Reduce command. */
public class StackReducer implements PlugIn {
	ImagePlus imp;
	private static int factor = 2;
	private boolean hyperstack, reduceSlices;

	public void run(String arg) {
		imp = WindowManager.getCurrentImage();
		if (imp==null)
			{IJ.noImage(); return;}
		ImageStack stack = imp.getStack();
		int size = stack.getSize();
		if (size==1 || (imp.getNChannels()==size&&imp.isComposite()))
			{IJ.error("Stack or hyperstack required"); return;}
		if (!showDialog(stack))
			return;
		if (hyperstack)
			reduceHyperstack(imp, factor, reduceSlices);
		else
			reduceStack(imp, factor);
	}

	public boolean showDialog(ImageStack stack) {
		hyperstack = imp.isHyperStack();
		boolean showCheckbox = false;
		if (hyperstack && imp.getNSlices()>1 && imp.getNFrames()>1)
			showCheckbox = true;
		else if (hyperstack && imp.getNSlices()>1)
			reduceSlices = true;
		int n = stack.getSize();
		GenericDialog gd = new GenericDialog("Reduce Size");
		gd.addNumericField("Reduction Factor:", factor, 0);
		if (showCheckbox)
			gd.addCheckbox("Reduce in Z-Dimension", false);
		gd.showDialog();
		if (gd.wasCanceled()) return false;
		factor = (int) gd.getNextNumber();
		if (showCheckbox)
			reduceSlices = gd.getNextBoolean();
		return true;
	}
	
	public void reduceStack(ImagePlus imp, int factor) {
		ImageStack stack = imp.getStack();
		boolean virtual = stack.isVirtual();
		int n = stack.getSize();
		ImageStack stack2 = new ImageStack(stack.getWidth(), stack.getHeight());
		for (int i=1; i<=n; i+=factor) {
			if (virtual) IJ.showProgress(i, n);
			stack2.addSlice(stack.getSliceLabel(i), stack.getProcessor(i));
		}
		imp.setStack(null, stack2);
		if (virtual) {
			IJ.showProgress(1.0);
			imp.setTitle(imp.getTitle());
		}
		Calibration cal = imp.getCalibration();
		if (cal.scaled()) cal.pixelDepth *= factor;
	}
	
	public void reduceHyperstack(ImagePlus imp, int factor, boolean reduceSlices) {
		int channels = imp.getNChannels();
		int slices = imp.getNSlices();
		int frames = imp.getNFrames();
		int zfactor = reduceSlices?factor:1;
		int tfactor = reduceSlices?1:factor;
		ImageStack stack = imp.getStack();
		ImageStack stack2 = new ImageStack(imp.getWidth(), imp.getHeight());
		boolean virtual = stack.isVirtual();
		int slices2 = slices/zfactor + ((slices%zfactor)!=0?1:0);
		int frames2 = frames/tfactor + ((frames%tfactor)!=0?1:0);
		int n = channels*slices2*frames2;
		int count = 1;
		for (int t=1; t<=frames; t+=tfactor) {
			for (int z=1; z<=slices; z+=zfactor) {
				for (int c=1; c<=channels; c++) {
					int i = imp.getStackIndex(c, z, t);
					IJ.showProgress(i, n);
					ImageProcessor ip = stack.getProcessor(imp.getStackIndex(c, z, t));
					//IJ.log(count++ +"  "+i+" "+c+" "+z+" "+t);
					stack2.addSlice(stack.getSliceLabel(i), ip);
				}
			}
		}
		imp.setStack(stack2, channels, slices2, frames2);
		Calibration cal = imp.getCalibration();
		if (cal.scaled()) cal.pixelDepth *= zfactor;
		if (virtual) imp.setTitle(imp.getTitle());
		IJ.showProgress(1.0);
	}

}