|
34 | 34 |
|
35 | 35 | package net.imglib2.algorithm.neighborhood;
|
36 | 36 |
|
| 37 | +import java.util.stream.LongStream; |
| 38 | + |
37 | 39 | import net.imglib2.Cursor;
|
| 40 | +import net.imglib2.FinalInterval; |
| 41 | +import net.imglib2.Interval; |
38 | 42 | import net.imglib2.IterableInterval;
|
39 | 43 | import net.imglib2.RandomAccess;
|
40 | 44 | import net.imglib2.RandomAccessible;
|
41 | 45 | import net.imglib2.RandomAccessibleInterval;
|
42 | 46 | import net.imglib2.Sampler;
|
| 47 | +import net.imglib2.util.ConstantUtils; |
43 | 48 |
|
44 | 49 | /**
|
45 | 50 | * A factory for Accessibles on {@link Neighborhood Neighborhoods}.
|
|
48 | 53 | */
|
49 | 54 | public interface Shape
|
50 | 55 | {
|
| 56 | + |
| 57 | + /** |
| 58 | + * Get the bounding box for a {@link Shape} with {@code numDimensions} |
| 59 | + * dimensions. |
| 60 | + * <p> |
| 61 | + * Providing {@code numDimensions} is required since the input from which |
| 62 | + * {@link Neighborhood neighborhoods} are generated is not known yet. The |
| 63 | + * bounding box is described by an {@link Interval} with the center of the |
| 64 | + * bounding box located at zero. |
| 65 | + * </p> |
| 66 | + * <p> |
| 67 | + * The values of this bounding box should only be used to determine the extent |
| 68 | + * of the {@link Shape}, ignoring the absolute {@link Interval#min(int) min} |
| 69 | + * and {@link Interval#max(int) max} values. |
| 70 | + * </p> |
| 71 | + * |
| 72 | + * @param numDimensions dimensions of the {@link Shape} |
| 73 | + * @return an {@link Interval} that describes the bounding box of a |
| 74 | + * {@link Shape} |
| 75 | + */ |
| 76 | + public default Interval getStructuringElementBoundingBox( final int numDimensions ) |
| 77 | + { |
| 78 | + final RandomAccessible< Object > accessible = ConstantUtils.constantRandomAccessible( null, numDimensions ); |
| 79 | + final RandomAccess< Neighborhood< Object > > access = neighborhoodsRandomAccessible( accessible ).randomAccess(); |
| 80 | + access.setPosition( new long[ numDimensions ] ); |
| 81 | + final long[] min = LongStream.generate( () -> Long.MAX_VALUE ).limit( numDimensions ).toArray(); |
| 82 | + final long[] max = LongStream.generate( () -> Long.MIN_VALUE ).limit( numDimensions ).toArray(); |
| 83 | + for ( final Cursor< Object > cursor = access.get().localizingCursor(); cursor.hasNext(); ) |
| 84 | + { |
| 85 | + cursor.fwd(); |
| 86 | + for ( int d = 0; d < numDimensions; ++d ) |
| 87 | + { |
| 88 | + final long pos = cursor.getLongPosition( d ); |
| 89 | + min[ d ] = Math.min( pos, min[ d ] ); |
| 90 | + max[ d ] = Math.max( pos, max[ d ] ); |
| 91 | + } |
| 92 | + } |
| 93 | + return new FinalInterval( min, max ); |
| 94 | + } |
| 95 | + |
51 | 96 | /**
|
52 | 97 | * Get an {@link IterableInterval} that contains all {@link Neighborhood
|
53 | 98 | * Neighborhoods} of the source image.
|
|
0 commit comments