Skip to content

Commit 86f337c

Browse files
committed
Downsample examples
1 parent 3711a2f commit 86f337c

File tree

2 files changed

+123
-5
lines changed

2 files changed

+123
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*-
2+
* #%L
3+
* ImgLib2: a general-purpose, multidimensional image processing library.
4+
* %%
5+
* Copyright (C) 2009 - 2024 Tobias Pietzsch, Stephan Preibisch, Stephan Saalfeld,
6+
* John Bogovic, Albert Cardona, Barry DeZonia, Christian Dietz, Jan Funke,
7+
* Aivar Grislis, Jonathan Hale, Grant Harris, Stefan Helfrich, Mark Hiner,
8+
* Martin Horn, Steffen Jaensch, Lee Kamentsky, Larry Lindsey, Melissa Linkert,
9+
* Mark Longair, Brian Northan, Nick Perry, Curtis Rueden, Johannes Schindelin,
10+
* Jean-Yves Tinevez and Michael Zinsmaier.
11+
* %%
12+
* Redistribution and use in source and binary forms, with or without
13+
* modification, are permitted provided that the following conditions are met:
14+
*
15+
* 1. Redistributions of source code must retain the above copyright notice,
16+
* this list of conditions and the following disclaimer.
17+
* 2. Redistributions in binary form must reproduce the above copyright notice,
18+
* this list of conditions and the following disclaimer in the documentation
19+
* and/or other materials provided with the distribution.
20+
*
21+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
25+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31+
* POSSIBILITY OF SUCH DAMAGE.
32+
* #L%
33+
*/
34+
package net.imglib2.algorithm.blocks.downsample;
35+
36+
import java.util.Arrays;
37+
38+
import bdv.cache.SharedQueue;
39+
import bdv.util.Bdv;
40+
import bdv.util.BdvFunctions;
41+
import bdv.util.BdvSource;
42+
import bdv.util.volatiles.VolatileViews;
43+
import bdv.viewer.DisplayMode;
44+
import ij.IJ;
45+
import ij.ImagePlus;
46+
import net.imglib2.algorithm.blocks.BlockAlgoUtils;
47+
import net.imglib2.algorithm.blocks.BlockSupplier;
48+
import net.imglib2.algorithm.blocks.UnaryBlockOperator;
49+
import net.imglib2.algorithm.blocks.convert.Convert;
50+
import net.imglib2.algorithm.blocks.downsample.Downsample.Offset;
51+
import net.imglib2.cache.img.CachedCellImg;
52+
import net.imglib2.img.Img;
53+
import net.imglib2.img.display.imagej.ImageJFunctions;
54+
import net.imglib2.type.numeric.ARGBType;
55+
import net.imglib2.type.numeric.integer.UnsignedByteType;
56+
import net.imglib2.view.Views;
57+
58+
public class DownsampleBdvPlayground2
59+
{
60+
public static void main( String[] args )
61+
{
62+
System.setProperty("apple.laf.useScreenMenuBar", "true");
63+
64+
final String fn = "/Users/pietzsch/workspace/data/e002_stack_fused-8bit.tif";
65+
final ImagePlus imp = IJ.openImage( fn );
66+
final Img< UnsignedByteType > img = ImageJFunctions.wrapByte( imp );
67+
68+
final BdvSource bdv = BdvFunctions.show(
69+
img,
70+
"img",
71+
Bdv.options() );
72+
bdv.setColor( new ARGBType( 0xffffff ) );
73+
bdv.setDisplayRange( 0, 255 );
74+
bdv.getBdvHandle().getViewerPanel().setDisplayMode( DisplayMode.SINGLE );
75+
76+
final boolean[] downsampleInDim = { true, true, true };
77+
final long[] downsampledDimensions = Downsample.getDownsampledDimensions( img.dimensionsAsLongArray(), downsampleInDim );
78+
final int[] cellDimensions = { 64, 64, 64 };
79+
80+
final double[] calib = new double[ 3 ];
81+
Arrays.setAll(calib, d -> downsampleInDim[ d ] ? 2 : 1 );
82+
83+
final BlockSupplier< UnsignedByteType > blocks = BlockSupplier
84+
.of( Views.extendMirrorDouble( img ) )
85+
.andThen( Downsample.downsample(
86+
img.getType(),
87+
Offset.HALF_PIXEL,
88+
img.numDimensions() ) );
89+
90+
final Img< UnsignedByteType > downsampled = BlockAlgoUtils.cellImg(
91+
blocks,
92+
downsampledDimensions,
93+
cellDimensions );
94+
95+
final BdvSource out = BdvFunctions.show(
96+
VolatileViews.wrapAsVolatile( downsampled, new SharedQueue( 32, 1 ) ),
97+
"downsampled half-pixel",
98+
Bdv.options()
99+
.addTo( bdv )
100+
.sourceTransform( calib )
101+
);
102+
out.setDisplayRange( 0, 255 );
103+
out.setColor( new ARGBType( 0x00ff00 ) );
104+
}
105+
}

