Skip to content

Commit

Permalink
SIFT Registration working
Browse files Browse the repository at this point in the history
  • Loading branch information
tischi committed Dec 27, 2023
1 parent 4267cfa commit 3dbeda0
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
import org.scijava.plugin.Plugin;
import sc.fiji.bdvpg.scijava.command.BdvPlaygroundActionCommand;

@Plugin(type = BdvPlaygroundActionCommand.class, menuPath = CommandConstants.CONTEXT_MENU_ITEMS_ROOT + "Transform>Registration - SIFT XY")
public class SIFTXYAlignCommand implements BdvPlaygroundActionCommand
@Plugin(type = BdvPlaygroundActionCommand.class, menuPath = CommandConstants.CONTEXT_MENU_ITEMS_ROOT + "Transform>Registration - SIFT")
public class SIFT2DAlignCommand implements BdvPlaygroundActionCommand
{
static { net.imagej.patcher.LegacyInjector.preinit(); }

Expand All @@ -49,9 +49,9 @@ public class SIFTXYAlignCommand implements BdvPlaygroundActionCommand
@Override
public void run()
{
IJ.log("# SIFT XY Aligner" +
"\nCurrently, only aligns along the XY axis." +
"\nPress Shift+Z to align current view along Z axis to avoid surprising results.");
IJ.log("# SIFT registration" +
"\nThe registration is computed in the currently visible 2D plane" +
"\nbut then applied to the full image in 3D.");
// start the alignment, which has its own GUI
SIFT2DAligner aligner = new SIFT2DAligner( bdvHandle );
if( ! aligner.run() ) return;
Expand All @@ -60,10 +60,14 @@ public void run()
SourceAndConverter< ? > movingSac = aligner.getMovingSac();
if ( movingSac.getSpimSource() instanceof TransformedSource )
{
AffineTransform3D affineTransform3D = aligner.getAffineTransform3D();
( ( TransformedSource< ? > ) movingSac.getSpimSource() ).setFixedTransform( affineTransform3D );
AffineTransform3D siftTransform3D = aligner.getSiftTransform3D();
TransformedSource< ? > transformedSource = ( TransformedSource< ? > ) movingSac.getSpimSource();
AffineTransform3D fixedTransform = new AffineTransform3D();
transformedSource.getFixedTransform( fixedTransform );
fixedTransform.preConcatenate( siftTransform3D );
transformedSource.setFixedTransform( fixedTransform );
bdvHandle.getViewerPanel().requestRepaint();
IJ.log( "Transformed " + movingSac.getSpimSource().getName() + " with " + affineTransform3D );
IJ.log( "Transformed " + movingSac.getSpimSource().getName() + " with " + siftTransform3D );
}
else
{
Expand Down
25 changes: 18 additions & 7 deletions src/main/java/org/embl/mobie/command/context/SIFT2DAligner.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public class SIFT2DAligner
private AffineTransform3D affineTransform3D;
private SourceAndConverter< ? > fixedSac;
private SourceAndConverter< ? > movingSac;
private ScreenShotMaker screenShotMaker;

static private class Param
{
Expand Down Expand Up @@ -209,7 +210,7 @@ private void extractImages( List< SourceAndConverter< ? > > sourceAndConverters,
.filter( sac -> sac.getSpimSource().getName().equals( movingImageName ) )
.findFirst().get();

ScreenShotMaker screenShotMaker = new ScreenShotMaker( bdvHandle, p.pixelSize, fixedSac.getSpimSource().getVoxelDimensions().unit() );
screenShotMaker = new ScreenShotMaker( bdvHandle, p.pixelSize, fixedSac.getSpimSource().getVoxelDimensions().unit() );
screenShotMaker.run( Arrays.asList( fixedSac, movingSac ) );
CompositeImage compositeImage = screenShotMaker.getCompositeImagePlus();

Expand Down Expand Up @@ -307,14 +308,24 @@ private boolean exec( final ImagePlus imp1, final ImagePlus imp2) {

if ( model instanceof AbstractAffineModel2D )
{
affineTransform3D = new AffineTransform3D();

// global to target canvas
AffineTransform3D canvasToGlobalTransform = screenShotMaker.getCanvasToGlobalTransform();
affineTransform3D.preConcatenate( canvasToGlobalTransform.inverse() );

// sift within canvas
final double[] a = new double[6];
( ( AbstractAffineModel2D< ? > ) model ).toArray( a );
affineTransform3D = new AffineTransform3D();
affineTransform3D.set(
a[0], a[2], 0, a[4] * p.pixelSize,
a[1], a[3], 0, a[5] * p.pixelSize,
AffineTransform3D canvasSiftTransform = new AffineTransform3D();
canvasSiftTransform.set(
a[0], a[2], 0, a[4],
a[1], a[3], 0, a[5],
0, 0, 1, 0);
affineTransform3D = affineTransform3D.inverse();
affineTransform3D.preConcatenate( canvasSiftTransform.inverse() );

// canvas to global
affineTransform3D.preConcatenate( canvasToGlobalTransform );
}
else
IJ.showMessage( "Cannot apply " + model );
Expand Down Expand Up @@ -355,7 +366,7 @@ private boolean exec( final ImagePlus imp1, final ImagePlus imp2) {
return modelFound;
}

public AffineTransform3D getAffineTransform3D()
public AffineTransform3D getSiftTransform3D()
{
return affineTransform3D;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,13 @@ public class ScreenShotMakerCommand extends DynamicCommand implements BdvPlaygro
@Parameter(label="Pixel unit", persist = false, choices = {"micrometer"} )
public String pixelUnit;

@Parameter(label="Show RGB Image")
public boolean showRGB = true;

@Parameter(label="Show Multi-Channel Image")
public boolean showMultiChannel = true;


@Override
public void run()
{
ScreenShotMaker screenShotMaker = new ScreenShotMaker( bdvh, targetSamplingInXY, pixelUnit );
screenShotMaker.run();

if( showRGB )
screenShotMaker.getRGBImagePlus().show();

if( showMultiChannel )
screenShotMaker.getCompositeImagePlus().show();
screenShotMaker.getRGBImagePlus().show();
screenShotMaker.getCompositeImagePlus().show();

if ( MoBIE.getInstance().getSettings().values.isOpenedFromCLI() )
MoBIE.imageJ.ui().showUI();
Expand Down
38 changes: 21 additions & 17 deletions src/main/java/org/embl/mobie/lib/bdv/ScreenShotMaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public class ScreenShotMaker
private ImagePlus rgbImagePlus = null;
private CompositeImage compositeImagePlus = null;
private long[] screenshotDimensions = new long[2];
private AffineTransform3D targetCanvasToGlobalTransform;
private AffineTransform3D canvasToGlobalTransform;

public ScreenShotMaker( BdvHandle bdvHandle, Double pixelSize, String pixelUnit ) {
this.bdvHandle = bdvHandle;
Expand Down Expand Up @@ -118,19 +118,26 @@ public void run( List< SourceAndConverter< ? > > sacs )

final AffineTransform3D viewerTransform = new AffineTransform3D();
bdvHandle.getViewerPanel().state().getViewerTransform( viewerTransform );
final int currentTimepoint = bdvHandle.getViewerPanel().state().getCurrentTimepoint();

screenshotDimensions = getCaptureImageSizeInPixels( bdvHandle, targetVoxelSpacing );
canvasToGlobalTransform = new AffineTransform3D();
// target canvas to viewer canvas...
double targetToViewer = targetVoxelSpacing / getViewerVoxelSpacing( bdvHandle );
canvasToGlobalTransform.scale( targetToViewer, targetToViewer, 1.0 );
// ...viewer canvas to global
AffineTransform3D viewerToGlobal = viewerTransform.inverse();
canvasToGlobalTransform.preConcatenate( viewerToGlobal );
IJ.log( "Canvas to global transform: " + canvasToGlobalTransform );

IJ.log( "Fetching data from " + sacs.size() + " images..." );

final ArrayList< RandomAccessibleInterval< FloatType > > floatCaptures = new ArrayList<>();
final ArrayList< RandomAccessibleInterval< ARGBType > > argbSources = new ArrayList<>();
final ArrayList< ARGBType > colors = new ArrayList<>();

final ArrayList< double[] > displayRanges = new ArrayList<>();

final int t = bdvHandle.getViewerPanel().state().getCurrentTimepoint();

IJ.log( "Fetching data from " + sacs.size() + " images..." );

screenshotDimensions = getCaptureImageSizeInPixels( bdvHandle, targetVoxelSpacing );
final long numPixels = screenshotDimensions[ 0 ] * screenshotDimensions[ 1 ];
long pixelsPerThread = numPixels / ThreadHelper.getNumIoThreads();
int dimensionsPerThread = (int) Math.sqrt( pixelsPerThread );
Expand All @@ -139,14 +146,6 @@ public void run( List< SourceAndConverter< ? > > sacs )
screenshotDimensions,
blockSize );

targetCanvasToGlobalTransform = new AffineTransform3D();
// target canvas to viewer canvas
double targetToViewer = getViewerVoxelSpacing( bdvHandle ) / targetVoxelSpacing;
targetCanvasToGlobalTransform.scale( 1 / targetToViewer, 1 / targetToViewer, 1.0 );
// viewer canvas to global
AffineTransform3D viewerToGlobal = viewerTransform.inverse();
targetCanvasToGlobalTransform.preConcatenate( viewerToGlobal );

IJ.log( "Number of threads: " + ThreadHelper.getNumIoThreads() );
IJ.log( "Block per thread: " + Arrays.toString( blockSize ) );
final long currentTimeMillis = System.currentTimeMillis();
Expand All @@ -161,10 +160,10 @@ public void run( List< SourceAndConverter< ? > > sacs )
final Converter< ?, ? > converter = sac.getConverter();

final int level = getLevel( source, targetVoxelSpacing );
final AffineTransform3D sourceTransform = BdvHandleHelper.getSourceTransform( source, t, level );
final AffineTransform3D sourceTransform = BdvHandleHelper.getSourceTransform( source, currentTimepoint, level );

// global to source
AffineTransform3D targetCanvasToSourceTransform = targetCanvasToGlobalTransform.copy();
AffineTransform3D targetCanvasToSourceTransform = canvasToGlobalTransform.copy();
AffineTransform3D globalToSource = sourceTransform.inverse();
targetCanvasToSourceTransform.preConcatenate( globalToSource );

Expand All @@ -179,7 +178,7 @@ public void run( List< SourceAndConverter< ? > > sacs )
(
ThreadHelper.ioExecutorService.submit( () ->
{
RealRandomAccess< ? extends Type< ? > > access = getRealRandomAccess( ( Source< Type< ? > > ) source, t, level, interpolate );
RealRandomAccess< ? extends Type< ? > > access = getRealRandomAccess( ( Source< Type< ? > > ) source, currentTimepoint, level, interpolate );

// to collect raw data
final IntervalView< FloatType > floatCrop = Views.interval( rawCapture, interval );
Expand Down Expand Up @@ -248,6 +247,11 @@ public void run( List< SourceAndConverter< ? > > sacs )
}
}

public AffineTransform3D getCanvasToGlobalTransform()
{
return canvasToGlobalTransform;
}

private List< SourceAndConverter< ? > > getVisibleSourceAndConverters()
{
final List< SourceAndConverter <?> > visibleSacs = MoBIEHelper.getVisibleSacs( bdvHandle );
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/embl/mobie/lib/bdv/view/SliceViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private void installContextMenuAndKeyboardShortCuts( )
actions.add( SourceAndConverterService.getCommandName( ViewerTransformLoggerCommand.class ) );
actions.add( SourceAndConverterService.getCommandName( SourceInfoLoggerCommand.class ) );
actions.add( SourceAndConverterService.getCommandName( BigWarpRegistrationCommand.class ) );
actions.add( SourceAndConverterService.getCommandName( SIFTXYAlignCommand.class ) );
actions.add( SourceAndConverterService.getCommandName( SIFT2DAlignCommand.class ) );
actions.add( SourceAndConverterService.getCommandName( ManualRegistrationCommand.class ) );
actions.add( SourceAndConverterService.getCommandName( FlipCommand.class ) );
actions.add( UNDO_SEGMENT_SELECTIONS );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.imagej.ImageJ;
import org.embl.mobie.MoBIE;
import org.embl.mobie.command.context.SIFTXYAlignCommand;
import org.embl.mobie.command.context.SIFT2DAlignCommand;
import org.embl.mobie.command.open.OpenMultipleImagesAndLabelsCommand;

import java.io.File;
Expand All @@ -19,7 +19,7 @@ public static void main( String[] args ) throws IOException
command.image0 = new File("/Users/tischer/Desktop/em-xray/xray-slice-ds-0.tif");
command.image1 = new File("/Users/tischer/Desktop/em-xray/em-slice-ds-0.tif");
command.run();
SIFTXYAlignCommand sift2DAlignCommand = new SIFTXYAlignCommand();
SIFT2DAlignCommand sift2DAlignCommand = new SIFT2DAlignCommand();
sift2DAlignCommand.bdvHandle = MoBIE.getInstance().getViewManager().getSliceViewer().getBdvHandle();
sift2DAlignCommand.run();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import net.imagej.ImageJ;
import net.imglib2.realtransform.AffineTransform3D;
import org.embl.mobie.MoBIE;
import org.embl.mobie.command.context.SIFTXYAlignCommand;
import org.embl.mobie.command.open.OpenMultipleImagesAndLabelsCommand;

import java.io.File;
Expand Down

0 comments on commit 3dbeda0

Please sign in to comment.