-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_for_CATMAID.bsh
More file actions
160 lines (130 loc) · 3.78 KB
/
export_for_CATMAID.bsh
File metadata and controls
160 lines (130 loc) · 3.78 KB
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
/**
* Export tiles for CATMAID.
*
*
* To use this script, open project in TrakEM2, regenerate mipmaps
* Open script editor (File/New/Script) Select Beanshell as language, paste this script in.
* Select layer range you wish to export (firstLayer and lastLayer)
* Select appropriate path
* Click Run
*
*/
import ij.Prefs;
Prefs.setThreads(1);
import java.lang.*;
import ij.ImagePlus;
import ij.process.ByteProcessor;
import ij.io.FileSaver;
import ij.IJ;
import ij.process.Blitter;
import ini.trakem2.Project;
import ini.trakem2.display.*;
import mpicbg.trakem2.transform.*;
import mpicbg.ij.util.*;
firstLayer = 31; // Section number minus one. first section = 0
lastLayer = 31;
path = "/home/witvliet/Desktop/trakem_export_test/"; //remember trailing slash
jpegQuality = 80;
tileWidth = 512;
tileHeight = 512;
front = Display.getFront();
layerSet = front.getLayerSet();
layers = layerSet.getLayers();
emptyImage = new ImagePlus("",new ByteProcessor(tileWidth,tileHeight));
left = 0;
top = 0;
w = layerSet.getLayerWidth();
h = layerSet.getLayerHeight();
ImagePlus openAndDelete(path) {
file = new File(path);
if ( file.exists() ) {
imp = new ImagePlus( path );
file.delete();
return imp;
} else {
return emptyImage;
}
}
emptySections = new ArrayList();
for ( int l = firstLayer; l <= lastLayer; ++l )
{
layer = layers.get( l );
if ( layer.getDisplayables( Patch.class ).size() == 0 ) {
IJ.log( "Section " + l + " empty" );
continue;
}
print(l);
dirPath = path + l;
dir = new File( dirPath );
if ( !dir.exists() )
dir.mkdirs();
/* level 0 tiles */
for ( y = 0; y < h; y += tileHeight )
{
for ( x = 0; x < w; x += tileWidth )
{
box = new Rectangle( x + left, y + top, tileWidth, tileHeight );
impTile = layer.getProject().getLoader().getFlatImage(
layer,
box,
1.0,
-1,
ImagePlus.GRAY8,
Patch.class,
true );
fileSaver = new FileSaver(impTile);
fileSaver.setJpegQuality( jpegQuality );
fileSaver.saveAsJpeg(dirPath + "/" + (y / tileHeight) + "_" + (x / tileWidth) + "_0.jpg");
fileSaver.saveAsTiff(dirPath + "/" + (y / tileHeight) + "_" + (x / tileWidth) + "_0.tif");
}
}
/* level [1,n] tiles */
for (
int w1 = ( int )Math.ceil( w / 2.0 ), h1 = ( int )Math.ceil( h / 2.0 ), s = 1;
w1 > tileWidth && h1 > tileHeight;
w1 = ( int )Math.ceil( w1 / 2.0 ), h1 = ( int )Math.ceil( h1 / 2.0 ), ++s )
{
IJ.log( w1 + " " + h1 );
for ( y = 0; y < h1; y += tileHeight )
{
yt = y / tileHeight;
for ( x = 0; x < w1; x += tileWidth )
{
xt = x / tileWidth;
imp1 = openAndDelete(
dirPath + "/" +
(2 * yt) + "_" +
(2 * xt) + "_" +
(s - 1) + ".tif");
imp2 = openAndDelete(
dirPath + "/" +
(2 * yt) + "_" +
(2 * xt + 1) + "_" +
(s - 1) + ".tif");
imp3 = openAndDelete(
dirPath + "/" +
(2 * yt + 1) + "_" +
(2 * xt) + "_" +
(s - 1) + ".tif");
imp4 = openAndDelete(
dirPath + "/" +
(2 * yt + 1) + "_" +
(2 * xt + 1) + "_" +
(s - 1) + ".tif");
ip4 = new ByteProcessor(tileWidth * 2, tileHeight * 2);
ip4.copyBits(imp1.getProcessor(), 0, 0, Blitter.COPY);
ip4.copyBits(imp2.getProcessor(), tileWidth, 0, Blitter.COPY);
ip4.copyBits(imp3.getProcessor(), 0, tileHeight, Blitter.COPY);
ip4.copyBits(imp4.getProcessor(), tileWidth, tileHeight, Blitter.COPY);
ip = mpicbg.trakem2.util.Downsampler.downsampleImageProcessor(ip4);
impTile = new ImagePlus("", ip);
fileSaver = new FileSaver( impTile );
fileSaver.setJpegQuality( jpegQuality );
fileSaver.saveAsJpeg( dirPath + "/" + yt + "_" + xt + "_" + s + ".jpg" );
if ( Math.ceil( w1 / 2.0 ) > tileWidth && Math.ceil( h1 / 2.0 ) > tileHeight )
fileSaver.saveAsTiff( dirPath + "/" + yt + "_" + xt + "_" + s + ".tif" );
}
}
}
}
print("done");