diff --git a/src/main/java/fr/pasteur/ida/zellige/utils/Utils.java b/src/main/java/fr/pasteur/ida/zellige/utils/Utils.java index ce67c8a3bfcd7cbc891e3b97532642bebd93b4b6..9463814acbcc1aa72e9005d947df0fa7d8d7cf19 100644 --- a/src/main/java/fr/pasteur/ida/zellige/utils/Utils.java +++ b/src/main/java/fr/pasteur/ida/zellige/utils/Utils.java @@ -2,7 +2,10 @@ package fr.pasteur.ida.zellige.utils; import ij.ImagePlus; import ij.io.FileSaver; -import net.imglib2.*; +import net.imglib2.Cursor; +import net.imglib2.RandomAccess; +import net.imglib2.RandomAccessible; +import net.imglib2.RandomAccessibleInterval; import net.imglib2.algorithm.gauss3.Gauss3; import net.imglib2.algorithm.gradient.PartialDerivative; import net.imglib2.algorithm.stats.Normalize; @@ -19,7 +22,6 @@ import net.imglib2.type.numeric.real.FloatType; import net.imglib2.view.Views; - //TODO Class description /** @@ -38,11 +40,11 @@ public class Utils * @param output - the filtered image as a {@link RandomAccessibleInterval} * @param sigma - a array containing the standard deviations for each dimension. */ - public static < T extends RealType < T > & NativeType < T > > void - gaussConvolution( RandomAccessibleInterval < T > input, RandomAccessibleInterval < T > output, + public static < T extends RealType< T > & NativeType< T > > void + gaussConvolution( RandomAccessibleInterval< T > input, RandomAccessibleInterval< T > output, double[] sigma ) { - RandomAccessible < T > infiniteInput = Views.extendValue( input, input.randomAccess().get() ); + RandomAccessible< T > infiniteInput = Views.extendValue( input, input.randomAccess().get() ); Gauss3.gauss( sigma, infiniteInput, output ); } @@ -54,14 +56,14 @@ public class Utils * @param input - the source image as a {@link RandomAccessibleInterval} * @param output - the output image as a {@link RandomAccessibleInterval} */ - public static < T extends RealType < T > & NativeType < T > > void - derivative( final RandomAccessibleInterval < T > input, - RandomAccessibleInterval < T > output ) + public static < T extends RealType< T > & NativeType< T > > void + derivative( final RandomAccessibleInterval< T > input, + RandomAccessibleInterval< T > output ) { //TODO What's the better way ? // RandomAccessible < T > infiniteInput = Views.extendBorder( input ); // RandomAccessible < T > infiniteInput = Views.extendMirrorDouble( input ); - RandomAccessible < T > infiniteInput = Views.extendValue( input, input.randomAccess().get().createVariable() ); + RandomAccessible< T > infiniteInput = Views.extendValue( input, input.randomAccess().get().createVariable() ); PartialDerivative.gradientCentralDifference2( infiniteInput, output, 1 ); } @@ -72,12 +74,14 @@ public class Utils * @param <T> - the type of the input Img. * @return a new Img with normalize pixel intensity values. */ - public static < T extends RealType < T > & NativeType < T > > Img < T > normalizeImage( - Img < T > image) + public static < T extends RealType< T > & NativeType< T > > Img< T > normalizeImage( + Img< T > image ) { - Img < T > normalizedImage = image.copy(); - T min = image.firstElement().copy().createVariable(); min.setReal( 0 ); - T max = image.firstElement().copy().createVariable();max.setReal( 255); + Img< T > normalizedImage = image.copy(); + T min = image.firstElement().copy().createVariable(); + min.setReal( 0 ); + T max = image.firstElement().copy().createVariable(); + max.setReal( 255 ); Normalize.normalize( normalizedImage, min, max ); return normalizedImage; } @@ -85,102 +89,104 @@ public class Utils /** * Saves the specified image at the specified path location. * - * @param image - the {@link Img} to save + * @param image - the {@link Img} to save * @param imageTitle - the name of the image - * @param imagePath - the location to - * @param <T> - the image type + * @param imagePath - the location to + * @param <T> - the image type */ - public static < T extends RealType < T > & NativeType < T > > void saveImage( Img <T> image, String imageTitle, String imagePath) + public static < T extends RealType< T > & NativeType< T > > void saveImage( Img< T > image, String imageTitle, String imagePath ) { - final ImagePlus imp = ImageJFunctions.wrap( image,imageTitle ).duplicate(); + final ImagePlus imp = ImageJFunctions.wrap( image, imageTitle ).duplicate(); final FileSaver s = new FileSaver( imp ); if ( image.numDimensions() == 3 ) { - imp.setDimensions( 1, (int)image.dimension( 2 ), 1 ); - s.saveAsTiffStack( imagePath + imageTitle+".tif" ); + imp.setDimensions( 1, ( int ) image.dimension( 2 ), 1 ); + s.saveAsTiffStack( imagePath + imageTitle + ".tif" ); } else { - s.saveAsTiff( imagePath + imageTitle+".tif"); + s.saveAsTiff( imagePath + imageTitle + ".tif" ); } } - public static < T extends RealType < T > & NativeType < T > > T getTypeValue(Number number, T type) + public static < T extends RealType< T > & NativeType< T > > T getTypeValue( Number number, T type ) { T result = type.createVariable(); - result.setReal( (double ) number); + result.setReal( ( double ) number ); return result; } /** * Converts an Img of type T extends RealType & NativeType into an IntType Img. + * * @param image - the specified image - * @param <T> - the image type + * @param <T> - the image type * @return the converted IntType image */ - public static < T extends RealType < T > & NativeType < T > > Img<IntType> convertIntoIntType(Img < T > image) + public static < T extends RealType< T > & NativeType< T > > Img< IntType > convertIntoIntType( Img< T > image ) { - final ImgFactory< IntType > imgFactory = new ArrayImgFactory<>( new IntType()); - final Img < IntType > copy = imgFactory.create(image); - Cursor <T> imageCursor = image.cursor(); - Cursor < IntType > copyCursor = copy.cursor(); + final ImgFactory< IntType > imgFactory = new ArrayImgFactory<>( new IntType() ); + final Img< IntType > copy = imgFactory.create( image ); + Cursor< T > imageCursor = image.cursor(); + Cursor< IntType > copyCursor = copy.cursor(); T type = imageCursor.next(); IntType intType = copyCursor.next(); - intType.set((int) type.getRealDouble()); - while (imageCursor.hasNext()) + intType.set( ( int ) type.getRealDouble() ); + while ( imageCursor.hasNext() ) { type = imageCursor.next(); intType = copyCursor.next(); - intType.set((int) type.getRealDouble()); + intType.set( ( int ) type.getRealDouble() ); } return copy; } - public static < T extends RealType < T > & NativeType < T > > Img< ShortType > convertIntoShortType(Img < T > image) + public static < T extends RealType< T > & NativeType< T > > Img< ShortType > convertIntoShortType( Img< T > image ) { - final ImgFactory< ShortType > imgFactory = new ArrayImgFactory<>( new ShortType()); - final Img < ShortType > copy = imgFactory.create(image); - Cursor <T> imageCursor = image.cursor(); - Cursor < ShortType > copyCursor = copy.cursor(); + final ImgFactory< ShortType > imgFactory = new ArrayImgFactory<>( new ShortType() ); + final Img< ShortType > copy = imgFactory.create( image ); + Cursor< T > imageCursor = image.cursor(); + Cursor< ShortType > copyCursor = copy.cursor(); T type = imageCursor.next(); - ShortType shortType = copyCursor.next(); + ShortType shortType = copyCursor.next(); shortType.set( ( short ) type.getRealDouble() ); - while (imageCursor.hasNext()) + while ( imageCursor.hasNext() ) { type = imageCursor.next(); shortType = copyCursor.next(); - shortType.set((short ) type.getRealDouble()); + shortType.set( ( short ) type.getRealDouble() ); } return copy; } + /** * Converts an Img of type T extends RealType & NativeType into an IntType Img. + * * @param image - the specified image - * @param <T> - the image type + * @param <T> - the image type * @return the converted IntType image */ - public static < T extends RealType < T > & NativeType < T > > Img<FloatType> convertIntoFloatType(Img < T > image) + public static < T extends RealType< T > & NativeType< T > > Img< FloatType > convertIntoFloatType( Img< T > image ) { - final ImgFactory< FloatType > imgFactory = new ArrayImgFactory<>( new FloatType()); - final Img < FloatType > copy = imgFactory.create(image); - Cursor <T> imageCursor = image.cursor(); - Cursor < FloatType > copyCursor = copy.cursor(); + final ImgFactory< FloatType > imgFactory = new ArrayImgFactory<>( new FloatType() ); + final Img< FloatType > copy = imgFactory.create( image ); + Cursor< T > imageCursor = image.cursor(); + Cursor< FloatType > copyCursor = copy.cursor(); T type = imageCursor.next(); FloatType floatType = copyCursor.next(); - floatType.set((int) type.getRealDouble()); - while (imageCursor.hasNext()) + floatType.set( ( int ) type.getRealDouble() ); + while ( imageCursor.hasNext() ) { type = imageCursor.next(); floatType = copyCursor.next(); - floatType.set((int) type.getRealDouble()); + floatType.set( ( int ) type.getRealDouble() ); } return copy; } - public static int[][] ImgToZMap ( final Img< IntType > image, int[][] originalZMap ) { @@ -192,9 +198,9 @@ public class Utils imgAccess.setPosition( x, 0 ); for ( int y = 0; y <= image.dimension( 1 ) - 1; y++ ) { - if ( originalZMap[ y ][ x ] != -1 ) + if ( originalZMap[ y ][ x ] != - 1 ) { - zMap[ y ][ x ] = Utils.setPositionAndGet( imgAccess, x, y ).getInteger(); + zMap[ y ][ x ] = Utils.setPositionAndGet( imgAccess, x, y ).getInteger(); } } @@ -220,9 +226,9 @@ public class Utils for ( int y = 0; y <= ZMapImg.dimension( 1 ) - 1; y++ ) { zMapAccess.setPosition( y, 1 ); - if ( zMap[ y ][ x ] != -1) + if ( zMap[ y ][ x ] != - 1 ) { - int z = zMap[ y ][ x ]; + int z = zMap[ y ][ x ]; IntType Z = new IntType( z ); zMapAccess.get().set( Z ); } @@ -231,6 +237,7 @@ public class Utils // ImageJFunctions.show( ZMapImg, "ZMap Image" ); return ZMapImg; } + public static int[][] smoothZMap( int[][] interpolatedZMap, int sigma ) { Img< IntType > zMapImg = Utils.zMapToZMapImg( interpolatedZMap ); @@ -244,6 +251,7 @@ public class Utils Utils.gaussConvolution( zMap, smoothZmaP, new double[]{ sigma, sigma } ); return smoothZmaP; } + /** * Returns a Double array corresponding to a specified image * @@ -271,40 +279,41 @@ public class Utils return zMap; } - public static < T extends RealType < T > & NativeType < T > > void setPosition(RandomAccess<T > randomAccess, int u, int v) - { - randomAccess.setPosition( u, 0 ); - randomAccess.setPosition( v, 1 ); - } + public static < T extends RealType< T > & NativeType< T > > void setPosition( RandomAccess< T > randomAccess, int u, int v ) + { + randomAccess.setPosition( u, 0 ); + randomAccess.setPosition( v, 1 ); + } - public static < T extends RealType < T > & NativeType < T > >void setPosition(RandomAccess<T > randomAccess,int u, int v, int z) + public static < T extends RealType< T > & NativeType< T > > void setPosition( RandomAccess< T > randomAccess, int u, int v, int z ) { - setPosition( randomAccess, u, v) ; + setPosition( randomAccess, u, v ); randomAccess.setPosition( z, 2 ); } - public static < T extends RealType < T > & NativeType < T > > T setPositionAndGet(RandomAccess<T > randomAccess,int u, int v) + public static < T extends RealType< T > & NativeType< T > > T setPositionAndGet( RandomAccess< T > randomAccess, int u, int v ) { - setPosition( randomAccess, u, v); + setPosition( randomAccess, u, v ); return randomAccess.get(); } - public static < T extends RealType < T > & NativeType < T > > void setPositionAndValue(RandomAccess<T > randomAccess,int u, int v, int z, T value) + public static < T extends RealType< T > & NativeType< T > > void setPositionAndValue( RandomAccess< T > randomAccess, int u, int v, int z, T value ) { setPosition( randomAccess, u, v, z ); randomAccess.get().set( value ); } - public static < T extends RealType < T > & NativeType < T > > void setPositionAndValue(RandomAccess<T > randomAccess,int u, int v, T value) + public static < T extends RealType< T > & NativeType< T > > void setPositionAndValue( RandomAccess< T > randomAccess, int u, int v, T value ) { setPosition( randomAccess, u, v ); randomAccess.get().set( value ); } - public static < T extends RealType < T > & NativeType < T > > T setPositionAndGet(RandomAccess<T > randomAccess,int u, int v, int z) + + public static < T extends RealType< T > & NativeType< T > > T setPositionAndGet( RandomAccess< T > randomAccess, int u, int v, int z ) { - setPosition( randomAccess, u, v, z ); - return randomAccess.get(); + setPosition( randomAccess, u, v, z ); + return randomAccess.get(); } }