-
Notifications
You must be signed in to change notification settings - Fork 7
/
apply-aligned-overlapping-projects.bsh
152 lines (125 loc) · 4.05 KB
/
apply-aligned-overlapping-projects.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
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
/**
* Applies a previously estimated rigid transformation to a series of
* overlapping projects and updates their bounding boxes accordingly
* Call
*
* xvfb-run ./ImageJ-linux64 -Ddir=<projects directory> -Dstart=<first index> -Dend=<last index> -Doverlap=<overlap size> -- --no-splash apply-aligned-overlaping-projects.bsh
*
* @author Stephan Saalfeld <[email protected]>
*/
import ini.trakem2.ControlWindow;
import ini.trakem2.Project;
import ini.trakem2.display.Layer;
import ini.trakem2.display.Patch;
import ij.IJ;
import ij.ImagePlus;
import ij.process.ImageProcessor;
import java.awt.geom.AffineTransform;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import mpicbg.trakem2.transform.RigidModel2D;
import mpicbg.trakem2.transform.TranslationModel2D;
import mpicbg.trakem2.util.Pair;
Project tryOpenProject(path) {
Exception f = null;
for (int i = 0; i < 10; ++i) {
try {
project = Project.openFSProject(path, false);
if (project != null)
return project;
}
catch (e) {
f = e;
f.printStackTrace();
}
System.out.println("Trial " + i + ", failed to open project \"" + path + "\".");
Thread.sleep(1000);
}
if (f == null)
return null;
else
throw f;
}
boolean initTransform(path, transform) {
try {
reader = new BufferedReader(new FileReader(path));
transform.init(reader.readLine());
reader.close();
return true;
}
catch (e) {
return false;
}
}
runtime = Runtime.getRuntime();
System.out.println( runtime.availableProcessors() + " cores available for multi-threading" );
dir = System.getProperty("dir");
start = Integer.parseInt(System.getProperty("start"));
end = Integer.parseInt(System.getProperty("end"));
overlap = Integer.parseInt(System.getProperty("overlap"));
ControlWindow.setGUIEnabled(false);
range = start + "-" + (start + overlap - 1);
project1 = null;
project2 = tryOpenProject(dir + "/" + range + "/project.xml");
layerset1;
layerset2 = project2.getRootLayerSet();
/* initialize ranges and transformation models */
ranges = new ArrayList();
ranges.add(range);
models = new ArrayList();
models.add(new RigidModel2D());
box = layerset2.get2DBounds();
for (int a = start ; a <= end ; a += overlap) {
System.out.println( "range1: " + range );
range = a + "-" + (a + 2 * overlap - 1);
ranges.add(range);
System.out.println("range2: " + range);
/* flip and load new project */
if (project1 != null)
project1.destroy();
project1 = project2;
project2 = tryOpenProject(dir + "/" + range + "/project.xml");
layerset1 = layerset2;
layerset2 = project2.getRootLayerSet();
/* load pairwise transformation model */
model = new RigidModel2D();
if (!initTransform(dir + "/" + range + "/rigid.txt", model))
System.out.println("Failed loading transformation.");
/* accumulate transformation model */
model.preConcatenate(models.get(models.size() - 1));
/* store transformation model */
models.add(model);
/* estimate local bounds after transformation */
box2 = layerset2.get2DBounds();
min = new float[]{box2.x, box2.y};
max = new float[]{box2.x + box2.width - 1, box2.y + box2.height - 1};
model.estimateBounds(min, max);
/* accumulate bounds */
box = box.union(new Rectangle(
(int)Math.floor(min[0]),
(int)Math.floor(min[1]),
(int)Math.ceil(max[0] + 1) - (int)Math.floor(min[0]),
(int)Math.ceil(max[1] + 1) - (int)Math.floor(min[1])));
System.out.println(model.toString());
System.out.println(box.toString());
}
project1.destroy();
project2.destroy();
/* re-box and apply transformation models */
for (int i = 0; i < ranges.size(); ++i) {
affine = models.get(i).createAffine();
project = tryOpenProject(dir + "/" + ranges.get(i) + "/project.xml");
System.out.println("final:"+affine.toString()+","+ranges.get(i));
layerset = project.getRootLayerSet();
for (patch : layerset.getDisplayables(Patch.class)) {
patch.getAffineTransform().preConcatenate(affine);
patch.updateInDatabase("transform");
patch.updateBucket();
}
layerset.setDimensions(box.x, box.y, box.width, box.height);
project.save();
project.destroy();
}
/* shutdown */
runtime.exit(0);