src/test/java/net/imglib2/algorithm/blocks/downsample/DownsampleDoublePlayground.java

+18-5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
import ij.IJ;
4242
import ij.ImagePlus;
4343
import java.util.Arrays;
44+
45+
import net.imglib2.algorithm.blocks.BlockSupplier;
4446
import net.imglib2.algorithm.blocks.downsample.DownsampleBlockProcessors.CenterDouble;
4547
import net.imglib2.algorithm.blocks.downsample.DownsampleBlockProcessors.HalfPixelDouble;
4648
import net.imglib2.algorithm.blocks.BlockAlgoUtils;
@@ -73,15 +75,20 @@ public static void main( String[] args )
7375
bdv.setDisplayRange( 0, 255 );
7476
bdv.getBdvHandle().getViewerPanel().setDisplayMode( DisplayMode.SINGLE );
7577

76-
final PrimitiveBlocks< DoubleType > blocks = PrimitiveBlocks.of(
77-
Converters.convert( Views.extendBorder( img ), new RealDoubleConverter<>(), new DoubleType() )
78-
).threadSafe();
78+
final BlockSupplier< DoubleType > blocks = BlockSupplier.of(
79+
Converters.convert( Views.extendBorder( img ), new RealDoubleConverter<>(), new DoubleType() ) );
7980

8081
final boolean[] downsampleInDim = { true, true, true };
8182
final long[] downsampledDimensions = Downsample.getDownsampledDimensions( img.dimensionsAsLongArray(), downsampleInDim );
8283
final int[] cellDimensions = { 64, 64, 64 };
8384
final CachedCellImg< DoubleType, ? > downsampled = BlockAlgoUtils.cellImg(
84-
blocks, new CenterDouble( downsampleInDim ), new DoubleType(), downsampledDimensions, cellDimensions );
85+
blocks.andThen( Downsample.downsample(
86+
new DoubleType(),
87+
Downsample.ComputationType.DOUBLE,
88+
Downsample.Offset.CENTERED,
89+
3 )
90+
),
91+
downsampledDimensions, cellDimensions );
8592

8693
final double[] calib = new double[ 3 ];
8794
Arrays.setAll(calib, d -> downsampleInDim[ d ] ? 2 : 1 );
@@ -95,7 +102,13 @@ public static void main( String[] args )
95102
// out.setColor( new ARGBType( 0xff0000 ) );
96103

97104
final CachedCellImg< DoubleType, ? > downsampled2 = BlockAlgoUtils.cellImg(
98-
blocks, new HalfPixelDouble( downsampleInDim ), new DoubleType(), downsampledDimensions, cellDimensions );
105+
blocks.andThen( Downsample.downsample(
106+
new DoubleType(),
107+
Downsample.ComputationType.DOUBLE,
108+
Downsample.Offset.HALF_PIXEL,
109+
3 )
110+
),
111+
downsampledDimensions, cellDimensions );
99112
final BdvSource out2 = BdvFunctions.show(
100113
VolatileViews.wrapAsVolatile( downsampled2 ),
101114
"downsampled half-pixel",

0 commit comments

Comments
 (0)