-
Notifications
You must be signed in to change notification settings - Fork 0
/
open-and-export-project-intersection.bsh
executable file
·164 lines (129 loc) · 4.78 KB
/
open-and-export-project-intersection.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
152
153
154
155
156
157
158
159
160
161
162
163
/**
* Call
*
* xvfb-run -a ./ImageJ-linux64 -Ddir1=<project1 directory> -Ddir2=<project2 directory> -Dexport=<export directory> -- --no-splash open-and-export-project-intersection.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.lang.Runtime;
import java.util.ArrayList;
import mpicbg.ij.TransformMeshMapping;
import mpicbg.models.CoordinateTransformMesh;
import mpicbg.models.InterpolatedCoordinateTransform;
import mpicbg.trakem2.util.Pair;
import java.io.Writer;
import java.io.FileWriter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.thoughtworks.xstream.XStream;
import java.io.IOException;
public static boolean writeObjectToFileXstream(Object o,String path) {
//filename is filepath string
try {
File file = new File(path);
file.getParentFile().mkdirs();
Writer writer = new FileWriter(file);
XStream xstream=new XStream();
writer.write(xstream.toXML(o));
writer.close();
}
catch ( IOException e )
{
System.err.println( "Error writing JSON file: " + path );
e.printStackTrace( System.err );
}
catch ( Exception e){
e.printStackTrace();
return false;
}
return true;
}
public static boolean writeObjectToFile(Object o,String path) {
//filename is filepath string
try {
File file = new File(path);
file.getParentFile().mkdirs();
Writer writer = new FileWriter(file);
//Gson gson = new GsonBuilder().create();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
gson.toJson(o, writer);
writer.close();
}
catch ( IOException e )
{
System.err.println( "Error writing JSON file: " + path );
e.printStackTrace( System.err );
}
catch ( Exception e){
e.printStackTrace();
return false;
}
return true;
}
int meshResolution = 128;
runtime = Runtime.getRuntime();
System.out.println( runtime.availableProcessors() + " cores available for multi-threading" );
dir1 = System.getProperty("dir1");
dir2 = System.getProperty("dir2");
export = System.getProperty("export");
ControlWindow.setGUIEnabled(false);
project1 = Project.openFSProject(dir1 + "/project.xml", false);
project2 = Project.openFSProject(dir2 + "/project.xml", false);
layerset1 = project1.getRootLayerSet();
layerset1.setSnapshotsMode(1);
layerset2 = project2.getRootLayerSet();
layerset2.setSnapshotsMode(1);
box = layerset1.get2DBounds().union(layerset2.get2DBounds());
/* 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((Object)layer1, (Object)layer2));
break;
}
}
}
/* render layers with interpolated transformations */
/* TODO do it for more than a single patch per layer */
for (int i = 0; i < pairs.size(); ++i) {
pair = pairs.get(i);
// float lambda = (i + 1.0f) / (pairs.size() + 1.0f); // for a transition range between two standalone projects
float lambda = (float)i / (float)pairs.size(); // for series of adjacent transition ranges
System.out.println("(" + pairs.get(i).a.getZ() + ", " + pairs.get(i).b.getZ() + ") : " + lambda);
/* 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();
/* interpolant */
ct = new InterpolatedCoordinateTransform(ct1, ct2, lambda);
/* get the pixels */
ip = p1.getImageProcessor();
ip.setInterpolationMethod(ImageProcessor.BILINEAR);
ipExport = ip.createProcessor(box.width, box.height);
/* render */
mesh = new CoordinateTransformMesh(ct, meshResolution, ip.getWidth(), ip.getHeight());
mapping = new TransformMeshMapping(mesh);
mapping.mapInterpolated(ip, ipExport);
/* save */
imp = new ImagePlus( "", ipExport );
IJ.save(imp, export + "/" + p1.getTitle() + ".tif");
//writeObjectToFile(ct,export + "/" +p1.getTitle() + ".xml");
writeObjectToFileXstream(ct,export + "/" +p1.getTitle() + ".xml");
//saveTransform(export + "/" + p1.getTitle() + ".xml",ct,box);
}
//writeObjectToFile(box,export + "/boundbox.xml");
writeObjectToFileXstream(box,export + "/boundbox.xml");
/* shutdown */
runtime.exit(0);