Search in sources :

Example 6 with NotEnoughDataPointsException

use of mpicbg.models.NotEnoughDataPointsException in project TrakEM2 by trakem2.

the class PolynomialModel2D method fit.

@Override
public <P extends PointMatch> void fit(final Collection<P> pointMatches) throws NotEnoughDataPointsException, IllDefinedDataPointsException {
    if (pointMatches.size() < getMinNumMatches())
        throw new NotEnoughDataPointsException(pointMatches.size() + " data points are not enough to estimate a 2d polynomial of order " + nlt.getDimension() + ", at least " + getMinNumMatches() + " data points required.");
    affine.fit(pointMatches);
    final double[][] h1 = new double[pointMatches.size()][2];
    final double[][] h2 = new double[pointMatches.size()][2];
    int i = 0;
    for (final P match : pointMatches) {
        final double[] tmp1 = match.getP1().getL().clone();
        affine.applyInPlace(tmp1);
        final double[] tmp2 = match.getP2().getW();
        h1[i] = new double[] { tmp1[0], tmp1[1] };
        h2[i] = new double[] { tmp2[0], tmp2[1] };
        ++i;
    }
    nlt.fit(h1, h2, lambda);
}
Also used : NotEnoughDataPointsException(mpicbg.models.NotEnoughDataPointsException)

Example 7 with NotEnoughDataPointsException

use of mpicbg.models.NotEnoughDataPointsException in project TrakEM2 by trakem2.

the class NonLinearTransformMode method doPainterUpdate.

@Override
protected void doPainterUpdate(final Rectangle r, final double m) {
    try {
        final CoordinateTransform mlst = createCT();
        final SimilarityModel2D toWorld = new SimilarityModel2D();
        toWorld.set(1.0 / m, 0, r.x - ScreenPatchRange.pad / m, r.y - ScreenPatchRange.pad / m);
        final mpicbg.models.CoordinateTransformList<mpicbg.models.CoordinateTransform> ctl = new mpicbg.models.CoordinateTransformList<mpicbg.models.CoordinateTransform>();
        ctl.add(toWorld);
        ctl.add(mlst);
        ctl.add(toWorld.createInverse());
        final CoordinateTransformMesh ctm = new CoordinateTransformMesh(ctl, 32, r.width * m + 2 * ScreenPatchRange.pad, r.height * m + 2 * ScreenPatchRange.pad);
        final TransformMeshMappingWithMasks<CoordinateTransformMesh> mapping = new TransformMeshMappingWithMasks<CoordinateTransformMesh>(ctm);
        // keep a pointer to the current list
        final HashMap<Paintable, GroupingMode.ScreenPatchRange<?>> screenPatchRanges = this.screenPatchRanges;
        for (final GroupingMode.ScreenPatchRange spr : screenPatchRanges.values()) {
            if (screenPatchRanges != this.screenPatchRanges) {
                // TODO should it call itself:  doPainterUpdate( r, m );
                break;
            }
            spr.update(mapping);
        }
    } catch (final NotEnoughDataPointsException e) {
    } catch (final NoninvertibleModelException e) {
    } catch (final IllDefinedDataPointsException e) {
    } catch (final Exception e) {
        e.printStackTrace();
    }
}
Also used : NotEnoughDataPointsException(mpicbg.models.NotEnoughDataPointsException) NoninvertibleModelException(mpicbg.models.NoninvertibleModelException) CoordinateTransformList(mpicbg.trakem2.transform.CoordinateTransformList) IllDefinedDataPointsException(mpicbg.models.IllDefinedDataPointsException) NotEnoughDataPointsException(mpicbg.models.NotEnoughDataPointsException) NoninvertibleModelException(mpicbg.models.NoninvertibleModelException) IllDefinedDataPointsException(mpicbg.models.IllDefinedDataPointsException) CoordinateTransformMesh(mpicbg.models.CoordinateTransformMesh) TransformMeshMappingWithMasks(mpicbg.trakem2.transform.TransformMeshMappingWithMasks) SimilarityModel2D(mpicbg.models.SimilarityModel2D) CoordinateTransform(mpicbg.trakem2.transform.CoordinateTransform)

Example 8 with NotEnoughDataPointsException

use of mpicbg.models.NotEnoughDataPointsException in project TrakEM2 by trakem2.

the class MovingLeastSquaresTransform method applyInPlace.

