File: GroupedZProjector.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 (87 lines) | stat: -rw-r--r-- 2,523 bytes parent folder | download | duplicates (2)
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
package ij.plugin;
import ij.*;
import ij.gui.GenericDialog;
import ij.process.*;
import ij.measure.Calibration;

/** This plugin implements the Image/Stacks/Tools/Grouped Z Project command. */

public class GroupedZProjector implements PlugIn {
	private static int method = ZProjector.AVG_METHOD;
	private int groupSize;
	
	public void run(String arg) {
		ImagePlus imp = IJ.getImage();
		int size = imp.getStackSize();
		if (size==1) {
			IJ.error("Z Project", "This command requires a stack");
			return;
		}
		if (imp.isHyperStack()) {
			new ZProjector().run("");
			return;
		}
		if (!showDialog(imp))
			return;
		ImagePlus imp2 = groupZProject(imp, method, groupSize);
		imp2.setCalibration(imp.getCalibration());
		Calibration cal = imp2.getCalibration();
		cal.pixelDepth *= groupSize;
		if (imp!=null)
			imp2.show();
	}
	
	public ImagePlus groupZProject(ImagePlus imp, int method, int groupSize) {
		if (method<0 || method>=ZProjector.METHODS.length)
			return null;
		int[] dim = imp.getDimensions();
		int projectedStackSize = imp.getStackSize()/groupSize;
		imp.setDimensions(1, groupSize, projectedStackSize);
		ZProjector zp = new ZProjector(imp);
		zp.setMethod(method);
		zp.setStartSlice(1);
		zp.setStopSlice(groupSize);
		zp.doHyperStackProjection(true);
		imp.setDimensions(dim[2], dim[3], dim[4]);

		ImagePlus zProjectorOutput = zp.getProjection();
		int[] zProjectDim = zProjectorOutput.getDimensions();
		for (int i=2; i<dim.length; i++) {
			if (dim[i] != 1)
				zProjectDim[i] = projectedStackSize;
			else
				zProjectDim[i] = 1;
		}
		// Fix dimensions for output ImagePlus
		zProjectorOutput.setDimensions(zProjectDim[2], zProjectDim[3], zProjectDim[4]);
		return zProjectorOutput;
	}
	
	boolean showDialog(ImagePlus imp) {
		int size = imp.getStackSize();
		GenericDialog gd = new GenericDialog("Z Project");
		gd.addChoice("Projection method:", ZProjector.METHODS, ZProjector.METHODS[method]);
		gd.addNumericField("Group size:", size, 0);
		String factors = "Valid factors: ";
		int i = 1, count = 0;
		while (i <= size && count<10) {
			if (size % i == 0) {
				count++; factors +=	 " "+ i +",";
			}
			i++;
		}
		gd.setInsets(10,0,0);
		gd.addMessage(factors+"...");
		gd.showDialog();
		if (gd.wasCanceled())
			return false;
		method = gd.getNextChoiceIndex();
		groupSize = (int)gd.getNextNumber();
		if (groupSize<1 || groupSize>size || (size%groupSize)!=0) {
			IJ.error("ZProject", "Group size must divide evenly into the stack size.");
			return false;
		}
		return true;
	}
	
}