Skip to content

Commit 2fe0ce8

Browse files
authored
Merge pull request #88 from imglib/ImgMath-blockread
ImgMath blockread and offset
2 parents e2fa1fe + a1f9c48 commit 2fe0ce8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+2358
-424
lines changed

src/main/java/net/imglib2/algorithm/math/Add.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,21 @@ public Add( final Object... obs )
2323
}
2424

2525
@Override
26-
public < O extends RealType< O > > Addition< O > reInit(
26+
public < O extends RealType< O > > OFunction< O > reInit(
2727
final O tmp,
2828
final Map< String, LetBinding< O > > bindings,
2929
final Converter< RealType< ? >, O > converter,
3030
final Map< Variable< O >, OFunction< O > > imgSources )
3131
{
32-
return new Addition< O >( tmp.copy(),
33-
this.a.reInit( tmp, bindings, converter, imgSources ),
34-
this.b.reInit( tmp, bindings, converter, imgSources ) );
32+
final OFunction< O > a = this.a.reInit( tmp, bindings, converter, imgSources ),
33+
b = this.b.reInit( tmp, bindings, converter, imgSources );
34+
35+
// Optimization: remove null ops
36+
if ( a.isZero() )
37+
return b;
38+
if ( b.isZero() )
39+
return a;
40+
41+
return new Addition< O >( tmp.copy(), a, b );
3542
}
3643
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package net.imglib2.algorithm.math;
2+
3+
import java.util.Map;
4+
5+
import net.imglib2.algorithm.math.abstractions.ABinaryFunction;
6+
import net.imglib2.algorithm.math.abstractions.ABooleanFunction;
7+
import net.imglib2.algorithm.math.abstractions.OFunction;
8+
import net.imglib2.algorithm.math.execution.LetBinding;
9+
import net.imglib2.algorithm.math.execution.LogicalAnd;
10+
import net.imglib2.algorithm.math.execution.LogicalAndBoolean;
11+
import net.imglib2.algorithm.math.execution.Variable;
12+
import net.imglib2.converter.Converter;
13+
import net.imglib2.type.numeric.RealType;
14+
15+
public class AndLogical extends ABinaryFunction
16+
{
17+
public AndLogical( final Object c1, final Object c2 )
18+
{
19+
super( c1, c2 );
20+
}
21+
22+
public AndLogical( final Object... c )
23+
{
24+
super( c );
25+
}
26+
27+
@Override
28+
public < O extends RealType< O > > OFunction< O > reInit(
29+
final O tmp,
30+
final Map< String, LetBinding< O > > bindings,
31+
final Converter< RealType< ? >, O > converter,
32+
final Map< Variable< O >, OFunction< O > > imgSources )
33+
{
34+
final OFunction< O > a = this.a.reInit( tmp, bindings, converter, imgSources );
35+
final OFunction< O > b = this.b.reInit( tmp, bindings, converter, imgSources );
36+
37+
38+
return a instanceof ABooleanFunction< ? > && b instanceof ABooleanFunction< ? > ?
39+
new LogicalAndBoolean< O >( tmp.createVariable(), a, b )
40+
: new LogicalAnd< O >( tmp.createVariable(), a, b );
41+
}
42+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package net.imglib2.algorithm.math;
2+
3+
import java.util.Map;
4+
import java.util.stream.LongStream;
5+
6+
import net.imglib2.RandomAccessible;
7+
import net.imglib2.algorithm.math.abstractions.IFunction;
8+
import net.imglib2.algorithm.math.abstractions.OFunction;
9+
import net.imglib2.algorithm.math.abstractions.RandomAccessOnly;
10+
import net.imglib2.algorithm.math.abstractions.ViewableFunction;
11+
import net.imglib2.algorithm.math.execution.BlockReadingSource;
12+
import net.imglib2.algorithm.math.execution.BlockReadingDirect;
13+
import net.imglib2.algorithm.math.execution.LetBinding;
14+
import net.imglib2.algorithm.math.execution.Variable;
15+
import net.imglib2.converter.Converter;
16+
import net.imglib2.type.numeric.RealType;
17+
18+
/**
19+
* Intended for reading cuboid blocks out of an integral image.
20+
*
21+
* @author Albert Cardona
22+
*
23+
* @param <I> The {@code Type} of the {@code RandomAccessible} from which to read blocks.
24+
*/
25+
public final class BlockReadSource< I extends RealType< I > > extends ViewableFunction implements IFunction, RandomAccessOnly< I >
26+
{
27+
final private RandomAccessible< I > src;
28+
final private long[][] corners;
29+
final private byte[] signs;
30+
31+
/**
32+
* A block centered on a particular pixel.
33+
*
34+
* @param src A {@code RandomAccessible} such as an @{code IntegralImg}, presumably a {@code RandomAccessibleInterval} that was extended with an {@code OutOfBounds} strategy.
35+
* @param blockRadius Array of half of the length of each side of the block.
36+
*/
37+
public BlockReadSource( final RandomAccessible< I > src, final long[] blockRadius )
38+
{
39+
this.src = src;
40+
this.corners = new long[ ( int )Math.pow( 2, blockRadius.length ) ][ blockRadius.length ];
41+
42+
// All possible combinations to define all corners, sorted with lower dimensions being the slower-moving,
43+
// following Gray code (see https://en.wikipedia.org/wiki/Gray_code )
44+
for (int d = 0; d < src.numDimensions(); ++d )
45+
{
46+
final int cycle = corners.length / ( int )Math.pow( 2, d + 1 );
47+
long inc = blockRadius[ d ];
48+
for (int i = 0; i < corners.length; ++i )
49+
{
50+
if ( 0 == i % cycle) inc *= -1;
51+
corners[ i ][ d ] = inc;
52+
//System.out.println("corners[" + i + "][" + d + "] = " + corners[i][d]);
53+
}
54+
}
55+
this.signs = BlockReadSource.signsArray( src );
56+
//for (int i=0; i<signs.length; ++i)
57+
// System.out.println("signs[" + i + "] = " + signs[i]);
58+
}
59+
60+
/**
61+
* A block centered on a particular pixel.
62+
*
63+
* @param src A {@code RandomAccessible} such as an @{code IntegralImg}, presumably a {@code RandomAccessibleInterval} that was extended with an {@code OutOfBounds} strategy.
64+
* @param blockRadius Half of the length of the side of the block in every dimension.
65+
*/
66+
public BlockReadSource( final RandomAccessible< I > src, final long blockRadius )
67+
{
68+
this( src, LongStream.generate( () -> blockRadius ).limit( src.numDimensions() ).toArray() );
69+
}
70+
71+
static public byte[] signsArray( final RandomAccessible< ? > src )
72+
{
73+
switch ( src.numDimensions() )
74+
{
75+
case 1:
76+
return new byte[]{ -1, 1 };
77+
case 2:
78+
// 2D: S( (x1,y1), (x2,y2) = C( x1,y1 )
79+
// -C( x1,y2 )
80+
// -C( x2,y1 )
81+
// +C( x2,y2 )
82+
// Corners as: (x1, y1), (x1, y2), (x2, y1), (x2, y2)
83+
return new byte[]{ 1, -1, -1, 1 };
84+
case 3:
85+
// 3D: S( (x1,y1,z1) to (x2,y2,z2) ) = - C( x1, y1, z1 )
86+
// + C( x1, y1, z2 )
87+
// + C( x1, y2, z1 )
88+
// - C( x1, y2, z2 )
89+
// + C( x2, y1, z1 )
90+
// - C( x2, y1, z2 )
91+
// - C( x2, y2, z1 )
92+
// + C( x2, y2, z2 )
93+
return new byte[]{ -1, 1, 1, -1, 1, -1, -1, 1 };
94+
default:
95+
// There's a clear pattern, but I can't find the time now to break through it
96+
// Must re-read Tapias 2011 doi:10.1016/j.patrec.2010.10.007
97+
// for the use of the Mobius function for determining the sign
98+
throw new UnsupportedOperationException( "Sorry, numDimensions " + src.numDimensions() + " not supported yet." );
99+
}
100+
}
101+
102+
/**
103+
*
104+
* @param src
105+
* @param corners In coordinate moves relative to a pixel's location.
106+
*/
107+
public BlockReadSource( final RandomAccessible< I > src, final long[][] corners )
108+
{
109+
this.src = src;
110+
this.corners = corners;
111+
this.signs = BlockReadSource.signsArray( src );
112+
}
113+
114+
@SuppressWarnings("unchecked")
115+
@Override
116+
public < O extends RealType< O > > OFunction< O > reInit(
117+
final O tmp,
118+
final Map< String, LetBinding< O > > bindings,
119+
final Converter< RealType< ? >, O > converter,
120+
final Map< Variable< O >, OFunction< O > > imgSources )
121+
{
122+
if ( tmp.getClass() == src.randomAccess().get().getClass() )
123+
return new BlockReadingDirect< O >( tmp.copy(), ( RandomAccessible< O > )this.src, this.corners, this.signs );
124+
return new BlockReadingSource< I, O >(
125+
tmp.copy(),
126+
converter,
127+
this.src,
128+
this.corners,
129+
this.signs );
130+
}
131+
132+
public RandomAccessible< I > getRandomAccessible()
133+
{
134+
return this.src;
135+
}
136+
}

0 commit comments

Comments
 (0)