Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#@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
You can’t perform that action at this time.