forked from trakem2/TrakEM2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownsamplerTest.java
More file actions
353 lines (307 loc) · 11.5 KB
/
DownsamplerTest.java
File metadata and controls
353 lines (307 loc) · 11.5 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import ij.ImageJ;
import ij.ImagePlus;
import ij.gui.OvalRoi;
import ij.process.ByteProcessor;
import ij.process.ColorProcessor;
import ij.process.FloatProcessor;
import ij.process.ImageProcessor;
import ij.process.ShortProcessor;
import ini.trakem2.imaging.FastIntegralImage;
import mpicbg.trakem2.util.Downsampler;
import mpicbg.trakem2.util.Downsampler.Pair;
import mpicbg.util.Timer;
/**
* License: GPL
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/**
*
*
* @author Stephan Saalfeld <saalfeld@mpi-cbg.de>
* @version 0.1a
*/
public class DownsamplerTest
{
final static int n = 20;
final private static void testShort( ShortProcessor ipShort )
{
final double min = ipShort.getMin();
final double max = ipShort.getMax();
while( ipShort.getWidth() > 32 )
{
ipShort = Downsampler.downsampleShortProcessor( ipShort );
ipShort.setMinAndMax( min, max );
}
}
final private static void testFloat( FloatProcessor ipFloat )
{
final double min = ipFloat.getMin();
final double max = ipFloat.getMax();
while( ipFloat.getWidth() > 32 )
{
ipFloat = Downsampler.downsampleFloatProcessor( ipFloat );
ipFloat.setMinAndMax( min, max );
}
}
final private static void testByte( ByteProcessor ipByte )
{
while( ipByte.getWidth() > 32 )
ipByte = Downsampler.downsampleByteProcessor( ipByte );
}
final private static void testColor( ColorProcessor ipColor )
{
while( ipColor.getWidth() > 32 )
ipColor = Downsampler.downsampleColorProcessor( ipColor );
}
/**
* Test downsampling the pyramid of a short image + byte alpha channel
* including the byte mapping of the short doing the alpha channel in a
* separate loop.
*
* @param ba
*/
final private static void testShortAlphaIndependently( Pair< ShortProcessor, byte[] > ba, ByteProcessor alpha )
{
while( ba.a.getWidth() > 32 )
{
ba = Downsampler.downsampleShort( ba.a );
alpha = Downsampler.downsampleByteProcessor( alpha );
// new ImagePlus( "pixels " + ba.a.getWidth(), ba.a ).show();
// new ImagePlus( "pixels to byte " + ba.a.getWidth(), new ByteProcessor( ba.a.getWidth(), ba.a.getHeight(), ba.b[ 0 ], null ) ).show();
// new ImagePlus( "alpha " + ba.a.getWidth(), new ByteProcessor( ba.a.getWidth(), ba.a.getHeight(), ba.b[ 1 ], null ) ).show();
}
}
/**
* Test downsampling the pyramid of a short image + byte alpha channel
* including the byte mapping of the short all in separate loops.
*
* @param ba
*/
final private static void testShortAlphaByteMappingIndependently( ShortProcessor sp, ByteProcessor alpha )
{
ByteProcessor bp;
while( sp.getWidth() > 32 )
{
sp = Downsampler.downsampleShortProcessor( sp );
bp = ( ByteProcessor )sp.convertToByte( true );
alpha = Downsampler.downsampleByteProcessor( alpha );
// new ImagePlus( "pixels " + ba.a.getWidth(), ba.a ).show();
// new ImagePlus( "pixels to byte " + ba.a.getWidth(), new ByteProcessor( ba.a.getWidth(), ba.a.getHeight(), ba.b[ 0 ], null ) ).show();
// new ImagePlus( "alpha " + ba.a.getWidth(), new ByteProcessor( ba.a.getWidth(), ba.a.getHeight(), ba.b[ 1 ], null ) ).show();
}
}
/**
* Test downsampling the pyramid of a short image + byte alpha channel
* including the byte mapping of the short doing the alpha channel in a
* separate loop.
*
* @param ba
*/
final private static void testFloatAlphaIndependently( Pair< FloatProcessor, byte[] > ba, ByteProcessor alpha )
{
while( ba.a.getWidth() > 32 )
{
ba = Downsampler.downsampleFloat( ba.a );
alpha = Downsampler.downsampleByteProcessor( alpha );
// new ImagePlus( "pixels " + ba.a.getWidth(), ba.a ).show();
// new ImagePlus( "pixels to byte " + ba.a.getWidth(), new ByteProcessor( ba.a.getWidth(), ba.a.getHeight(), ba.b[ 0 ], null ) ).show();
// new ImagePlus( "alpha " + ba.a.getWidth(), new ByteProcessor( ba.a.getWidth(), ba.a.getHeight(), ba.b[ 1 ], null ) ).show();
}
}
/**
* Test downsampling the pyramid of a short image + byte alpha channel
* including the byte mapping of the short doing the alpha channel in a
* separate loop.
*
* @param ba
*/
final private static void testColorAlphaIndependently( Pair< ColorProcessor, byte[][] > ba, ByteProcessor alpha )
{
while( ba.a.getWidth() > 32 )
{
ba = Downsampler.downsampleColor( ba.a );
alpha = Downsampler.downsampleByteProcessor( alpha );
// new ImagePlus( "pixels " + ba.a.getWidth(), ba.a ).show();
// new ImagePlus( "pixels to byte " + ba.a.getWidth(), new ByteProcessor( ba.a.getWidth(), ba.a.getHeight(), ba.b[ 0 ], null ) ).show();
// new ImagePlus( "alpha " + ba.a.getWidth(), new ByteProcessor( ba.a.getWidth(), ba.a.getHeight(), ba.b[ 1 ], null ) ).show();
}
}
final private static void testAlphaOutside( Pair< ByteProcessor, ByteProcessor > ba )
{
while( ba.a.getWidth() > 32 )
{
ba = Downsampler.downsampleAlphaAndOutside( ba.a, ba.b );
// new ImagePlus( "alpha " + ba.a.getWidth(), ba.a ).show();
// new ImagePlus( "outside " + ba.b.getWidth(), ba.b ).show();
}
}
final private static void testOutside( ByteProcessor a )
{
while( a.getWidth() > 32 )
{
a = Downsampler.downsampleOutside( a );
// new ImagePlus( "alpha " + ba.a.getWidth(), ba.a ).show();
// new ImagePlus( "outside " + ba.b.getWidth(), ba.b ).show();
}
}
final private static void testByteIntegral( ByteProcessor ipByte )
{
while( ipByte.getWidth() > 32 )
{
int w = ipByte.getWidth(),
h = ipByte.getHeight();
long[] l = FastIntegralImage.longIntegralImage((byte[])ipByte.getPixels(), w, h);
ipByte = new ByteProcessor(
w/2, h/2,
FastIntegralImage.scaleAreaAverage(l, w+1, h+1, w/2, h/2),
null);
}
}
/**
* @param args
*/
public static void main( final String[] args )
{
new ImageJ();
final Timer timer = new Timer();
final ImagePlus imp = new ImagePlus( "/home/saalfeld/tmp/fetter-example.tif" );
//final ImagePlus imp = new ImagePlus( "/home/albert/Desktop/t2/fetter-example.tif" );
//final ImagePlus imp = new ImagePlus( "/home/saalfeld/Desktop/norway.jpg" );
imp.show();
final ImageProcessor ip = imp.getProcessor().duplicate();
System.out.println( "short" );
final ShortProcessor ipShort = ( ShortProcessor )ip.convertToShort( false );
for ( int i = 0; i < n; ++i )
{
timer.start();
testShort( ipShort );
final long t = timer.stop();
System.out.println( i + ": " + t + "ms" );
}
System.out.println( "downsampleShort() + downsampleByteProcessor() (independent short with byte mapping + alpha)" );
for ( int i = 0; i < n; ++i )
{
final Pair< ShortProcessor, byte[] > ba = new Pair< ShortProcessor, byte[] >( ipShort, ( byte[] )ipShort.convertToByte( true ).getPixels() );
final ByteProcessor alpha = new ByteProcessor( ipShort.getWidth(), ipShort.getHeight(), ( byte[] )ipShort.convertToByte( true ).getPixels(), null );
timer.start();
testShortAlphaIndependently( ba, alpha );
final long t = timer.stop();
System.out.println( i + ": " + t + "ms" );
}
System.out.println( "downsampleShortProcessor() + convertToByte() + downsampleByteProcessor() (independent short + byte mapping + alpha)" );
for ( int i = 0; i < n; ++i )
{
final ByteProcessor alpha = new ByteProcessor( ipShort.getWidth(), ipShort.getHeight(), ( byte[] )ipShort.convertToByte( true ).getPixels(), null );
timer.start();
testShortAlphaByteMappingIndependently( ipShort, alpha );
final long t = timer.stop();
System.out.println( i + ": " + t + "ms" );
}
System.out.println( "float" );
final FloatProcessor ipFloat = ( FloatProcessor )ip.convertToFloat();
for ( int i = 0; i < n; ++i )
{
timer.start();
testFloat( ipFloat );
final long t = timer.stop();
System.out.println( i + ": " + t + "ms" );
}
System.out.println( "independent float with byte mapping + alpha" );
for ( int i = 0; i < n; ++i )
{
final Pair< FloatProcessor, byte[] > ba = new Pair< FloatProcessor, byte[] >( ipFloat, ( byte[] )ipShort.convertToByte( true ).getPixels() );
final ByteProcessor alpha = new ByteProcessor( ipShort.getWidth(), ipShort.getHeight(), ( byte[] )ipShort.convertToByte( true ).getPixels(), null );
timer.start();
testFloatAlphaIndependently( ba, alpha );
final long t = timer.stop();
System.out.println( i + ": " + t + "ms" );
}
System.out.println( "byte" );
final ByteProcessor ipByte = ( ByteProcessor )ip.convertToByte( true );
for ( int i = 0; i < n; ++i )
{
timer.start();
testByte( ipByte );
final long t = timer.stop();
System.out.println( i + ": " + t + "ms" );
}
System.out.println( "2 x byte" );
final ByteProcessor ipByte2 = ( ByteProcessor )ipByte.duplicate();
for ( int i = 0; i < n; ++i )
{
timer.start();
testByte( ipByte );
testByte( ipByte2 );
final long t = timer.stop();
System.out.println( i + ": " + t + "ms" );
}
System.out.println( "color" );
final ColorProcessor ipColor = ( ColorProcessor )ip.convertToRGB();
for ( int i = 0; i < n; ++i )
{
timer.start();
testColor( ipColor );
final long t = timer.stop();
System.out.println( i + ": " + t + "ms" );
}
System.out.println( "independent color with byte mapping + alpha" );
for ( int i = 0; i < n; ++i )
{
final byte[][] rgb = new byte[ 4 ][ ipColor.getWidth() * ipColor.getHeight() ];
ipColor.getRGB( rgb[ 0 ], rgb[ 1 ], rgb[ 2 ] );
final Pair< ColorProcessor, byte[][] > ba = new Pair< ColorProcessor, byte[][] >( ipColor, rgb );
final ByteProcessor alpha = new ByteProcessor( ipShort.getWidth(), ipShort.getHeight(), ( byte[] )ipShort.convertToByte( true ).getPixels(), null );
timer.start();
testColorAlphaIndependently( ba, alpha );
final long t = timer.stop();
System.out.println( i + ": " + t + "ms" );
}
System.out.println( "alpha + outside" );
for ( int i = 0; i < n; ++i )
{
ByteProcessor outside = new ByteProcessor( ipByte.getWidth(), ipByte.getHeight() );
outside.setRoi( new OvalRoi( 100, 100, ipByte.getWidth() - 200, ipByte.getHeight() - 200 ) );
outside.setValue( 255 );
outside.fill(outside.getMask());
final Pair< ByteProcessor, ByteProcessor > ba = new Pair< ByteProcessor, ByteProcessor >( ipByte, outside );
timer.start();
testAlphaOutside( ba );
final long t = timer.stop();
System.out.println( i + ": " + t + "ms" );
}
System.out.println( "outside" );
for ( int i = 0; i < n; ++i )
{
ByteProcessor outside = new ByteProcessor( ipByte.getWidth(), ipByte.getHeight() );
outside.setRoi( new OvalRoi( 100, 100, ipByte.getWidth() - 200, ipByte.getHeight() - 200 ) );
outside.setValue( 255 );
outside.fill(outside.getMask());
timer.start();
testOutside( outside );
final long t = timer.stop();
System.out.println( i + ": " + t + "ms" );
}
// System.out.println( "byte integral" );
// final ByteProcessor ipByteI = ( ByteProcessor )ipShort.convertToByte( true );
//
// for ( int i = 0; i < 10; ++i )
// {
// timer.start();
// testByteIntegral( ipByteI );
// final long t = timer.stop();
// System.out.println( i + ": " + t + "ms" );
// }
}
}