Skip to content

Commit

Permalink
Improve SIFT Dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
tischi committed Dec 28, 2023
1 parent f69f0b8 commit 65197bc
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,61 @@
import bdv.util.BdvHandle;
import bdv.viewer.SourceAndConverter;
import ij.IJ;
import ij.gui.NonBlockingGenericDialog;
import ij.gui.YesNoCancelDialog;
import net.imglib2.realtransform.AffineTransform3D;
import org.embl.mobie.command.CommandConstants;
import org.scijava.command.Interactive;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.widget.Button;
import sc.fiji.bdvpg.scijava.command.BdvPlaygroundActionCommand;

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

@Parameter
public BdvHandle bdvHandle;

@Parameter ( label = "Compute Alignment", callback = "compute")
private Button compute;

@Parameter ( label = "Toggle Alignment", callback = "toggle")
private Button toggle;

private AffineTransform3D previousTransform;
private AffineTransform3D newTransform;
private TransformedSource< ? > transformedSource;
private boolean isAligned;

@Override
public void run()
{
//
}

private void toggle()
{
if ( transformedSource == null )
{
IJ.showMessage( "Please first [ Compute Alignment ]." );
return;
}

if ( isAligned )
{
transformedSource.setFixedTransform( previousTransform );
}
else
{
transformedSource.setFixedTransform( newTransform );
}

bdvHandle.getViewerPanel().requestRepaint();
isAligned = ! isAligned;
}

private void compute()
{
IJ.log("# SIFT registration" +
"\nThe registration is computed in the currently visible 2D plane" +
Expand All @@ -63,33 +100,26 @@ public void run()
if ( movingSac.getSpimSource() instanceof TransformedSource )
{
// apply the transformation
AffineTransform3D siftTransform3D = aligner.getSiftTransform3D();
TransformedSource< ? > transformedSource = ( TransformedSource< ? > ) movingSac.getSpimSource();
AffineTransform3D previousFixedTransform = new AffineTransform3D();
transformedSource.getFixedTransform( previousFixedTransform );
AffineTransform3D newFixedTransform = previousFixedTransform.copy();
newFixedTransform.preConcatenate( siftTransform3D );
transformedSource.setFixedTransform( newFixedTransform );
bdvHandle.getViewerPanel().requestRepaint();
AffineTransform3D siftTransform = aligner.getSiftTransform3D();
transformedSource = ( TransformedSource< ? > ) movingSac.getSpimSource();
previousTransform = new AffineTransform3D();
transformedSource.getFixedTransform( previousTransform );
newTransform = previousTransform.copy();
newTransform.preConcatenate( siftTransform );
transformedSource.setFixedTransform( newTransform );
IJ.showMessage( "Transforming " + transformedSource.getName() );
IJ.showMessage( "Previous Transform: " + previousTransform );
IJ.showMessage( "Additional SIFT Transform: " + siftTransform );
IJ.showMessage( "Combined Transform: " + newTransform );

// ask user whether to accept the transformation
NonBlockingGenericDialog dialog = new NonBlockingGenericDialog( "SIFT Alignment" );
dialog.addMessage( "Press OK to keep the transformation" );
dialog.addMessage( "Press Cancel to discard the transformation" );
dialog.showDialog();
if ( dialog.wasOKed() )
{
IJ.log( "Transformed " + movingSac.getSpimSource().getName() + " with " + siftTransform3D );
}
else if ( dialog.wasCanceled() )
{
transformedSource.setFixedTransform( previousFixedTransform );
bdvHandle.getViewerPanel().requestRepaint();
}
isAligned = true;
bdvHandle.getViewerPanel().requestRepaint();
}
else
{
IJ.log("Cannot apply transformation to image of type " + movingSac.getSpimSource().getClass() );
}
}


}
18 changes: 12 additions & 6 deletions src/main/java/org/embl/mobie/command/context/SIFT2DAligner.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public class SIFT2DAligner
private SourceAndConverter< ? > fixedSac;
private SourceAndConverter< ? > movingSac;
private ScreenShotMaker screenShotMaker;
private static String fixedImageName;
private static String movingImageName;

static private class Param
{
Expand Down Expand Up @@ -135,8 +137,9 @@ public SIFT2DAligner( BdvHandle bdvHandle )

public boolean showUI()
{
if( p.pixelSize == null )
p.pixelSize = BdvHandleHelper.getViewerVoxelSpacing( bdvHandle );
double viewerVoxelSpacing = BdvHandleHelper.getViewerVoxelSpacing( bdvHandle );

p.pixelSize = 2 * viewerVoxelSpacing;

List< SourceAndConverter< ? > > sourceAndConverters = MoBIEHelper.getVisibleSacs( bdvHandle );
if ( sourceAndConverters.size() < 2 )
Expand All @@ -151,8 +154,11 @@ public boolean showUI()

final GenericDialog gd = new GenericDialog( "SIFT 2D Aligner" );

gd.addChoice( "fixed_image :", titles, titles[ 0 ] );
gd.addChoice( "moving_image :", titles, titles[ 1 ] );
String fixedDefault = Arrays.asList( titles ).contains( fixedImageName ) ? fixedImageName : titles[ 0 ];
gd.addChoice( "fixed_image :", titles, fixedDefault );

String movingDefault = Arrays.asList( titles ).contains( movingImageName ) ? movingImageName : titles[ 1 ];
gd.addChoice( "moving_image :", titles, movingDefault );
String voxelUnit = sourceAndConverters.get( 0 ).getSpimSource().getVoxelDimensions().unit();

gd.addNumericField( "pixel_size :", p.pixelSize, 2, 6, voxelUnit );
Expand All @@ -178,8 +184,8 @@ public boolean showUI()

if (gd.wasCanceled()) return false;

String fixedImageName = gd.getNextChoice();
String movingImageName = gd.getNextChoice();
fixedImageName = gd.getNextChoice();
movingImageName = gd.getNextChoice();

p.pixelSize = gd.getNextNumber();
p.modelIndex = gd.getNextChoiceIndex();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.scijava.plugin.Plugin;
import org.scijava.widget.Button;

import java.io.File;
import java.io.IOException;


Expand Down
2 changes: 1 addition & 1 deletion src/test/java/projects/em_xray_alignment/OpenEMXRAY.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class OpenEMXRAY
{
public static void main( String[] args ) throws IOException
{
OpenerLogging.setLogging( true );
// OpenerLogging.setLogging( true );
final ImageJ imageJ = new ImageJ();
imageJ.ui().showUI();
new MoBIE("/Volumes/cba/exchange/em-xray-alignment/mobie" );
Expand Down

0 comments on commit 65197bc

Please sign in to comment.