Search in sources :

Example 1 with PointMatchCollectionAndAffine

use of lenscorrection.Distortion_Correction.PointMatchCollectionAndAffine in project TrakEM2 by trakem2.

the class DistortionCorrectionTask method run.

public static final void run(final CorrectDistortionFromSelectionParam p, final List<Patch> patches, final Displayable active, final Layer layer, final Worker worker) {
    /* no multiple inheritance, so p cannot be an Align.ParamOptimize, working around legacy by copying data into one ... */
    final Align.ParamOptimize ap = new Align.ParamOptimize();
    ap.sift.set(p.sift);
    ap.desiredModelIndex = p.desiredModelIndex;
    ap.expectedModelIndex = p.expectedModelIndex;
    ap.maxEpsilon = p.maxEpsilon;
    ap.minInlierRatio = p.minInlierRatio;
    ap.rod = p.rod;
    ap.identityTolerance = p.identityTolerance;
    ap.lambda = p.lambdaRegularize;
    ap.maxIterations = p.maxIterationsOptimize;
    ap.maxPlateauwidth = p.maxPlateauwidthOptimize;
    ap.minNumInliers = p.minNumInliers;
    ap.regularize = p.regularize;
    ap.regularizerModelIndex = p.regularizerIndex;
    ap.rejectIdentity = p.rejectIdentity;
    /**
     * Get all patches that will be affected.
     */
    final List<Patch> allPatches = new ArrayList<Patch>();
    for (final Layer l : layer.getParent().getLayers().subList(p.firstLayerIndex, p.lastLayerIndex + 1)) for (final Displayable d : l.getDisplayables(Patch.class)) allPatches.add((Patch) d);
    /**
     * Unset the coordinate transforms of all patches if desired.
     */
    if (p.clearTransform) {
        if (worker != null)
            worker.setTaskName("Clearing present transforms");
        setCoordinateTransform(allPatches, null, Runtime.getRuntime().availableProcessors());
        Display.repaint();
    }
    if (worker != null)
        worker.setTaskName("Establishing SIFT correspondences");
    final List<AbstractAffineTile2D<?>> tiles = new ArrayList<AbstractAffineTile2D<?>>();
    final List<AbstractAffineTile2D<?>> fixedTiles = new ArrayList<AbstractAffineTile2D<?>>();
    final List<Patch> fixedPatches = new ArrayList<Patch>();
    if (active != null && active instanceof Patch)
        fixedPatches.add((Patch) active);
    Align.tilesFromPatches(ap, patches, fixedPatches, tiles, fixedTiles);
    final List<AbstractAffineTile2D<?>[]> tilePairs = new ArrayList<AbstractAffineTile2D<?>[]>();
    if (p.tilesAreInPlace)
        AbstractAffineTile2D.pairOverlappingTiles(tiles, tilePairs);
    else
        AbstractAffineTile2D.pairTiles(tiles, tilePairs);
    AbstractAffineTile2D<?> fixedTile = null;
    if (fixedTiles.size() > 0)
        fixedTile = fixedTiles.get(0);
    else
        fixedTile = tiles.get(0);
    Align.connectTilePairs(ap, tiles, tilePairs, p.maxNumThreadsSift, p.multipleHypotheses);
    /**
     * Shift all local coordinates into the original image frame
     */
    for (final AbstractAffineTile2D<?> tile : tiles) {
        final Rectangle box = tile.getPatch().getCoordinateTransformBoundingBox();
        for (final PointMatch m : tile.getMatches()) {
            final double[] l = m.getP1().getL();
            final double[] w = m.getP1().getW();
            l[0] += box.x;
            l[1] += box.y;
            w[0] = l[0];
            w[1] = l[1];
        }
    }
    if (Thread.currentThread().isInterrupted())
        return;
    final List<Set<Tile<?>>> graphs = AbstractAffineTile2D.identifyConnectedGraphs(tiles);
    if (graphs.size() > 1)
        Utils.log("Could not interconnect all images with correspondences.  ");
    final List<AbstractAffineTile2D<?>> interestingTiles;
    /**
     * Find largest graph.
     */
    Set<Tile<?>> largestGraph = null;
    for (final Set<Tile<?>> graph : graphs) if (largestGraph == null || largestGraph.size() < graph.size())
        largestGraph = graph;
    interestingTiles = new ArrayList<AbstractAffineTile2D<?>>();
    for (final Tile<?> t : largestGraph) interestingTiles.add((AbstractAffineTile2D<?>) t);
    if (Thread.currentThread().isInterrupted())
        return;
    Utils.log("Estimating lens model:");
    /* initialize with pure affine */
    Align.optimizeTileConfiguration(ap, interestingTiles, fixedTiles);
    /* measure the current error */
    double e = 0;
    int n = 0;
    for (final AbstractAffineTile2D<?> t : interestingTiles) for (final PointMatch pm : t.getMatches()) {
        e += pm.getDistance();
        ++n;
    }
    e /= n;
    double dEpsilon_i = 0;
    double epsilon_i = e;
    double dEpsilon_0 = 0;
    NonLinearTransform lensModel = null;
    Utils.log("0: epsilon = " + e);
    /* Store original point locations */
    final HashMap<Point, Point> originalPoints = new HashMap<Point, Point>();
    for (final AbstractAffineTile2D<?> t : interestingTiles) for (final PointMatch pm : t.getMatches()) originalPoints.put(pm.getP1(), pm.getP1().clone());
    /* ad hoc conditions to terminate iteration:
		 * small improvement ( 1/1000) relative to first iteration
		 * less than 20 iterations
		 * at least 2 iterations */
    for (int i = 1; i < 20 && (i < 2 || dEpsilon_i <= dEpsilon_0 / 1000); ++i) {
        if (Thread.currentThread().isInterrupted())
            return;
        /* Some data shuffling for the lens correction interface */
        final List<PointMatchCollectionAndAffine> matches = new ArrayList<PointMatchCollectionAndAffine>();
        for (final AbstractAffineTile2D<?>[] tilePair : tilePairs) {
            final AffineTransform a = tilePair[0].createAffine();
            a.preConcatenate(tilePair[1].getModel().createInverseAffine());
            final Collection<PointMatch> commonMatches = new ArrayList<PointMatch>();
            tilePair[0].commonPointMatches(tilePair[1], commonMatches);
            final Collection<PointMatch> originalCommonMatches = new ArrayList<PointMatch>();
            for (final PointMatch pm : commonMatches) originalCommonMatches.add(new PointMatch(originalPoints.get(pm.getP1()), originalPoints.get(pm.getP2())));
            matches.add(new PointMatchCollectionAndAffine(a, originalCommonMatches));
        }
        if (worker != null)
            worker.setTaskName("Estimating lens distortion correction");
        lensModel = Distortion_Correction.createInverseDistortionModel(matches, p.dimension, p.lambda, (int) fixedTile.getWidth(), (int) fixedTile.getHeight());
        /* update local points */
        for (final AbstractAffineTile2D<?> t : interestingTiles) for (final PointMatch pm : t.getMatches()) {
            final Point currentPoint = pm.getP1();
            final Point originalPoint = originalPoints.get(currentPoint);
            final double[] l = currentPoint.getL();
            final double[] lo = originalPoint.getL();
            l[0] = lo[0];
            l[1] = lo[1];
            lensModel.applyInPlace(l);
        }
        /* re-optimize */
        Align.optimizeTileConfiguration(ap, interestingTiles, fixedTiles);
        /* measure the current error */
        e = 0;
        n = 0;
        for (final AbstractAffineTile2D<?> t : interestingTiles) for (final PointMatch pm : t.getMatches()) {
            e += pm.getDistance();
            ++n;
        }
        e /= n;
        dEpsilon_i = e - epsilon_i;
        epsilon_i = e;
        if (i == 1)
            dEpsilon_0 = dEpsilon_i;
        Utils.log(i + ": epsilon = " + e);
        Utils.log(i + ": delta epsilon = " + dEpsilon_i);
    }
    if (lensModel != null) {
        if (p.visualize) {
            if (Thread.currentThread().isInterrupted())
                return;
            if (worker != null)
                worker.setTaskName("Visualizing lens distortion correction");
            lensModel.visualizeSmall(p.lambda);
        }
        if (worker != null)
            worker.setTaskName("Applying lens distortion correction");
        appendCoordinateTransform(allPatches, lensModel, Runtime.getRuntime().availableProcessors());
        Utils.log("Done.");
    } else
        Utils.log("No lens model found.");
}
Also used : Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Rectangle(java.awt.Rectangle) PointMatchCollectionAndAffine(lenscorrection.Distortion_Correction.PointMatchCollectionAndAffine) Displayable(ini.trakem2.display.Displayable) Align(mpicbg.trakem2.align.Align) AbstractAffineTile2D(mpicbg.trakem2.align.AbstractAffineTile2D) Tile(mpicbg.models.Tile) Point(mpicbg.models.Point) Layer(ini.trakem2.display.Layer) Point(mpicbg.models.Point) PointMatch(mpicbg.models.PointMatch) AffineTransform(java.awt.geom.AffineTransform) Patch(ini.trakem2.display.Patch)

Aggregations

Displayable (ini.trakem2.display.Displayable)1 Layer (ini.trakem2.display.Layer)1 Patch (ini.trakem2.display.Patch)1 Rectangle (java.awt.Rectangle)1 AffineTransform (java.awt.geom.AffineTransform)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1 PointMatchCollectionAndAffine (lenscorrection.Distortion_Correction.PointMatchCollectionAndAffine)1 Point (mpicbg.models.Point)1 PointMatch (mpicbg.models.PointMatch)1 Tile (mpicbg.models.Tile)1 AbstractAffineTile2D (mpicbg.trakem2.align.AbstractAffineTile2D)1 Align (mpicbg.trakem2.align.Align)1