Skip to content

Commit

Permalink
DoG detection takes RandomAccessible's instead of RandomAccessibleInt…
Browse files Browse the repository at this point in the history
…erval's (so OutOfBounds - or not - can be defined outside)
  • Loading branch information
StephanPreibisch committed Feb 29, 2024
1 parent 94aa958 commit 3b071be
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import ij.io.Opener;
import mpicbg.spim.data.generic.sequence.AbstractSequenceDescription;
import mpicbg.spim.data.generic.sequence.BasicViewDescription;
import mpicbg.spim.data.generic.sequence.BasicViewSetup;
import mpicbg.spim.data.sequence.Channel;
import mpicbg.spim.data.sequence.ViewId;
import mpicbg.spim.data.sequence.VoxelDimensions;
Expand Down Expand Up @@ -98,9 +97,9 @@ public LegacyDHMImgLoader(
public RandomAccessibleInterval< FloatType > getFloatImage( final ViewId view, final boolean normalize )
{
if ( normalize )
return ImgLib2Tools.normalizeVirtual( getImage( view ) );
return ImgLib2Tools.normalizeVirtualRAI( getImage( view ) );
else
return ImgLib2Tools.convertVirtual( getImage( view ) );
return ImgLib2Tools.convertVirtualRAI( getImage( view ) );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
import net.imglib2.IterableInterval;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.img.Img;
import net.imglib2.img.ImgFactory;
import net.imglib2.img.array.ArrayImg;
import net.imglib2.type.NativeType;
import net.imglib2.type.numeric.RealType;
Expand Down Expand Up @@ -139,9 +138,9 @@ public LegacyFileMapImgLoaderLOCI(
public RandomAccessibleInterval< FloatType > getFloatImage(ViewId view, boolean normalize)
{
if ( normalize )
return ImgLib2Tools.normalizeVirtual( getImage( view ) );
return ImgLib2Tools.normalizeVirtualRAI( getImage( view ) );
else
return ImgLib2Tools.convertVirtual( getImage( view ) );
return ImgLib2Tools.convertVirtualRAI( getImage( view ) );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ public LegacyLightSheetZ1ImgLoader(
public RandomAccessibleInterval< FloatType > getFloatImage( final ViewId view, final boolean normalize )
{
if ( normalize )
return ImgLib2Tools.normalizeVirtual( getImage( view ) );
return ImgLib2Tools.normalizeVirtualRAI( getImage( view ) );
else
return ImgLib2Tools.convertVirtual( getImage( view ) );
return ImgLib2Tools.convertVirtualRAI( getImage( view ) );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ final public static < T extends RealType< T > & NativeType< T > > void populateI
public RandomAccessibleInterval< FloatType > getFloatImage( final ViewId view, final boolean normalize )
{
if ( normalize )
return ImgLib2Tools.normalizeVirtual( getImage( view ) );
return ImgLib2Tools.normalizeVirtualRAI( getImage( view ) );
else
return ImgLib2Tools.convertVirtual( getImage( view ) );
return ImgLib2Tools.convertVirtualRAI( getImage( view ) );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ public static ImagePlus open( File file )
public RandomAccessibleInterval< FloatType > getFloatImage( final ViewId view, final boolean normalize )
{
if ( normalize )
return ImgLib2Tools.normalizeVirtual( getImage( view ) );
return ImgLib2Tools.normalizeVirtualRAI( getImage( view ) );
else
return ImgLib2Tools.convertVirtual( getImage( view ) );
return ImgLib2Tools.convertVirtualRAI( getImage( view ) );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ public LegacyStackImgLoaderLOCI(
public RandomAccessibleInterval< FloatType > getFloatImage( final ViewId view, final boolean normalize )
{
if ( normalize )
return ImgLib2Tools.normalizeVirtual( getImage( view ) );
return ImgLib2Tools.normalizeVirtualRAI( getImage( view ) );
else
return ImgLib2Tools.convertVirtual( getImage( view ) );
return ImgLib2Tools.convertVirtualRAI( getImage( view ) );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ public SimViewSetupImgLoader(
public RandomAccessibleInterval<FloatType> getFloatImage( final int timepointId, final boolean normalize, ImgLoaderHint... hints )
{
if ( normalize )
return ImgLib2Tools.normalizeVirtual( getImage( timepointId, hints ) );
return ImgLib2Tools.normalizeVirtualRAI( getImage( timepointId, hints ) );
else
return ImgLib2Tools.convertVirtual( getImage( timepointId, hints ) );
return ImgLib2Tools.convertVirtualRAI( getImage( timepointId, hints ) );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,9 @@ public RandomAccessibleInterval< FloatType > getFloatImage(int timepointId, bool
ImgLoaderHint... hints)
{
if ( normalize )
return ImgLib2Tools.normalizeVirtual( getImage( timepointId, hints ) );
return ImgLib2Tools.normalizeVirtualRAI( getImage( timepointId, hints ) );
else
return ImgLib2Tools.convertVirtual( getImage( timepointId, hints ) );
return ImgLib2Tools.convertVirtualRAI( getImage( timepointId, hints ) );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public RandomAccessibleInterval< FloatType > getFloatImage( final int timepointI
// TODO: this is stupid, remove capablitity to get FloatType images!
if ( normalize )
{
return ImgLib2Tools.normalizeVirtual( img );
return ImgLib2Tools.normalizeVirtualRAI( img );
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public RandomAccessibleInterval< FloatType > getFloatImage( final int timepointI
// TODO: this is stupid, remove capablitity to get FloatType images!
if ( normalize )
{
return ImgLib2Tools.normalizeVirtual( img );
return ImgLib2Tools.normalizeVirtualRAI( img );
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ public UnsignedShortType getImageType()
public RandomAccessibleInterval< FloatType > getFloatImage( final int timepointId, final boolean normalize, final ImgLoaderHint... hints )
{
if ( normalize )
return ImgLib2Tools.normalizeVirtual( getImage( timepointId, hints ) );
return ImgLib2Tools.normalizeVirtualRAI( getImage( timepointId, hints ) );
else
return ImgLib2Tools.convertVirtual( getImage( timepointId, hints ) );
return ImgLib2Tools.convertVirtualRAI( getImage( timepointId, hints ) );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import ij.IJ;
import mpicbg.spim.data.sequence.ViewDescription;
import mpicbg.spim.data.sequence.ViewId;
import net.imglib2.FinalInterval;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.realtransform.AffineTransform3D;
import net.imglib2.type.NativeType;
Expand Down Expand Up @@ -60,7 +61,7 @@ public static HashMap< ViewId, List< InterestPoint >> findInterestPoints( final
return interestPoints;
}

/**
/*
* finds all interest points and returns them as InterestPoints (which is a mpicbg Point and implements RealLocalizable),
* by default returns world coordinates
*
Expand Down Expand Up @@ -92,8 +93,9 @@ public static < T extends RealType< T > & NativeType< T > > List< InterestPoint
// compute Difference-of-Gaussian (includes normalization)
//
List< InterestPoint > ips = DoGImgLib2.computeDoG(
Views.zeroMin( input ),
Views.extendMirrorSingle( Views.zeroMin( input ) ),
null,
new FinalInterval( Views.zeroMin( input ) ),
sigma,
threshold,
1, // 0 = no subpixel localization, 1 = quadratic fit
Expand Down Expand Up @@ -150,8 +152,9 @@ public static void addInterestPoints( final HashMap< ViewId, List< InterestPoint

@SuppressWarnings("unchecked")
List< InterestPoint > ips = DoGImgLib2.computeDoG(
input.getA(),
Views.extendMirrorSingle( input.getA() ),
null, // mask
new FinalInterval( input.getA() ),
dog.sigma,
dog.threshold,
dog.localization,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,9 @@
import net.imglib2.img.array.ArrayImg;
import net.imglib2.img.array.ArrayImgFactory;
import net.imglib2.img.array.ArrayImgs;
import net.imglib2.img.basictypeaccess.AccessFlags;
import net.imglib2.img.basictypeaccess.array.FloatArray;
import net.imglib2.img.cell.CellImgFactory;
import net.imglib2.img.display.imagej.ImageJFunctions;
import net.imglib2.type.NativeType;
import net.imglib2.type.Type;
import net.imglib2.type.numeric.RealType;
import net.imglib2.type.numeric.real.FloatType;
Expand All @@ -82,7 +80,6 @@
import net.preibisch.mvrecon.process.interestpointdetection.methods.lazygauss.LazyGauss;
import net.preibisch.mvrecon.process.interestpointdetection.methods.lazygauss.LazyWeightedGauss;
import util.ImgLib2Tools;
import util.Lazy;

public class DoGImgLib2
{
Expand Down Expand Up @@ -112,7 +109,7 @@ public static void main ( String[] args )

// 1388x1040x81 = 116925120
final ArrayList<InterestPoint> points =
computeDoG(inputCropped, null, 2.000, 0.03, 1/*localization*/, false /*findMin*/, true /*findMax*/, 0, 255, Executors.newFixedThreadPool( 8 ) );
computeDoG( Views.extendMirrorDouble( inputCropped ), Views.extendZero( mask ), new FinalInterval( inputCropped ), 2.000, 0.03, 1/*localization*/, false /*findMin*/, true /*findMax*/, 0, 255, Executors.newFixedThreadPool( 8 ) );

System.out.println( System.currentTimeMillis() - time );

Expand Down Expand Up @@ -141,8 +138,9 @@ public static < T extends RealType< T > > int radiusDoG( final double sigma )
}

public static < T extends RealType< T > > ArrayList< InterestPoint > computeDoG(
final RandomAccessibleInterval< T > input,
final RandomAccessibleInterval< T > mask,
final RandomAccessible< T > input,
final RandomAccessible< T > mask,
final Interval interval,
final double sigma,
final double threshold,
final int localization,
Expand All @@ -152,12 +150,13 @@ public static < T extends RealType< T > > ArrayList< InterestPoint > computeDoG(
final double maxIntensity,
final ExecutorService service )
{
return computeDoG(input, mask, sigma, threshold, localization, findMin, findMax, minIntensity, maxIntensity, blockSize, service, null, null, false, 0.0 );
return computeDoG(input, mask, interval, sigma, threshold, localization, findMin, findMax, minIntensity, maxIntensity, blockSize, service, null, null, false, 0.0 );
}

public static < T extends RealType< T > > ArrayList< InterestPoint > computeDoG(
final RandomAccessibleInterval< T > input,
final RandomAccessibleInterval< T > mask,
final RandomAccessible< T > input,
final RandomAccessible< T > mask,
final Interval interval,
final double sigma,
final double threshold,
final int localization,
Expand Down Expand Up @@ -189,9 +188,9 @@ public static < T extends RealType< T > > ArrayList< InterestPoint > computeDoG(
final float[] minmax;

if ( mask == null )
minmax = FusionTools.minMax( input, service );
minmax = FusionTools.minMax( Views.interval( input, interval ), service );
else
minmax = minMax( input, mask, service );
minmax = minMax( Views.interval( input, interval ), Views.interval( mask, interval ), service );

min = minmax[ 0 ];
max = minmax[ 1 ];
Expand All @@ -206,7 +205,7 @@ public static < T extends RealType< T > > ArrayList< InterestPoint > computeDoG(
IOFunctions.println( "(" + new Date(System.currentTimeMillis()) + "): min intensity = " + min + ", max intensity = " + max );

// normalize image
final RandomAccessibleInterval< FloatType > inputFloat = ImgLib2Tools.normalizeVirtual( input, min, max );
final RandomAccessible< FloatType > inputFloat = ImgLib2Tools.normalizeVirtual( input, min, max );

final float k = LaPlaceFunctions.computeK( 4 );
final float K_MIN1_INV = LaPlaceFunctions.computeKWeight(k);
Expand All @@ -232,33 +231,33 @@ public static < T extends RealType< T > > ArrayList< InterestPoint > computeDoG(
"threshold=" + minPeakValue + ", sigma1=" + Util.printCoordinates( sigma1 ) + ", sigma2=" + Util.printCoordinates( sigma2 ) + ")" );

final long[] minInterval = new long[ inputFloat.numDimensions() ];
inputFloat.min( minInterval );
interval.min( minInterval );

final RandomAccessibleInterval< FloatType > gauss1, gauss2;
final RandomAccessibleInterval< FloatType > maskFloat;
final RandomAccessible< FloatType > maskFloat;

if ( mask == null )
{
maskFloat = null;

if ( cuda == null )
{
gauss1 = LazyGauss.init( Views.extendMirrorDouble( inputFloat ), new FinalInterval( inputFloat ), new FloatType(), sigma1, blockSize );
gauss2 = LazyGauss.init( Views.extendMirrorDouble( inputFloat ), new FinalInterval( inputFloat ), new FloatType(), sigma2, blockSize );
gauss1 = LazyGauss.init( inputFloat, interval, new FloatType(), sigma1, blockSize );
gauss2 = LazyGauss.init( inputFloat, interval , new FloatType(), sigma2, blockSize );
}
else
{
// TODO: untested
gauss1 = computeGaussCUDA( inputFloat, sigma1, cuda, cudaDevice, accurateCUDA, percentGPUMem );
gauss2 = computeGaussCUDA( inputFloat, sigma2, cuda, cudaDevice, accurateCUDA, percentGPUMem );
gauss1 = computeGaussCUDA( Views.interval( inputFloat, interval ), sigma1, cuda, cudaDevice, accurateCUDA, percentGPUMem );
gauss2 = computeGaussCUDA( Views.interval( inputFloat, interval ), sigma2, cuda, cudaDevice, accurateCUDA, percentGPUMem );
}
}
else
{
maskFloat = Converters.convertRAI( mask, (i,o) -> o.set( i.getRealFloat() ), new FloatType());//ImgLib2Tools.convertVirtual( mask );
maskFloat = Converters.convert( mask, (i,o) -> o.set( i.getRealFloat() ), new FloatType());//ImgLib2Tools.convertVirtual( mask );

gauss1 = LazyWeightedGauss.init( Views.extendMirrorSingle( inputFloat ), Views.extendZero( maskFloat ), new FinalInterval( inputFloat ), new FloatType(), sigma1, blockSize );
gauss2 = LazyWeightedGauss.init( Views.extendMirrorSingle( inputFloat ), Views.extendZero( maskFloat ), new FinalInterval( inputFloat ), new FloatType(), sigma2, blockSize );
gauss1 = LazyWeightedGauss.init( inputFloat, maskFloat, interval, new FloatType(), sigma1, blockSize ); // mask zero oobs
gauss2 = LazyWeightedGauss.init( inputFloat, maskFloat, interval, new FloatType(), sigma2, blockSize );
}

final RandomAccessibleInterval< FloatType > dog = Converters.convert(gauss2, gauss1, new BiConverter<FloatType, FloatType, FloatType>()
Expand All @@ -277,7 +276,7 @@ public void convert( final FloatType inputA, final FloatType inputB, final Float
if ( !silent )
IOFunctions.println("(" + new Date(System.currentTimeMillis()) + "): Detecting peaks." );

final ArrayList< SimplePeak > peaks = findPeaks( dogCached, maskFloat, minInitialPeakValue, service );
final ArrayList< SimplePeak > peaks = findPeaks( dogCached, Views.interval( maskFloat, interval ), minInitialPeakValue, service );

if ( !silent )
IOFunctions.println("(" + new Date(System.currentTimeMillis()) + "): Found " + peaks.size() + " initial peaks (before refinement)." );
Expand Down
28 changes: 20 additions & 8 deletions src/main/java/util/ImgLib2Tools.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
package util;

import net.imglib2.RandomAccessible;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.converter.Converters;
import net.imglib2.type.numeric.RealType;
Expand All @@ -30,17 +31,28 @@

public class ImgLib2Tools
{
public static final < T extends RealType< T > > RandomAccessibleInterval<FloatType> convertVirtual( final RandomAccessibleInterval< T > img )
public static final < T extends RealType< T > > RandomAccessibleInterval<FloatType> convertVirtualRAI( final RandomAccessibleInterval< T > img )
{
return Converters.convertRAI( img, (i,o) -> o.set( i.getRealFloat() ), new FloatType());
/*
return new ConvertedRandomAccessibleInterval<T, FloatType>(
}

public static final < T extends RealType< T > > RandomAccessible<FloatType> convertVirtual( final RandomAccessible< T > img )
{
return Converters.convert( img, (i,o) -> o.set( i.getRealFloat() ), new FloatType());
}

public static final < T extends RealType< T > > RandomAccessible<FloatType> normalizeVirtual( final RandomAccessible< T > img, final double min, final double max )
{
final float minf = (float)min;
final float maxf = (float)max;

return Converters.convert(
img,
new RealFloatConverter<T>(),
new FloatType() );*/
( input, output ) -> output.set( ( input.getRealFloat() - minf ) / ( maxf - minf ) ),
new FloatType() );
}

public static final < T extends RealType< T > > RandomAccessibleInterval<FloatType> normalizeVirtual( final RandomAccessibleInterval< T > img, final double min, final double max )
public static final < T extends RealType< T > > RandomAccessibleInterval<FloatType> normalizeVirtualRAI( final RandomAccessibleInterval< T > img, final double min, final double max )
{
final float minf = (float)min;
final float maxf = (float)max;
Expand All @@ -51,7 +63,7 @@ public static final < T extends RealType< T > > RandomAccessibleInterval<FloatTy
new FloatType() );
}

public static final < T extends RealType< T > > RandomAccessibleInterval<FloatType> normalizeVirtual( final RandomAccessibleInterval< T > img )
public static final < T extends RealType< T > > RandomAccessibleInterval<FloatType> normalizeVirtualRAI( final RandomAccessibleInterval< T > img )
{
double min = Double.MAX_VALUE;
double max = -Double.MAX_VALUE;
Expand All @@ -67,6 +79,6 @@ public static final < T extends RealType< T > > RandomAccessibleInterval<FloatTy
max = v;
}

return normalizeVirtual( img, min, max );
return normalizeVirtualRAI( img, min, max );
}
}

0 comments on commit 3b071be

Please sign in to comment.