@Override
public final void applyInPlace(final double[] location) {
    final Collection<PointMatch> weightedMatches = new ArrayList<PointMatch>();
    for (final PointMatch m : matches) {
        final double[] l = m.getP1().getL();
        double s = 0;
        for (int i = 0; i < location.length; ++i) {
            final double dx = l[i] - location[i];
            s += dx * dx;
        }
        if (s <= 0) {
            final double[] w = m.getP2().getW();
            for (int i = 0; i < location.length; ++i) location[i] = w[i];
            return;
        }
        final double weight = m.getWeight() * weigh(s);
        final PointMatch mw = new PointMatch(m.getP1(), m.getP2(), weight);
        weightedMatches.add(mw);
    }
    try {
        synchronized (model) {
            model.fit(weightedMatches);
            model.applyInPlace(location);
        }
    } catch (final IllDefinedDataPointsException e) {
    } catch (final NotEnoughDataPointsException e) {
    }
}
Also used : PointMatch(mpicbg.models.PointMatch) NotEnoughDataPointsException(mpicbg.models.NotEnoughDataPointsException) IllDefinedDataPointsException(mpicbg.models.IllDefinedDataPointsException) ArrayList(java.util.ArrayList) Point(mpicbg.models.Point)

Example 9 with NotEnoughDataPointsException

use of mpicbg.models.NotEnoughDataPointsException in project TrakEM2 by trakem2.

the class Distortion_Correction method extractSIFTPointsThreaded.

protected static void extractSIFTPointsThreaded(final int index, final List<Feature>[] siftFeatures, final List<PointMatch>[] inliers, final AbstractAffineModel2D<?>[] models) {
    // save all matching candidates
    final List<PointMatch>[] candidates = new List[siftFeatures.length - 1];
    final Thread[] threads = MultiThreading.newThreads();
    // start at second
    final AtomicInteger ai = new AtomicInteger(0);
    for (int ithread = 0; ithread < threads.length; ++ithread) {
        threads[ithread] = new Thread() {

            @Override
            public void run() {
                setPriority(Thread.NORM_PRIORITY);
                for (int j = ai.getAndIncrement(); j < candidates.length; j = ai.getAndIncrement()) {
                    final int i = (j < index ? j : j + 1);
                    candidates[j] = FloatArray2DSIFT.createMatches(siftFeatures[index], siftFeatures[i], 1.5f, null, Float.MAX_VALUE, 0.5f);
                }
            }
        };
    }
    MultiThreading.startAndJoin(threads);
    // get rid of the outliers and save the rigid transformations to match
    // the inliers
    final AtomicInteger ai2 = new AtomicInteger(0);
    for (int ithread = 0; ithread < threads.length; ++ithread) {
        threads[ithread] = new Thread() {

            @Override
            public void run() {
                setPriority(Thread.NORM_PRIORITY);
                for (int i = ai2.getAndIncrement(); i < candidates.length; i = ai2.getAndIncrement()) {
                    final List<PointMatch> tmpInliers = new ArrayList<PointMatch>();
                    // RigidModel2D m =
                    // RigidModel2D.estimateBestModel(candidates.get(i),
                    // tmpInliers, sp.min_epsilon, sp.max_epsilon,
                    // sp.min_inlier_ratio);
                    final AbstractAffineModel2D<?> m;
                    switch(sp.expectedModelIndex) {
                        case 0:
                            m = new TranslationModel2D();
                            break;
                        case 1:
                            m = new RigidModel2D();
                            break;
                        case 2:
                            m = new SimilarityModel2D();
                            break;
                        case 3:
                            m = new AffineModel2D();
                            break;
                        default:
                            return;
                    }
                    boolean modelFound = false;
                    try {
                        modelFound = m.filterRansac(candidates[i], tmpInliers, 1000, sp.maxEpsilon, sp.minInlierRatio, 10);
                    } catch (final NotEnoughDataPointsException e) {
                        modelFound = false;
                    }
                    if (modelFound)
                        IJ.log("Model found:\n  " + candidates[i].size() + " candidates\n  " + tmpInliers.size() + " inliers\n  " + String.format("%.2f", m.getCost()) + "px average displacement");
                    else
                        IJ.log("No Model found.");
                    inliers[index * (sp.numberOfImages - 1) + i] = tmpInliers;
                    models[index * (sp.numberOfImages - 1) + i] = m;
                // System.out.println("**** MODEL ADDED: " +
                // (index*(sp.numberOfImages-1)+i));
                }
            }
        };
    }
    MultiThreading.startAndJoin(threads);
}
Also used : NotEnoughDataPointsException(mpicbg.models.NotEnoughDataPointsException) AbstractAffineModel2D(mpicbg.models.AbstractAffineModel2D) PointMatch(mpicbg.models.PointMatch) RigidModel2D(mpicbg.models.RigidModel2D) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractAffineModel2D(mpicbg.models.AbstractAffineModel2D) AffineModel2D(mpicbg.models.AffineModel2D) ArrayList(java.util.ArrayList) List(java.util.List) TranslationModel2D(mpicbg.models.TranslationModel2D) SimilarityModel2D(mpicbg.models.SimilarityModel2D)

