use of mpicbg.models.PointMatch in project TrakEM2 by trakem2.
the class RansacRegressionReduceFilter method filter.
@Override
public void filter(final List<PointMatch> candidates, final Collection<PointMatch> inliers) {
try {
if (model.filterRansac(candidates, inliers, iterations, maxEpsilon, minInlierRatio, minNumInliers, maxTrust)) {
model.fit(inliers);
final double[] minMax = minMax(inliers);
inliers.clear();
final Point p1 = new Point(new double[] { minMax[0] });
final Point p2 = new Point(new double[] { minMax[1] });
p1.apply(model);
p2.apply(model);
inliers.add(new PointMatch(p1, new Point(p1.getW().clone())));
inliers.add(new PointMatch(p2, new Point(p2.getW().clone())));
} else
inliers.clear();
} catch (final Exception e) {
inliers.clear();
}
}
use of mpicbg.models.PointMatch in project TrakEM2 by trakem2.
the class RansacRegressionReduceFilter method minMax.
protected static final double[] minMax(final Iterable<PointMatch> matches) {
final Iterator<PointMatch> iter = matches.iterator();
PointMatch m = iter.next();
double min = m.getP1().getL()[0], max = min;
while (iter.hasNext()) {
m = iter.next();
final double x = m.getP1().getL()[0];
if (x < min)
min = x;
else if (x > max)
max = x;
}
return new double[] { min, max };
}
use of mpicbg.models.PointMatch in project TrakEM2 by trakem2.
the class TransformMesh method closestSourceAffine.
/**
* Return the {@link AffineModel2D} used by the triangle enclosing or
* closest to the passed target location.
*
* @param location
* @return
*/
public AffineModel2D closestSourceAffine(final double[] location) {
assert location.length == 2 : "2d transform meshs can be applied to 2d points only.";
final Set<AffineModel2D> s = av.keySet();
for (final AffineModel2D ai : s) {
final ArrayList<PointMatch> pm = av.get(ai);
if (isInSourcePolygon(pm, location))
return ai;
}
/* not in the mesh, find the closest affine */
double dMin = Double.MAX_VALUE;
AffineModel2D closestAffine = new AffineModel2D();
final double x = location[0];
final double y = location[1];
for (final AffineModel2D ai : s) {
final ArrayList<PointMatch> pm = av.get(ai);
double d = 0;
for (final PointMatch p : pm) {
final double[] l = p.getP1().getL();
final double dx = l[0] - x;
final double dy = l[1] - y;
d += Math.sqrt(dx * dx + dy * dy);
}
if (d < dMin) {
dMin = d;
closestAffine = ai;
}
}
return closestAffine;
}
use of mpicbg.models.PointMatch in project TrakEM2 by trakem2.
the class TransformMesh method closestTargetAffine.
/**
* Return the {@link AffineModel2D} used by the triangle enclosing or
* closest to the passed target location.
*
* @param location
* @return
*/
public AffineModel2D closestTargetAffine(final double[] location) {
assert location.length == 2 : "2d transform meshs can be applied to 2d points only.";
final Set<AffineModel2D> s = av.keySet();
for (final AffineModel2D ai : s) {
final ArrayList<PointMatch> pm = av.get(ai);
if (isInConvexTargetPolygon(pm, location))
return ai;
}
/* not in the mesh, find the closest affine */
double dMin = Double.MAX_VALUE;
AffineModel2D closestAffine = new AffineModel2D();
final double x = location[0];
final double y = location[1];
for (final AffineModel2D ai : s) {
final ArrayList<PointMatch> pm = av.get(ai);
double d = 0;
for (final PointMatch p : pm) {
final double[] w = p.getP2().getW();
final double dx = w[0] - x;
final double dy = w[1] - y;
d += Math.sqrt(dx * dx + dy * dy);
}
if (d < dMin) {
dMin = d;
closestAffine = ai;
}
}
return closestAffine;
}
use of mpicbg.models.PointMatch in project TrakEM2 by trakem2.
the class MovingLeastSquaresTransform method toDataString.
private final void toDataString(final StringBuilder data) {
if (AffineModel2D.class.isInstance(model))
data.append("affine 2");
else if (TranslationModel2D.class.isInstance(model))
data.append("translation 2");
else if (RigidModel2D.class.isInstance(model))
data.append("rigid 2");
else if (SimilarityModel2D.class.isInstance(model))
data.append("similarity 2");
else if (AffineModel3D.class.isInstance(model))
data.append("affine 3");
else
data.append("unknown");
data.append(' ').append(alpha);
// Sort matches, so that they are always written the same way
// Will help lots git and .zip to reduce XML file size
final ArrayList<PointMatch> pms = new ArrayList<PointMatch>(matches);
Collections.sort(pms, SORTER);
for (final PointMatch m : pms) {
final double[] p1 = m.getP1().getL();
final double[] p2 = m.getP2().getW();
for (int k = 0; k < p1.length; ++k) data.append(' ').append(p1[k]);
for (int k = 0; k < p2.length; ++k) data.append(' ').append(p2[k]);
data.append(' ').append(m.getWeight());
}
}
Aggregations