diff --git a/src/main/java/fr/pasteur/ida/zellige/steps/ChannelImageSelection.java b/src/main/java/fr/pasteur/ida/zellige/steps/ChannelImageSelection.java new file mode 100644 index 0000000000000000000000000000000000000000..604ea6db8035629dc2bc29fcf2771f8b31f6a75f --- /dev/null +++ b/src/main/java/fr/pasteur/ida/zellige/steps/ChannelImageSelection.java @@ -0,0 +1,59 @@ +package fr.pasteur.ida.zellige.steps; + +import net.imagej.Dataset; +import net.imagej.ImgPlus; +import net.imglib2.RandomAccess; +import net.imglib2.img.Img; +import net.imglib2.type.NativeType; +import net.imglib2.type.numeric.RealType; + +public class ChannelImageSelection< T extends RealType< T > & NativeType< T > > +{ + + private final int targetChannel; + private final Dataset dataset; + + public static< T extends RealType< T > & NativeType< T > > Img< T > run(Dataset input, int targetChannel) + { + ChannelImageSelection<T> channelImageSelection = new ChannelImageSelection<>( input, targetChannel ); + return channelImageSelection.run(); + } + + public ChannelImageSelection( Dataset dataset, int targetChannel ) + { + this.targetChannel = targetChannel; + this.dataset = dataset; + } + + public Img< T > run() + { + Img< T > input = ( ImgPlus< T > ) this.dataset.getImgPlus(); + if (getTargetChannel() == 0) + { + return input; + } + else + { + Img< T > output = input.factory().create( input.dimension( 0 ), input.dimension( 1 ), input.dimension( 3 ) ); + RandomAccess< T > randomAccess = input.randomAccess(); + RandomAccess< T > randomAccess1 = output.randomAccess(); + for ( int x = 0; x < ( int ) input.dimension( 0 ); x++ ) + { + for ( int y = 0; y < ( int ) input.dimension( 1 ); y++ ) + { + for ( int z = 0; z < ( int ) input.dimension( 3 ); z++ ) + { + randomAccess.setPositionAndGet( x, y, getTargetChannel()-1, z ); + randomAccess1.setPosition( new int[]{ x, y, z } ); + randomAccess1.get().set( randomAccess.get() ); + } + } + } + return output; + } + } + public int getTargetChannel() + { + return targetChannel; + } +}