Example 10 with NotEnoughDataPointsException

use of mpicbg.models.NotEnoughDataPointsException in project TrakEM2 by trakem2.

the class Distortion_Correction method extractSIFTPoints.

protected void extractSIFTPoints(final int index, final List<Feature>[] siftFeatures, final List<List<PointMatch>> inliers, final List<AbstractAffineModel2D<?>> models) {
    // save all matching candidates
    final List<List<PointMatch>> candidates = new ArrayList<List<PointMatch>>();
    for (int j = 0; j < siftFeatures.length; j++) {
        if (index == j)
            continue;
        candidates.add(FloatArray2DSIFT.createMatches(siftFeatures[index], siftFeatures[j], 1.5f, null, Float.MAX_VALUE, 0.5f));
    }
    // get rid of the outliers and save the transformations to match the inliers
    for (int i = 0; i < candidates.size(); ++i) {
        final List<PointMatch> tmpInliers = new ArrayList<PointMatch>();
        final AbstractAffineModel2D<?> m;
        switch(sp.expectedModelIndex) {
            case 0:
                m = new TranslationModel2D();
                break;
            case 1:
                m = new RigidModel2D();
                break;
            case 2:
                m = new SimilarityModel2D();
                break;
            case 3:
                m = new AffineModel2D();
                break;
            default:
                return;
        }
        try {
            m.filterRansac(candidates.get(i), tmpInliers, 1000, sp.maxEpsilon, sp.minInlierRatio, 10);
        } catch (final NotEnoughDataPointsException e) {
            e.printStackTrace();
        }
        inliers.add(tmpInliers);
        models.add(m);
    }
}
Also used : PointMatch(mpicbg.models.PointMatch) NotEnoughDataPointsException(mpicbg.models.NotEnoughDataPointsException) RigidModel2D(mpicbg.models.RigidModel2D) ArrayList(java.util.ArrayList) AbstractAffineModel2D(mpicbg.models.AbstractAffineModel2D) AffineModel2D(mpicbg.models.AffineModel2D) ArrayList(java.util.ArrayList) List(java.util.List) TranslationModel2D(mpicbg.models.TranslationModel2D) SimilarityModel2D(mpicbg.models.SimilarityModel2D)

Aggregations

NotEnoughDataPointsException (mpicbg.models.NotEnoughDataPointsException)16 ArrayList (java.util.ArrayList)14 PointMatch (mpicbg.models.PointMatch)13 Point (mpicbg.models.Point)12 SimilarityModel2D (mpicbg.models.SimilarityModel2D)12 AffineModel2D (mpicbg.models.AffineModel2D)10 Patch (ini.trakem2.display.Patch)8 AbstractAffineModel2D (mpicbg.models.AbstractAffineModel2D)8 Layer (ini.trakem2.display.Layer)7 Rectangle (java.awt.Rectangle)7 IllDefinedDataPointsException (mpicbg.models.IllDefinedDataPointsException)6 AffineTransform (java.awt.geom.AffineTransform)5 SIFT (mpicbg.ij.SIFT)5 Feature (mpicbg.imagefeatures.Feature)5 FloatArray2DSIFT (mpicbg.imagefeatures.FloatArray2DSIFT)5 RigidModel2D (mpicbg.models.RigidModel2D)5 TranslationModel2D (mpicbg.models.TranslationModel2D)5 RigidModel2D (mpicbg.trakem2.transform.RigidModel2D)5 TranslationModel2D (mpicbg.trakem2.transform.TranslationModel2D)5 ExecutorService (java.util.concurrent.ExecutorService)3