-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstack-mapping.bsh
84 lines (70 loc) · 2.4 KB
/
stack-mapping.bsh
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
import ij.ImagePlus;
import ij.process.ImageProcessor;
import ij.IJ;
import ij.measure.Calibration;
import mpicbg.ij.stack.InverseTransformMapping;
import mpicbg.models.TranslationModel3D;
import mpicbg.models.AffineModel3D;
import mpicbg.models.InverseCoordinateTransformList;
am = new float[]{
-0.010760121f, -0.003578307f, 0.9999361f, 0.0f,
-0.8556553f, 0.5174942f, -0.0073560514f, 0.0f,
-0.51743555f, -0.8556801f, -0.008630178f, 0.0f};
imp = IJ.getImage();
/* un-scale */
c = imp.getCalibration();
zFactor = (float)(c.pixelDepth / c.pixelWidth);
unScale = new AffineModel3D();
unScale.set(
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, zFactor, 0.0f);
/* center shift */
centerShift = new TranslationModel3D();
centerShift.set(-imp.getWidth() / 2, -imp.getHeight() / 2, -imp.getStack().getSize() / 2 * zFactor);
/* center un-shift */
centerUnShift = new TranslationModel3D();
centerUnShift.set(imp.getWidth() / 2, imp.getHeight() / 2, imp.getStack().getSize() / 2 * zFactor);
/* rotation */
rotation = new AffineModel3D();
rotation.set(
am[0], am[1], am[2], am[3],
am[4], am[5], am[6], am[7],
am[8], am[9], am[10], am[11]);
transform = new AffineModel3D();
transform.preConcatenate(unScale);
transform.preConcatenate(centerShift);
transform.preConcatenate(rotation);
transform.preConcatenate(centerUnShift);
/* bounding volume */
min = new float[]{0, 0, 0};
max = new float[]{imp.getWidth(), imp.getHeight(), imp.getStack().getSize()};
transform.estimateBounds(min, max);
w = (int)Math.ceil(max[0] - min[0]);
h = (int)Math.ceil(max[1] - min[1]);
d = (int)Math.ceil(max[2] - min[2]);
/* bounding volume offset */
minShift = new TranslationModel3D();
minShift.set(-min[0], -min[1], -min[2]);
transform.preConcatenate(minShift);
/* render target stack */
mapping = new InverseTransformMapping(transform);
ip = imp.getStack().getProcessor(1).createProcessor(imp.getWidth(), imp.getHeight());
targetStack = new ImageStack(w, h);
for (s = 0; s < d; ++s) {
ip = ip.createProcessor(w, h);
mapping.setSlice(s);
try {
mapping.mapInterpolated(imp.getStack(), ip);
}
catch (Exception e) {
e.printStackTrace();
}
targetStack.addSlice("", ip);
}
/* set proper calibration (it's isotropic at the former x,y-scale now) */
impTarget = new ImagePlus("target", targetStack);
calibration = imp.getCalibration().copy();
calibration.pixelDepth = calibration.pixelWidth;
impTarget.setCalibration(calibration);
impTarget.show();