Java源码示例:mpicbg.models.Tile
示例1
/**
* Estimate min/max/average displacement of all
* {@link PointMatch PointMatches} in all {@link Tile Tiles}.
*/
@Override
protected void updateErrors()
{
double cd = 0.0;
minError = Double.MAX_VALUE;
maxError = 0.0;
for ( Tile< ? > t : tiles )
{
t.updateCost();
double d = t.getDistance();
if ( d < minError ) minError = d;
// >= because if they are all 0.0, worstTile would be null
if ( d >= maxError )
{
maxError = d;
worstTile = t;
}
cd += d;
}
cd /= tiles.size();
error = cd;
}
示例2
private Tile<InterpolatedAffineModel2D<AffineModel2D, B>> buildTileFromSpec(final TileSpec tileSpec) {
final CoordinateTransformList<CoordinateTransform> transformList = tileSpec.getTransformList();
final AffineModel2D lastTransform = (AffineModel2D)
transformList.get(transformList.getList(null).size() - 1);
final AffineModel2D lastTransformCopy = lastTransform.copy();
final double sampleWidth = (tileSpec.getWidth() - 1.0) / (parameters.samplesPerDimension - 1.0);
final double sampleHeight = (tileSpec.getHeight() - 1.0) / (parameters.samplesPerDimension - 1.0);
final B regularizer = parameters.regularizerModelType.getInstance();
try {
ScriptUtil.fit(regularizer, lastTransformCopy, sampleWidth, sampleHeight, parameters.samplesPerDimension);
} catch (final Throwable t) {
throw new IllegalArgumentException(regularizer.getClass() + " model derivation failed for tile '" +
tileSpec.getTileId() + "', cause: " + t.getMessage(),
t);
}
return new Tile<>(new InterpolatedAffineModel2D<>(lastTransformCopy,
regularizer,
parameters.startLambda)); // note: lambda gets reset during optimization loops
}
示例3
final static protected < T extends Model< T > & Affine1D< T > > HashMap< Patch, ArrayList< Tile< T > > > generateCoefficientsTiles(
final Collection< Patch > patches,
final T template,
final int nCoefficients )
{
final HashMap< Patch, ArrayList< Tile< T > > > map = new HashMap< Patch, ArrayList< Tile< T > > >();
for ( final Patch p : patches )
{
final ArrayList< Tile< T > > coefficientModels = new ArrayList< Tile< T > >();
for ( int i = 0; i < nCoefficients; ++i )
coefficientModels.add( new Tile< T >( template.copy() ) );
map.put( p, coefficientModels );
}
return map;
}
示例4
protected static void addPointMatches( final ArrayList< PointMatchGeneric< Detection > > correspondences, final Tile<?> tileA, final Tile<?> tileB )
{
final ArrayList<PointMatch> pm = new ArrayList<PointMatch>();
pm.addAll( correspondences );
if ( correspondences.size() > 0 )
{
tileA.addMatches( pm );
tileB.addMatches( PointMatch.flip( pm ) );
tileA.addConnectedTile( tileB );
tileB.addConnectedTile( tileA );
}
}
示例5
public static void main( String[] args )
{
// multiple keys can map to the same value
final HashMap< ViewId, Tile< AffineModel3D > > map = new HashMap<ViewId, Tile<AffineModel3D>>();
final AffineModel3D m1 = new AffineModel3D();
final AffineModel3D m2 = new AffineModel3D();
final Tile< AffineModel3D > tile1 = new Tile<AffineModel3D>( m1 );
final Tile< AffineModel3D > tile2 = new Tile<AffineModel3D>( m2 );
final ViewId v11 = new ViewId( 1, 1 );
final ViewId v21 = new ViewId( 2, 1 );
final ViewId v12 = new ViewId( 1, 2 );
final ViewId v22 = new ViewId( 2, 2 );
map.put( v11, tile1 );
map.put( v21, tile2 );
map.put( v12, tile1 );
map.put( v22, tile2 );
m1.set( 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 );
m2.set( 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 );
System.out.println( map.get( v11 ).getModel() );
System.out.println( map.get( v21 ).getModel() );
System.out.println( map.get( v12 ).getModel() );
System.out.println( map.get( v22 ).getModel() );
}
示例6
public synchronized static <T extends AbstractDetection<T>> void addPointMatches( final ArrayList<PointMatchGeneric<T>> correspondences, final Tile<?> tileA, final Tile<?> tileB )
{
final ArrayList<PointMatch> pm = new ArrayList<PointMatch>();
pm.addAll( correspondences );
if ( correspondences.size() > 0 )
{
tileA.addMatches( pm );
tileB.addMatches( PointMatch.flip( pm ) );
tileA.addConnectedTile( tileB );
tileB.addConnectedTile( tileA );
}
}
示例7
public Matcher(
final Rectangle roi,
final ValuePair< Patch, Patch > patchPair,
final HashMap< Patch, ArrayList< Tile< ? > > > coefficientsTiles,
final PointMatchFilter filter,
final double scale,
final int numCoefficients )
{
this.roi = roi;
this.patchPair = patchPair;
this.coefficientsTiles = coefficientsTiles;
this.filter = filter;
this.scale = scale;
this.numCoefficients = numCoefficients;
}
示例8
final static protected void identityConnect( final Tile< ? > t1, final Tile< ? > t2, final double weight )
{
final ArrayList< PointMatch > matches = new ArrayList< PointMatch >();
matches.add( new PointMatch( new Point( new double[] { 0 } ), new Point( new double[] { 0 } ) ) );
matches.add( new PointMatch( new Point( new double[] { 1 } ), new Point( new double[] { 1 } ) ) );
t1.connect( t2, matches );
}
示例9
final static protected void pairwiseAlign(
final AbstractAffineTile2D< ? > tile,
final Set< AbstractAffineTile2D< ? > > visited )
{
visited.add( tile );
for ( final Tile< ? > t : tile.getConnectedTiles() )
{
if ( visited.contains( t ) ) continue;
pairwiseAlign( ( AbstractAffineTile2D< ? > )t, visited );
// TODO Actually do it ...
}
}
示例10
/**
* Extract the common {@linkplain PointMatch PointMatches} of two tiles.
*
* @param other
* @param commonMatches
*/
final public void commonPointMatches( final Tile< ? > other, final Collection< PointMatch > commonMatches )
{
for ( final PointMatch pm : matches )
for ( final PointMatch otherPm : other.getMatches() )
if ( pm.getP1() == otherPm.getP2() )
{
commonMatches.add( pm );
break;
}
}
示例11
private void saveTargetStackTiles(final HashMap<String, Tile<InterpolatedAffineModel2D<AffineModel2D, B>>> idToTileMap)
throws IOException {
LOG.info("saveTargetStackTiles: entry");
for (final ResolvedTileSpecCollection resolvedTiles : zToTileSpecsMap.values()) {
final Set<String> tileIdsToRemove = new HashSet<>();
for (final TileSpec tileSpec : resolvedTiles.getTileSpecs()) {
final String tileId = tileSpec.getTileId();
final Tile<InterpolatedAffineModel2D<AffineModel2D, B>> tile = idToTileMap.get(tileId);
if (tile == null) {
tileIdsToRemove.add(tileId);
} else {
resolvedTiles.addTransformSpecToTile(tileId,
getTransformSpec(tile.getModel()),
REPLACE_LAST);
}
if (parameters.mergedZ != null) {
tileSpec.setZ(parameters.mergedZ);
}
}
if (tileIdsToRemove.size() > 0) {
LOG.info("removed {} unaligned tile specs from target collection", tileIdsToRemove.size());
resolvedTiles.removeTileSpecs(tileIdsToRemove);
}
if (resolvedTiles.getTileCount() > 0) {
targetDataClient.saveResolvedTiles(resolvedTiles, parameters.targetStack, null);
} else {
LOG.info("skipping tile spec save since no specs are left to save");
}
}
LOG.info("saveTargetStackTiles: saved tiles for all split sections");
if (parameters.completeTargetStack) {
targetDataClient.setStackState(parameters.targetStack, StackMetaData.StackState.COMPLETE);
}
LOG.info("saveTargetStackTiles: exit");
}
示例12
/**
* @param model
* @param type
* @param spimData
* @param channelsToProcess - just to annotate the registration
* @param description
* @return
*/
public < M extends Model< M > > boolean computeGlobalOpt(
final M model,
final GlobalOptimizationType type,
final SpimData2 spimData,
final List< ChannelProcess > channelsToProcess,
final String description )
{
final HashMap< ViewId, Tile< M > > tiles = GlobalOpt.compute( model, type, this, type.considerTimePointsAsUnit() );
if ( tiles == null )
return false;
String channelList = "[";
for ( final ChannelProcess c : channelsToProcess )
channelList += c.getLabel() + " (c=" + c.getChannel().getName() + "), ";
channelList = channelList.substring( 0, channelList.length() - 2 ) + "]";
final AffineTransform3D mapBackModel;
// TODO: Map back first tile as good as possible to original location???
if ( type.getMapBackReferenceTile( this ) != null && type.getMapBackModel() != null )
mapBackModel = computeMapBackModel( tiles, type, spimData );
else
mapBackModel = null;
// update the view registrations
for ( final ViewId viewId : this.getViews() )
{
final Tile< M > tile = tiles.get( viewId );
// TODO: we assume that M is an Affine3D, which is not necessarily true
final Affine3D< ? > tilemodel = (Affine3D< ? >)tile.getModel();
final double[][] m = new double[ 3 ][ 4 ];
tilemodel.toMatrix( m );
final AffineTransform3D t = new AffineTransform3D();
t.set( m[0][0], m[0][1], m[0][2], m[0][3],
m[1][0], m[1][1], m[1][2], m[1][3],
m[2][0], m[2][1], m[2][2], m[2][3] );
if ( mapBackModel != null )
{
t.preConcatenate( mapBackModel );
IOFunctions.println( "ViewId=" + viewId.getViewSetupId() + ": " + t );
}
Apply_Transformation.preConcatenateTransform( spimData, viewId, t, description + " on " + channelList );
}
return true;
}
示例13
protected < M extends Model< M > > AffineTransform3D computeMapBackModel( final HashMap< ViewId, Tile< M > > tiles, final GlobalOptimizationType type, final SpimData2 spimData )
{
final AbstractModel< ? > mapBackModel = type.getMapBackModel();
if ( mapBackModel.getMinNumMatches() > 4 )
{
IOFunctions.println( "Cannot map back using a model that needs more than 4 points: " + mapBackModel.getClass().getSimpleName() );
return null;
}
else
{
IOFunctions.println( "Mapping back to reference frame using a " + mapBackModel.getClass().getSimpleName() );
final ViewId referenceTile = type.getMapBackReferenceTile( this );
final ViewDescription referenceTileViewDescription = spimData.getSequenceDescription().getViewDescription( referenceTile );
final ViewSetup referenceTileSetup = referenceTileViewDescription.getViewSetup();
Dimensions size = ViewSetupUtils.getSizeOrLoad( referenceTileSetup, referenceTileViewDescription.getTimePoint(), spimData.getSequenceDescription().getImgLoader() );
long w = size.dimension( 0 );
long h = size.dimension( 1 );
final double[][] p = new double[][]{
{ 0, 0, 0 },
{ w, 0, 0 },
{ 0, h, 0 },
{ w, h, 0 } };
// original coordinates == pa
final double[][] pa = new double[ 4 ][ 3 ];
// map coordinates to the actual input coordinates
final ViewRegistration inputModel = spimData.getViewRegistrations().getViewRegistration( referenceTile );
for ( int i = 0; i < p.length; ++i )
inputModel.getModel().apply( p[ i ], pa[ i ] );
final M outputModel = tiles.get( referenceTile ).getModel();
// transformed coordinates == pb
final double[][] pb = new double[ 4 ][ 3 ];
for ( int i = 0; i < p.length; ++i )
pb[ i ] = outputModel.apply( pa[ i ] );
// compute the model that maps pb >> pa
try
{
final ArrayList< PointMatch > pm = new ArrayList< PointMatch >();
for ( int i = 0; i < p.length; ++i )
pm.add( new PointMatch( new Point( pb[ i ] ), new Point( pa[ i ] ) ) );
mapBackModel.fit( pm );
} catch ( Exception e )
{
IOFunctions.println( "Could not compute model for mapping back: " + e );
e.printStackTrace();
return null;
}
final AffineTransform3D mapBack = new AffineTransform3D();
final double[][] m = new double[ 3 ][ 4 ];
((Affine3D<?>)mapBackModel).toMatrix( m );
mapBack.set( m[0][0], m[0][1], m[0][2], + m[0][3],
m[1][0], m[1][1], m[1][2], m[1][3],
m[2][0], m[2][1], m[2][2], m[2][3] );
IOFunctions.println( "Model for mapping back: " + mapBack + "\n" );
return mapBack;
}
}
示例14
/**
* Computes a pre-alignemnt of all non-fixed {@link Tile}s by propagating the pairwise
* models. This does not give a correct registration but a very good starting point
* for the global optimization. This is necessary for models where the global optimization
* is not guaranteed to converge like the {@link HomographyModel2D}, {@link RigidModel3D}, ...
*
* @return - a list of {@link Tile}s that could not be pre-aligned
* @throws NotEnoughDataPointsException
// * @throws {@link IllDefinedDataPointsException}
*/
public List< Tile< ? > > preAlign() throws NotEnoughDataPointsException, IllDefinedDataPointsException
{
// first get order all tiles by
// a) unaligned
// b) aligned - which initially only contains the fixed ones
final ArrayList< Tile< ? > > unAlignedTiles = new ArrayList< Tile< ? > >();
final ArrayList< Tile< ? > > alignedTiles = new ArrayList< Tile< ? > >();
// if no tile is fixed, take another */
if ( getFixedTiles().size() == 0 )
{
final Iterator< ? extends Tile > it = this.getTiles().iterator();
alignedTiles.add( it.next() );
while ( it.hasNext() )
unAlignedTiles.add( it.next() );
}
else
{
for ( final Tile< ? > tile : this.getTiles() )
{
if ( this.getFixedTiles().contains( tile ) )
alignedTiles.add( tile );
else
unAlignedTiles.add( tile );
}
}
// we go through each fixed/aligned tile and try to find a pre-alignment
// for all other unaligned tiles
for ( final ListIterator< Tile< ?> > referenceIterator = alignedTiles.listIterator(); referenceIterator.hasNext(); )
{
// once all tiles are aligned we can quit this loop
if ( unAlignedTiles.size() == 0 )
break;
// get the next reference tile (either a fixed or an already aligned one
final Tile< ? > referenceTile = referenceIterator.next();
// transform all reference points into the reference coordinate system
// so that we get the direct model even if we are not anymore at the
// level of the fixed tile
referenceTile.apply();
// now we go through the unaligned tiles to see if we can align it to the current reference tile one
for ( final ListIterator< Tile< ?> > targetIterator = unAlignedTiles.listIterator(); targetIterator.hasNext(); )
{
// get the tile that we want to preregister
final Tile< ? > targetTile = targetIterator.next();
// target tile is connected to reference tile
if ( referenceTile.getConnectedTiles().contains( targetTile ) )
{
// extract all PointMatches between reference and target tile and fit a model only on these
final ArrayList< PointMatch > pm = getConnectingPointMatches( targetTile, referenceTile );
// are there enough matches?
if ( pm.size() > targetTile.getModel().getMinNumMatches() )
{
// fit the model of the targetTile to the subset of matches
// mapping its local coordinates target.p.l into the world
// coordinates reference.p.w
// this will give us an approximation for the global optimization
targetTile.getModel().fit( pm );
// now that we managed to fit the model we remove the
// Tile from unaligned tiles and add it to aligned tiles
targetIterator.remove();
// now add the aligned target tile to the end of the reference list
int countFwd = 0;
while ( referenceIterator.hasNext() )
{
referenceIterator.next();
++countFwd;
}
referenceIterator.add( targetTile );
// move back to the current position
// (+1 because it add just behind the current position)
for ( int j = 0; j < countFwd + 1; ++j )
referenceIterator.previous();
}
}
}
}
return unAlignedTiles;
}
示例15
final public Tile getWorstTile() { return worstTile; }