Skip to content

Instantly share code, notes, and snippets.

@bogovicj
Last active February 18, 2019 12:30
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bogovicj/66a5c10009ac64d9d0e0a057d8c72b2b to your computer and use it in GitHub Desktop.
#@Dataset(label="Image to transform") img
#@Double(label="scale") scaling_factor
#@UIService ui
import net.imglib2.*;
import net.imglib2.realtransform.*;
import net.imglib2.view.*;
import net.imglib2.interpolation.*;
import net.imglib2.interpolation.randomaccess.*;
int nd = img.numDimensions();
RealPoint p = new RealPoint(nd);
long N = 0;
c = img.cursor();
while( c.hasNext())
{
if ( c.next().getRealDouble() > 0 )
{
N++;
for( int i = 0; i < nd; i++ )
p.setPosition( p.getDoublePosition( i ) + c.getDoublePosition( i ), i );
}
}
for( int i = 0; i < nd; i++ )
p.setPosition( p.getDoublePosition( i ) / N, i );
center = new double[ nd ];
p.localize( center );
t = new AffineTransform3D(); // centering part
t.translate( center );
transform = new AffineTransform3D();
transform.preConcatenate( t.inverse() ); // center about zero
transform.scale( scaling_factor ); // scale
transform.preConcatenate( t ); // un-center
interp = new NearestNeighborInterpolatorFactory()
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