Skip to content

Instantly share code, notes, and snippets.

@bogovicj
Created July 21, 2022 17:05
Show Gist options
  • Save bogovicj/e09acc130084ce64088fa411491b43fc to your computer and use it in GitHub Desktop.
Save bogovicj/e09acc130084ce64088fa411491b43fc to your computer and use it in GitHub Desktop.
Rigidly align slices using landmarks (ImageJ2)
#@ ImagePlus imp
#@ UIService ui
/**
* Rigidly align slices with point rois.
*
* Usage:
* 1) Click at least 3 point landmarks in every slice of your image.
* There must be an equal number of landmarks in each slice.
* Landmarks must be clicked in the same order in each slice.
* 2) Run this script.
*
* John Bogovic
* July 2022
*/
def fitModel( p, q )
{
N = p[0].size();
double[][] pa = p as double[][];
double[][] qa = q as double[][];
model = new RigidModel2D();
w = (0..<N).collect{ 1 } as double[];
model.fit( pa, qa, w );
return model;
}
def toAffine( model )
{
aff = new AffineTransform2D();
mtx = new double[2][3];
model.toMatrix( mtx );
affine = new double[ 6 ];
affine[ 0 ] = mtx[ 0 ][ 0 ];
affine[ 1 ] = mtx[ 0 ][ 1 ];
affine[ 2 ] = mtx[ 0 ][ 2 ];
affine[ 3 ] = mtx[ 1 ][ 0 ];
affine[ 4 ] = mtx[ 1 ][ 1 ];
affine[ 5 ] = mtx[ 1 ][ 2 ];
aff.set( affine );
return aff;
}
def getPoints( roi, slice )
{
pts = [];
poly = roi.getFloatPolygon();
(0..<roi.getNCoordinates()).forEach {
if( roi.getPointPosition(it) == slice )
{
pts += [[poly.xpoints[it], poly.ypoints[it]]];
}
}
return pts.transpose();
}
roi = imp.getRoi();
if( roi.getType() != Roi.POINT )
{
IJ.showMessage("need to have a point roi");
return;
}
N = roi.getNCoordinates();
ns = imp.getNSlices();
transforms = [ new AffineTransform2D() ]
j = 1;
(2..ns).reverse().forEach {
i = it - 1;
p = getPoints( roi, it-1 );
q = getPoints( roi, it );
model = fitModel( p, q );
affine = toAffine( model );
affine.concatenate( transforms[j-1]);
transforms += [ affine ];
j = j + 1;
}
transforms = transforms.reverse();
println( transforms );
img = ImageJFunctions.wrap( imp );
slices = [];
(0..<ns).forEach {
println( it );
slc = Views.hyperSlice(img, 2, it);
imgXfm = RealViews.affine(
Views.interpolate(
Views.extendZero( slc ),
new NLinearInterpolatorFactory<>() ),
transforms[it] );
slices += [ Views.interval( Views.raster( imgXfm ), slc )];
}
println( slices );
xfmStack = Views.stack( slices );
ui.show( xfmStack );
/*
* Copyright 2022 John Bogovic
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* ********************
* only imports below *
******************** */
import java.util.Collections;
import ij.IJ;
import ij.gui.Roi;
import mpicbg.models.RigidModel2D;
import net.imglib2.realtransform.AffineTransform2D;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.img.display.imagej.ImageJFunctions;
import net.imglib2.interpolation.randomaccess.NLinearInterpolatorFactory;
import net.imglib2.realtransform.AffineGet;
import net.imglib2.realtransform.AffineRandomAccessible;
import net.imglib2.realtransform.AffineTransform2D;
import net.imglib2.realtransform.RealViews;
import net.imglib2.type.numeric.integer.UnsignedByteType;
import net.imglib2.view.Views;
import bigwarp.transforms.WrappedCoordinateTransform;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment