Skip to content

Instantly share code, notes, and snippets.

@tpietzsch
Created February 19, 2016 18:25
Show Gist options
  • Save tpietzsch/6827e7bc7781288fa240 to your computer and use it in GitHub Desktop.
Save tpietzsch/6827e7bc7781288fa240 to your computer and use it in GitHub Desktop.
public static < T extends Type< T > & Comparable< T > >
void maxFilterBox(
final RandomAccessibleInterval< T > input,
final RandomAccessibleInterval< T > output,
final ImgFactory< T > imgFactory, final T type,
final int span )
{
final int n = input.numDimensions();
final long[][] tmpdims = new long[ n ][];
final long[][] tmpoffsets = new long[ n ][];
tmpdims[ n - 1 ] = new long[ n ];
tmpoffsets[ n - 1 ] = new long[ n ];
output.dimensions( tmpdims[ n - 1 ] );
output.min( tmpoffsets[ n - 1 ] );
for ( int d = n - 2; d >= 0; --d )
{
tmpdims[ d ] = tmpdims[ d + 1 ].clone();
tmpdims[ d ][ d + 1 ] += 2 * span;
tmpoffsets[ d ] = tmpoffsets[ d + 1 ].clone();
tmpoffsets[ d ][ d + 1 ] -= span;
}
RandomAccessibleInterval< T > currentOutput = null;
final long[] spanMin = new long[ n ];
final long[] spanMax = new long[ n ];
for ( int d = 0; d < n; ++d )
{
Arrays.fill( spanMin, 0 );
Arrays.fill( spanMax, 0 );
spanMin[ d ] = -span;
spanMax[ d ] = span;
final FinalInterval spanInterval = new FinalInterval( spanMin, spanMax );
final RandomAccessibleInterval< T > currentInput = ( d == 0 ) ? input : currentOutput;
currentOutput = ( d == n - 1 ) ? output : Views.interval( Views.translate( imgFactory.create( tmpdims[ d ], type ), tmpoffsets[ d ] ), output );
final NeighborhoodsIterableInterval< T > neighborhoods = new RectangleShape.NeighborhoodsIterableInterval<>( currentInput, spanInterval, RectangleNeighborhoodUnsafe.< T >factory() );
final RandomAccess< T > out = currentOutput.randomAccess();
for ( final Neighborhood< T > neighborhood : neighborhoods )
{
out.setPosition( neighborhood );
final T o = out.get();
o.set( neighborhood.firstElement() );
for ( final T i : neighborhood )
if ( i.compareTo( o ) > 0 )
o.set( i );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment