Last active
June 7, 2022 12:31
-
-
Save bogovicj/bd7794ad32f879942f700c668924d751 to your computer and use it in GitHub Desktop.
Extract the scale parameter for a set of bigwarp landmark points defining a similarity model.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#@ File (label="Landmark file") landmarksPath | |
#@ Integer (label="Number of dimensions", value=3) nd | |
#@ String (label="Direction", choices={"Forward", "Inverse" }, value="Forward") direction | |
import mpicbg.models.*; | |
import bigwarp.landmarks.LandmarkTableModel; | |
import net.imglib2.util.*; | |
// load landmarks | |
tableModel = new LandmarkTableModel( nd ); | |
try | |
{ | |
tableModel.load( landmarksPath ); | |
} catch ( IOException e ) | |
{ | |
e.printStackTrace(); | |
return; | |
} | |
def getModel( final int numDims ) | |
{ | |
if( numDims == 2 ) | |
return new SimilarityModel2D(); | |
else if( numDims == 3 ) | |
return new SimilarityModel3D(); | |
else | |
return null; | |
} | |
/** | |
* Determinant of a 2d transform stored as a row-major 3d homogeneous matrix array | |
*/ | |
def det2d( final double[][] mtx ) | |
{ | |
return (mtx[0][0] * mtx[1][1]) - (mtx[0][1] * mtx[1][0]); | |
} | |
/** | |
* Determinant of a 3d transform stored as a row major 4d homogeneous matrix array | |
*/ | |
def det3d( final double[][] mtx ) | |
{ | |
return mtx[0][0] * mtx[1][1] * mtx[2][2] + | |
mtx[1][0] * mtx[2][1] * mtx[0][2] + | |
mtx[2][0] * mtx[0][1] * mtx[1][2] - | |
mtx[0][2] * mtx[1][1] * mtx[2][0] - | |
mtx[1][2] * mtx[2][1] * mtx[0][0] - | |
mtx[2][2] * mtx[0][1] * mtx[1][0]; | |
} | |
/** | |
* Need this until Fiji's mpicbg versions are updated | |
*/ | |
def double[][] toMatrix2d( final SimilarityModel2D model ) | |
{ | |
a = new double[ 6 ]; | |
model.toArray( a ); | |
return [[ a[0], -a[1], a[4] ], | |
[ a[1], a[0], a[5] ]] as double[][]; | |
} | |
/** | |
* Need this until Fiji's mpicbg versions are updated | |
*/ | |
def double[][] toMatrix3d( final SimilarityModel3D model ) | |
{ | |
mtx = new double[ 3 ][ 4 ]; | |
model.toMatrix( mtx ); | |
return mtx; | |
} | |
model = getModel( nd ); | |
if( model == null ) | |
{ | |
println( "Only works for 2 or 3 dimensions"); | |
return; | |
} | |
// fit the model | |
int numActive = tableModel.numActive(); | |
int ndims = tableModel.getNumdims(); | |
mvgPts = new double[ ndims ][ numActive ]; | |
tgtPts = new double[ ndims ][ numActive ]; | |
tableModel.copyLandmarks( mvgPts, tgtPts ); | |
double[] w = new double[ numActive ]; | |
Arrays.fill( w, 1.0 ); | |
try { | |
model.fit( mvgPts, tgtPts, w ); | |
} catch (NotEnoughDataPointsException e) { | |
e.printStackTrace(); | |
} catch (IllDefinedDataPointsException e) { | |
e.printStackTrace(); | |
} | |
themodel = model; | |
if( direction.equals( "Inverse" ) ) | |
{ | |
themodel = model.createInverse(); | |
println( themodel ) | |
} | |
if( nd == 2 ) | |
{ | |
det = det2d( toMatrix2d( themodel )); | |
avgscale = Math.sqrt( det ); | |
} | |
else if( nd == 3 ) | |
{ | |
det = det3d( toMatrix3d( themodel )); | |
avgscale = Math.cbrt( det ); | |
} | |
println( 'determinant : ' + det ); | |
println( 'avgerage scale : ' + avgscale ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment