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);
}
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();
}
}
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) {
}
}
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);
}
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);
}
}
Aggregations