Merge branch 'master' of ssh://repo.or.cz/srv/git/trakem2
[trakem2.git] / DownsamplerTest.java
blobba1becab35265bfa6b3c16e885c412cad4ad3ba0
1 import ij.ImageJ;
2 import ij.ImagePlus;
3 import ij.gui.OvalRoi;
4 import ij.process.ByteProcessor;
5 import ij.process.ColorProcessor;
6 import ij.process.FloatProcessor;
7 import ij.process.ImageProcessor;
8 import ij.process.ShortProcessor;
9 import ini.trakem2.imaging.FastIntegralImage;
10 import mpicbg.trakem2.util.Downsampler;
11 import mpicbg.trakem2.util.Downsampler.Pair;
12 import mpicbg.util.Timer;
14 /**
15 * License: GPL
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License 2
19 * as published by the Free Software Foundation.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 /**
34 * @author Stephan Saalfeld <saalfeld@mpi-cbg.de>
35 * @version 0.1a
37 public class DownsamplerTest
39 final static int n = 20;
41 final private static void testShort( ShortProcessor ipShort )
43 final double min = ipShort.getMin();
44 final double max = ipShort.getMax();
46 while( ipShort.getWidth() > 32 )
48 ipShort = Downsampler.downsampleShortProcessor( ipShort );
49 ipShort.setMinAndMax( min, max );
53 final private static void testFloat( FloatProcessor ipFloat )
55 final double min = ipFloat.getMin();
56 final double max = ipFloat.getMax();
58 while( ipFloat.getWidth() > 32 )
60 ipFloat = Downsampler.downsampleFloatProcessor( ipFloat );
61 ipFloat.setMinAndMax( min, max );
65 final private static void testByte( ByteProcessor ipByte )
67 while( ipByte.getWidth() > 32 )
68 ipByte = Downsampler.downsampleByteProcessor( ipByte );
71 final private static void testColor( ColorProcessor ipColor )
73 while( ipColor.getWidth() > 32 )
74 ipColor = Downsampler.downsampleColorProcessor( ipColor );
77 /**
78 * Test downsampling the pyramid of a short image + byte alpha channel
79 * including the byte mapping of the short doing the alpha channel in a
80 * separate loop.
82 * @param ba
84 final private static void testShortAlphaIndependently( Pair< ShortProcessor, byte[] > ba, ByteProcessor alpha )
86 while( ba.a.getWidth() > 32 )
88 ba = Downsampler.downsampleShort( ba.a );
89 alpha = Downsampler.downsampleByteProcessor( alpha );
90 // new ImagePlus( "pixels " + ba.a.getWidth(), ba.a ).show();
91 // new ImagePlus( "pixels to byte " + ba.a.getWidth(), new ByteProcessor( ba.a.getWidth(), ba.a.getHeight(), ba.b[ 0 ], null ) ).show();
92 // new ImagePlus( "alpha " + ba.a.getWidth(), new ByteProcessor( ba.a.getWidth(), ba.a.getHeight(), ba.b[ 1 ], null ) ).show();
96 /**
97 * Test downsampling the pyramid of a short image + byte alpha channel
98 * including the byte mapping of the short all in separate loops.
100 * @param ba
102 final private static void testShortAlphaByteMappingIndependently( ShortProcessor sp, ByteProcessor alpha )
104 ByteProcessor bp;
105 while( sp.getWidth() > 32 )
107 sp = Downsampler.downsampleShortProcessor( sp );
108 bp = ( ByteProcessor )sp.convertToByte( true );
109 alpha = Downsampler.downsampleByteProcessor( alpha );
110 // new ImagePlus( "pixels " + ba.a.getWidth(), ba.a ).show();
111 // new ImagePlus( "pixels to byte " + ba.a.getWidth(), new ByteProcessor( ba.a.getWidth(), ba.a.getHeight(), ba.b[ 0 ], null ) ).show();
112 // new ImagePlus( "alpha " + ba.a.getWidth(), new ByteProcessor( ba.a.getWidth(), ba.a.getHeight(), ba.b[ 1 ], null ) ).show();
117 * Test downsampling the pyramid of a short image + byte alpha channel
118 * including the byte mapping of the short doing the alpha channel in a
119 * separate loop.
121 * @param ba
123 final private static void testFloatAlphaIndependently( Pair< FloatProcessor, byte[] > ba, ByteProcessor alpha )
125 while( ba.a.getWidth() > 32 )
127 ba = Downsampler.downsampleFloat( ba.a );
128 alpha = Downsampler.downsampleByteProcessor( alpha );
129 // new ImagePlus( "pixels " + ba.a.getWidth(), ba.a ).show();
130 // new ImagePlus( "pixels to byte " + ba.a.getWidth(), new ByteProcessor( ba.a.getWidth(), ba.a.getHeight(), ba.b[ 0 ], null ) ).show();
131 // new ImagePlus( "alpha " + ba.a.getWidth(), new ByteProcessor( ba.a.getWidth(), ba.a.getHeight(), ba.b[ 1 ], null ) ).show();
136 * Test downsampling the pyramid of a short image + byte alpha channel
137 * including the byte mapping of the short doing the alpha channel in a
138 * separate loop.
140 * @param ba
142 final private static void testColorAlphaIndependently( Pair< ColorProcessor, byte[][] > ba, ByteProcessor alpha )
144 while( ba.a.getWidth() > 32 )
146 ba = Downsampler.downsampleColor( ba.a );
147 alpha = Downsampler.downsampleByteProcessor( alpha );
148 // new ImagePlus( "pixels " + ba.a.getWidth(), ba.a ).show();
149 // new ImagePlus( "pixels to byte " + ba.a.getWidth(), new ByteProcessor( ba.a.getWidth(), ba.a.getHeight(), ba.b[ 0 ], null ) ).show();
150 // new ImagePlus( "alpha " + ba.a.getWidth(), new ByteProcessor( ba.a.getWidth(), ba.a.getHeight(), ba.b[ 1 ], null ) ).show();
155 final private static void testAlphaOutside( Pair< ByteProcessor, ByteProcessor > ba )
157 while( ba.a.getWidth() > 32 )
159 ba = Downsampler.downsampleAlphaAndOutside( ba.a, ba.b );
160 // new ImagePlus( "alpha " + ba.a.getWidth(), ba.a ).show();
161 // new ImagePlus( "outside " + ba.b.getWidth(), ba.b ).show();
165 final private static void testOutside( ByteProcessor a )
167 while( a.getWidth() > 32 )
169 a = Downsampler.downsampleOutside( a );
170 // new ImagePlus( "alpha " + ba.a.getWidth(), ba.a ).show();
171 // new ImagePlus( "outside " + ba.b.getWidth(), ba.b ).show();
176 final private static void testByteIntegral( ByteProcessor ipByte )
178 while( ipByte.getWidth() > 32 )
180 int w = ipByte.getWidth(),
181 h = ipByte.getHeight();
182 long[] l = FastIntegralImage.longIntegralImage((byte[])ipByte.getPixels(), w, h);
183 ipByte = new ByteProcessor(
184 w/2, h/2,
185 FastIntegralImage.scaleAreaAverage(l, w+1, h+1, w/2, h/2),
186 null);
192 * @param args
194 public static void main( final String[] args )
196 new ImageJ();
197 final Timer timer = new Timer();
199 final ImagePlus imp = new ImagePlus( "/home/saalfeld/tmp/fetter-example.tif" );
200 //final ImagePlus imp = new ImagePlus( "/home/albert/Desktop/t2/fetter-example.tif" );
201 //final ImagePlus imp = new ImagePlus( "/home/saalfeld/Desktop/norway.jpg" );
202 imp.show();
204 final ImageProcessor ip = imp.getProcessor().duplicate();
206 System.out.println( "short" );
207 final ShortProcessor ipShort = ( ShortProcessor )ip.convertToShort( false );
209 for ( int i = 0; i < n; ++i )
211 timer.start();
212 testShort( ipShort );
213 final long t = timer.stop();
214 System.out.println( i + ": " + t + "ms" );
217 System.out.println( "downsampleShort() + downsampleByteProcessor() (independent short with byte mapping + alpha)" );
219 for ( int i = 0; i < n; ++i )
221 final Pair< ShortProcessor, byte[] > ba = new Pair< ShortProcessor, byte[] >( ipShort, ( byte[] )ipShort.convertToByte( true ).getPixels() );
222 final ByteProcessor alpha = new ByteProcessor( ipShort.getWidth(), ipShort.getHeight(), ( byte[] )ipShort.convertToByte( true ).getPixels(), null );
223 timer.start();
224 testShortAlphaIndependently( ba, alpha );
225 final long t = timer.stop();
226 System.out.println( i + ": " + t + "ms" );
229 System.out.println( "downsampleShortProcessor() + convertToByte() + downsampleByteProcessor() (independent short + byte mapping + alpha)" );
231 for ( int i = 0; i < n; ++i )
233 final ByteProcessor alpha = new ByteProcessor( ipShort.getWidth(), ipShort.getHeight(), ( byte[] )ipShort.convertToByte( true ).getPixels(), null );
234 timer.start();
235 testShortAlphaByteMappingIndependently( ipShort, alpha );
236 final long t = timer.stop();
237 System.out.println( i + ": " + t + "ms" );
240 System.out.println( "float" );
241 final FloatProcessor ipFloat = ( FloatProcessor )ip.convertToFloat();
243 for ( int i = 0; i < n; ++i )
245 timer.start();
246 testFloat( ipFloat );
247 final long t = timer.stop();
248 System.out.println( i + ": " + t + "ms" );
251 System.out.println( "independent float with byte mapping + alpha" );
253 for ( int i = 0; i < n; ++i )
255 final Pair< FloatProcessor, byte[] > ba = new Pair< FloatProcessor, byte[] >( ipFloat, ( byte[] )ipShort.convertToByte( true ).getPixels() );
256 final ByteProcessor alpha = new ByteProcessor( ipShort.getWidth(), ipShort.getHeight(), ( byte[] )ipShort.convertToByte( true ).getPixels(), null );
257 timer.start();
258 testFloatAlphaIndependently( ba, alpha );
259 final long t = timer.stop();
260 System.out.println( i + ": " + t + "ms" );
263 System.out.println( "byte" );
264 final ByteProcessor ipByte = ( ByteProcessor )ip.convertToByte( true );
266 for ( int i = 0; i < n; ++i )
268 timer.start();
269 testByte( ipByte );
270 final long t = timer.stop();
271 System.out.println( i + ": " + t + "ms" );
274 System.out.println( "2 x byte" );
275 final ByteProcessor ipByte2 = ( ByteProcessor )ipByte.duplicate();
277 for ( int i = 0; i < n; ++i )
279 timer.start();
280 testByte( ipByte );
281 testByte( ipByte2 );
282 final long t = timer.stop();
283 System.out.println( i + ": " + t + "ms" );
287 System.out.println( "color" );
288 final ColorProcessor ipColor = ( ColorProcessor )ip.convertToRGB();
290 for ( int i = 0; i < n; ++i )
292 timer.start();
293 testColor( ipColor );
294 final long t = timer.stop();
295 System.out.println( i + ": " + t + "ms" );
298 System.out.println( "independent color with byte mapping + alpha" );
300 for ( int i = 0; i < n; ++i )
302 final byte[][] rgb = new byte[ 4 ][ ipColor.getWidth() * ipColor.getHeight() ];
303 ipColor.getRGB( rgb[ 0 ], rgb[ 1 ], rgb[ 2 ] );
304 final Pair< ColorProcessor, byte[][] > ba = new Pair< ColorProcessor, byte[][] >( ipColor, rgb );
305 final ByteProcessor alpha = new ByteProcessor( ipShort.getWidth(), ipShort.getHeight(), ( byte[] )ipShort.convertToByte( true ).getPixels(), null );
306 timer.start();
307 testColorAlphaIndependently( ba, alpha );
308 final long t = timer.stop();
309 System.out.println( i + ": " + t + "ms" );
312 System.out.println( "alpha + outside" );
314 for ( int i = 0; i < n; ++i )
316 ByteProcessor outside = new ByteProcessor( ipByte.getWidth(), ipByte.getHeight() );
317 outside.setRoi( new OvalRoi( 100, 100, ipByte.getWidth() - 200, ipByte.getHeight() - 200 ) );
318 outside.setValue( 255 );
319 outside.fill(outside.getMask());
320 final Pair< ByteProcessor, ByteProcessor > ba = new Pair< ByteProcessor, ByteProcessor >( ipByte, outside );
321 timer.start();
322 testAlphaOutside( ba );
323 final long t = timer.stop();
324 System.out.println( i + ": " + t + "ms" );
327 System.out.println( "outside" );
329 for ( int i = 0; i < n; ++i )
331 ByteProcessor outside = new ByteProcessor( ipByte.getWidth(), ipByte.getHeight() );
332 outside.setRoi( new OvalRoi( 100, 100, ipByte.getWidth() - 200, ipByte.getHeight() - 200 ) );
333 outside.setValue( 255 );
334 outside.fill(outside.getMask());
335 timer.start();
336 testOutside( outside );
337 final long t = timer.stop();
338 System.out.println( i + ": " + t + "ms" );
342 // System.out.println( "byte integral" );
343 // final ByteProcessor ipByteI = ( ByteProcessor )ipShort.convertToByte( true );
345 // for ( int i = 0; i < 10; ++i )
346 // {
347 // timer.start();
348 // testByteIntegral( ipByteI );
349 // final long t = timer.stop();
350 // System.out.println( i + ": " + t + "ms" );
351 // }