Skip to content

Commit

Permalink
Merge pull request #269 from trackmate-sc/label-image-32-bit
Browse files Browse the repository at this point in the history
Export label images on 32-bit float type.
  • Loading branch information
ctrueden authored Mar 26, 2024
2 parents fd365a6 + 84d12e7 commit fd72d4f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/main/java/fiji/plugin/trackmate/action/CTCExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ public static void exportSegmentationData( final String exportRootFolder, final
for ( int frame = 0; frame < dims[ 3 ]; frame++ )
{
final ImgPlus< UnsignedShortType > imgCT = TMUtils.hyperSlice( labelImg, 0, frame );
final SpotRoiWriter spotWriter = new SpotRoiWriter( imgCT );
final SpotRoiWriter< UnsignedShortType > spotWriter = new SpotRoiWriter<>( imgCT );

for ( final Spot spot : model.getSpots().iterable( frame, true ) )
{
Expand Down Expand Up @@ -446,7 +446,7 @@ public static String exportTrackingData( final String exportRootFolder, final in
{
final long frame = spot.getFeature( Spot.FRAME ).longValue();
final ImgPlus< UnsignedShortType > imgCT = TMUtils.hyperSlice( labelImg, 0, frame );
final SpotRoiWriter spotRoiWriter = new SpotRoiWriter( imgCT );
final SpotRoiWriter< UnsignedShortType > spotRoiWriter = new SpotRoiWriter<>( imgCT );
spotRoiWriter.write( spot, currentID );
}

Expand Down
34 changes: 18 additions & 16 deletions src/main/java/fiji/plugin/trackmate/action/LabelImgExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@
import net.imglib2.RandomAccess;
import net.imglib2.img.Img;
import net.imglib2.img.display.imagej.ImageJFunctions;
import net.imglib2.type.numeric.RealType;
import net.imglib2.type.numeric.integer.UnsignedShortType;
import net.imglib2.type.numeric.real.FloatType;
import net.imglib2.util.Util;
import net.imglib2.view.Views;

Expand Down Expand Up @@ -395,7 +397,7 @@ public static final ImagePlus createLabelImagePlus(
*
* @return a new {@link Img}.
*/
public static final Img< UnsignedShortType > createLabelImg(
public static final Img< FloatType > createLabelImg(
final Model model,
final long[] dimensions,
final double[] calibration,
Expand Down Expand Up @@ -435,7 +437,7 @@ public static final Img< UnsignedShortType > createLabelImg(
*
* @return a new {@link Img}.
*/
public static final Img< UnsignedShortType > createLabelImg(
public static final Img< FloatType > createLabelImg(
final Model model,
final long[] dimensions,
final double[] calibration,
Expand All @@ -448,13 +450,13 @@ public static final Img< UnsignedShortType > createLabelImg(
* Create target image.
*/
final Dimensions targetSize = FinalDimensions.wrap( dimensions );
final Img< UnsignedShortType > lblImg = Util.getArrayOrCellImgFactory( targetSize, new UnsignedShortType() ).create( targetSize );
final Img< FloatType > lblImg = Util.getArrayOrCellImgFactory( targetSize, new FloatType() ).create( targetSize );
final AxisType[] axes = new AxisType[] {
Axes.X,
Axes.Y,
Axes.Z,
Axes.TIME };
final ImgPlus< UnsignedShortType > imgPlus = new ImgPlus<>( lblImg, "LblImg", axes, calibration );
final ImgPlus< FloatType > imgPlus = new ImgPlus<>( lblImg, "LblImg", axes, calibration );

/*
* Determine the starting id for spots not in tracks.
Expand All @@ -475,10 +477,10 @@ public static final Img< UnsignedShortType > createLabelImg(
logger.log( "Writing label image.\n" );
for ( int frame = 0; frame < dimensions[ 3 ]; frame++ )
{
final ImgPlus< UnsignedShortType > imgCT = TMUtils.hyperSlice( imgPlus, 0, frame );
final ImgPlus< FloatType > imgCT = TMUtils.hyperSlice( imgPlus, 0, frame );
final SpotWriter spotWriter = exportSpotsAsDots
? new SpotAsDotWriter( imgCT )
: new SpotRoiWriter( imgCT );
? new SpotAsDotWriter<>( imgCT )
: new SpotRoiWriter<>( imgCT );

for ( final Spot spot : model.getSpots().iterable( frame, true ) )
{
Expand Down Expand Up @@ -554,34 +556,34 @@ public static interface SpotWriter
public void write( Spot spot, int id );
}

public static final class SpotRoiWriter implements SpotWriter
public static final class SpotRoiWriter< T extends RealType< T > > implements SpotWriter
{

private final ImgPlus< UnsignedShortType > img;
private final ImgPlus< T > img;

public SpotRoiWriter( final ImgPlus< UnsignedShortType > img )
public SpotRoiWriter( final ImgPlus< T > img )
{
this.img = img;
}

@Override
public void write( final Spot spot, final int id )
{
for ( final UnsignedShortType pixel : SpotUtil.iterable( spot, img ) )
pixel.set( id );
for ( final T pixel : SpotUtil.iterable( spot, img ) )
pixel.setReal( id );
}
}

public static final class SpotAsDotWriter implements SpotWriter
public static final class SpotAsDotWriter< T extends RealType< T > > implements SpotWriter
{

private final double[] calibration;

private final long[] center;

private final RandomAccess< UnsignedShortType > ra;
private final RandomAccess< T > ra;

public SpotAsDotWriter( final ImgPlus< UnsignedShortType > img )
public SpotAsDotWriter( final ImgPlus< T > img )
{
this.calibration = TMUtils.getSpatialCalibration( img );
this.center = new long[ img.numDimensions() ];
Expand All @@ -595,7 +597,7 @@ public void write( final Spot spot, final int id )
center[ d ] = Math.round( spot.getFeature( Spot.POSITION_FEATURES[ d ] ).doubleValue() / calibration[ d ] );

ra.setPosition( center );
ra.get().set( id );
ra.get().setReal( id );
}
}
}

0 comments on commit fd72d4f

Please sign in to comment.