diff --git a/src/main/java/fr/pasteur/ida/zellige/utils/LocalOtsu.java b/src/main/java/fr/pasteur/ida/zellige/utils/LocalOtsuClassification.java similarity index 52% rename from src/main/java/fr/pasteur/ida/zellige/utils/LocalOtsu.java rename to src/main/java/fr/pasteur/ida/zellige/utils/LocalOtsuClassification.java index 8705d83cd3e8e4ae993bc1ead28f328c6487d168..7d9cb60c7a48288b39f40e85dc743b4ed84b75d6 100644 --- a/src/main/java/fr/pasteur/ida/zellige/utils/LocalOtsu.java +++ b/src/main/java/fr/pasteur/ida/zellige/utils/LocalOtsuClassification.java @@ -16,76 +16,114 @@ import net.imglib2.view.Views; import java.util.List; - -public class LocalOtsu +/** + * + */ +public class LocalOtsuClassification { - - public static < T extends RealType< T > & NativeType< T > > Img< BitType > find( final Img< T > source , double percent) + /** + * + * @param input the input {@link Img} + * @param percent the percentage of global otsu value + * @param <T> the type on the input + * @return a 3D binary {@link Img<BitType>} + */ + public static < T extends RealType< T > & NativeType< T > > Img< BitType > find( final Img< T > input , double percent) { // Prepare output. - final ImgFactory< BitType > factory = Util.getArrayOrCellImgFactory( source, new BitType() ); - Img< BitType > binary = factory.create( source ); - new OtsuThreshold<>( source, binary, percent); + final ImgFactory< BitType > factory = Util.getArrayOrCellImgFactory( input, new BitType() ); + Img< BitType > binary = factory.create( input ); + new OtsuThreshold<>( input, binary, percent); return binary; } - + /** + * + * @param <T> + */ private static final class OtsuThreshold< T extends RealType< T > & NativeType< T > > { private final Img< T > source; - private final Img< T > output; + private final Img< T > grid; private final Img< BitType > binary; private final double percent; + /** + * + * @param source the input {@link Img} + * @param binary the resulting binary image as a {@link Img<BitType> } + * @param percent the percentage of global otsu value + */ private OtsuThreshold( final Img< T > source, final Img< BitType > binary, double percent ) { this.source = source; this.binary = binary; this.percent = percent; final ImgFactory< T > factory = Util.getArrayOrCellImgFactory( source, Util.getTypeFromInterval( source ) ); - this.output = factory.create( source ); + this.grid = factory.create( source ); run(); } - + /** + * + */ public void run() { - computeLocalThreshold( source, output ); - applyLocalThreshold( source, output, binary, percent ); + computeLocalThreshold( source, grid); + applyLocalThreshold( source, grid, binary, percent ); } - public Img< BitType > getOutput() + /** + * + * @return the image containing the grid of local thresholds + */ + public Img< BitType > getGrid() { return binary; } + /** + * + * @param input the input {@link Img} + * @param grid the image containing the grid of local thresholds + * @param <T> the type on the input + */ public static < T extends RealType< T > & NativeType< T > > void - computeLocalThreshold( Img< T > source, Img< T > output ) + computeLocalThreshold( Img< T > input, Img< T > grid ) { - long X = source.dimension( 0 ); - long Y = source.dimension( 1 ); - long Z = source.dimension( 2 ); + long X = input.dimension( 0 ); + long Y = input.dimension( 1 ); + long Z = input.dimension( 2 ); List< Interval > intervals = Grids.collectAllContainedIntervals( new long[]{ X, Y, Z }, new int[]{ 50, 50, 3 } );//TODO Size of a grid cube ? for ( Interval interval : intervals ) { - IntervalView< T > viewSource = Views.offsetInterval( source, interval ); - IntervalView< T > viewOutput = Views.offsetInterval( output, interval ); + IntervalView< T > viewSource = Views.offsetInterval( input, interval ); + IntervalView< T > viewGrid = Views.offsetInterval( grid, interval ); T threshold = Otsu.getThreshold( viewSource ); - viewOutput.forEach( pixel -> pixel.set( threshold ) ); + viewGrid.forEach( pixel -> pixel.set( threshold ) ); } } + + /** + * + * @param input the input {@link Img} + * @param grid the image containing the grid of local thresholds + * @param binary the resulting binary image as a {@link Img<BitType> } + * @param percent the percentage of global otsu value + * @param <T> the input type + */ public static < T extends RealType< T > & NativeType< T > > void - applyLocalThreshold( Img< T > source, Img< T > output, Img<BitType> binary , double percent) + applyLocalThreshold( Img< T > input, Img< T > grid, Img<BitType> binary , double percent) { - double threshold = Threshold.getThreshold( source, percent ).getRealDouble(); - Utils.gaussConvolution( output.copy(), output, new double[]{5, 5, 1} ); - ImageJFunctions.show( output.copy(), "grid" ); - Cursor<T> sourceCursor = source.localizingCursor(); - Cursor<T> outputCursor = output.cursor(); + double threshold = Threshold.getThreshold( input, percent ).getRealDouble(); + Utils.gaussConvolution( grid.copy(), grid, new double[]{5, 5, 1} ); + ImageJFunctions.show( grid.copy(), "grid" ); + Cursor<T> sourceCursor = input.localizingCursor(); + Cursor<T> outputCursor = grid.cursor(); RandomAccess< BitType > randomAccess = binary.randomAccess(); while(sourceCursor.hasNext()) {