Skip to content

Instantly share code, notes, and snippets.

@bogovicj
Last active April 2, 2019 17:27
Show Gist options
  • Save bogovicj/d94c35c4bd9ac698e807c6d446d49bdb to your computer and use it in GitHub Desktop.
Save bogovicj/d94c35c4bd9ac698e807c6d446d49bdb to your computer and use it in GitHub Desktop.
ImageJ / Fiji: Parses a string representing an affine transormation, applies it to an image, and displays it.
#@Dataset(label="Image to transform") img
#@String(label="affine transform string") affineString
#@UIService ui
import net.imglib2.realtransform.*;
import net.imglib2.view.*;
import net.imglib2.interpolation.*;
import net.imglib2.interpolation.randomaccess.NLinearInterpolatorFactory;
nd = 3
if( affineString.startsWith( "[3,3]" )){
println( "2" )
nd = 2
} else if( affineString.startsWith( "[4,4]" )){
println("3")
}
paramString = affineString.replace( "[3,3](AffineTransform", "" ).replaceAll( "\\) .*", "" ).replaceAll( "[\\]\\[\\s]", "" )
paramDouble = []
for ( p in paramString.split(",") ) {
paramDouble.add( Double.parseDouble( p ));
}
transform = null;
if( nd == 2 )
transform = new AffineTransform2D();
else if ( nd == 3 ){
transform = new AffineTransform3D();
}
transform.set( paramDouble as double[] )
println( transform )
interp = new NLinearInterpolatorFactory()
result = Views.interval(
Views.raster(
RealViews.transform(
Views.interpolate( Views.extendZero( img ), interp),
transform )),
img );
ui.show( "transformed", result )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment