-
Notifications
You must be signed in to change notification settings - Fork 7
/
align-overlapping-projects-pair.bsh
135 lines (117 loc) · 3.39 KB
/
align-overlapping-projects-pair.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
/**
* Aligns a pair of overlapping projects
* Call
*
* xvfb-run ./ImageJ-linux64 -Ddir1=<project1 directory> -Ddir2=<project2 directory> -- --no-splash align-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.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import mpicbg.ij.TransformMeshMapping;
import mpicbg.models.CoordinateTransformMesh;
import mpicbg.models.InterpolatedCoordinateTransform;
import mpicbg.models.Point;
import mpicbg.models.PointMatch;
import mpicbg.trakem2.transform.RigidModel2D;
import mpicbg.trakem2.transform.TranslationModel2D;
import mpicbg.trakem2.util.Pair;
double sampleSpacing = 100.0;
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 saveTransform(path, transform) {
try {
fos = new FileOutputStream(path);
out = new OutputStreamWriter(fos, "UTF-8");
dataString = transform.toDataString();
out.write(dataString, 0, dataString.length() );
out.close();
return true;
}
catch (e) {
return false;
}
}
runtime = Runtime.getRuntime();
System.out.println(runtime.availableProcessors() + " cores available for multi-threading");
dir1 = System.getProperty("dir1");
dir2 = System.getProperty("dir2");
ControlWindow.setGUIEnabled(false);
project1 = tryOpenProject(dir1 + "/project.xml");
project2 = tryOpenProject(dir2 + "/project.xml");
layerset1 = project1.getRootLayerSet();
layerset2 = project2.getRootLayerSet();
/* put corresponding layers into a list of Pairs */
pairs = new ArrayList();
for (layer1 : layerset1.getLayers()) {
for (layer2 : layerset2.getLayers()) {
if (layer1.getZ() == layer2.getZ()) {
pairs.add(new Pair(layer1, layer2));
break;
}
}
}
/* sample point matches */
/* TODO do it for more than a single patch per layer */
matches = new ArrayList();
for (pair : pairs) {
System.out.println("layer " + pair.a.getZ());
/* corresponding patches */
p1 = pair.a.getDisplayables(Patch.class).get(0);
p2 = pair.b.getDisplayables(Patch.class).get(0);
/* all inclusive CoordinateTransform */
ct1 = p1.getFullCoordinateTransform();
ct2 = p2.getFullCoordinateTransform();
/* generate samples */
w = p1.getOWidth();
h = p1.getOHeight();
for (double y = 0; y < h; y += sampleSpacing) {
for (double x = 0; x < w; x += sampleSpacing) {
p = new Point(new float[]{(float)x, (float)y });
pw = p.getW();
p.apply(ct1);
p1 = new Point(new float[]{pw[0], pw[1]});
p.apply(ct2);
p2 = new Point(new float[]{pw[0], pw[1]});
matches.add(new PointMatch(p2, p1));
}
}
}
/* estimate pairwise transformation model */
model = new RigidModel2D();
//model = new TranslationModel2D();
model.fit(matches);
/* try to save transform */
for (i = 0; i < 10 && !saveTransform(dir2 + "/rigid.txt", model); ++i)
Thread.sleep(1000);
project1.destroy();
project2.destroy();
/* shutdown */
runtime.exit(0);