Search in sources :

Example 1 with NoninvertibleModelException

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

the class Blending method intersects.

/**
 * Returns true if fo[0,1] x,y world coords intersect the affine and potentially coordinate transformed pixels of the other Patch.
 */
private static double intersects(final double[] fo, final Patch other, final TransformMesh mesh) {
    // First inverse affine transform
    final AffineTransform at = other.getAffineTransform();
    final Point2D.Double po = new Point2D.Double(fo[0], fo[1]);
    final int o_width = other.getOWidth();
    final int o_height = other.getOHeight();
    try {
        at.inverseTransform(po, po);
    } catch (final NoninvertibleTransformException nite) {
        return -1;
    }
    if (null == mesh) {
        if (po.x >= 0 && po.x < o_width && po.y >= 0 && po.y < o_height) {
            return computeWeight(po.x, po.y, o_width, o_height);
        } else {
            return -1;
        }
    }
    // Then inverse the coordinate transform
    try {
        fo[0] = po.x;
        fo[1] = po.y;
        mesh.applyInverseInPlace(fo);
        return computeWeight(fo[0], fo[1], o_width, o_height);
    } catch (final NoninvertibleModelException nime) {
        // outside boundaries
        return -1;
    }
}
Also used : NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) Point2D(java.awt.geom.Point2D) NoninvertibleModelException(mpicbg.models.NoninvertibleModelException) AffineTransform(java.awt.geom.AffineTransform)

Example 2 with NoninvertibleModelException

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

the class AbstractAffineTile2D method makeVirtualConnection.

/**
 * Add a virtual {@linkplain PointMatch connection} between two
 * {@linkplain AbstractAffineTile2D Tiles}.  The
 * {@linkplain PointMatch connection} is placed in the center of the
 * intersection area of both tiles.
 *
 * TODO Not yet tested---Do we need these virtual connections?
 *
 * @param t
 */
public final void makeVirtualConnection(final AbstractAffineTile2D<?> t) {
    final Area a = new Area(patch.getPerimeter());
    final Area b = new Area(t.patch.getPerimeter());
    a.intersect(b);
    final double[] fa = new double[2];
    int i = 0;
    final double[] coords = new double[6];
    final PathIterator p = a.getPathIterator(null);
    while (!p.isDone()) {
        p.currentSegment(coords);
        fa[0] += coords[0];
        fa[1] += coords[1];
        ++i;
        p.next();
    }
    if (i > 0) {
        fa[0] /= i;
        fa[1] /= i;
        final double[] fb = fa.clone();
        try {
            model.applyInverseInPlace(fa);
            t.model.applyInverseInPlace(fb);
        } catch (final NoninvertibleModelException e) {
            return;
        }
        final Point pa = new Point(fa);
        final Point pb = new Point(fb);
        final PointMatch ma = new PointMatch(pa, pb, 0.1f);
        final PointMatch mb = new PointMatch(pb, pa, 0.1f);
        addVirtualMatch(ma);
        addConnectedTile(t);
        t.addVirtualMatch(mb);
        t.addConnectedTile(this);
    }
}
Also used : PointMatch(mpicbg.models.PointMatch) Area(java.awt.geom.Area) PathIterator(java.awt.geom.PathIterator) NoninvertibleModelException(mpicbg.models.NoninvertibleModelException) Point(mpicbg.models.Point) Point(mpicbg.models.Point)

Example 3 with NoninvertibleModelException

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

the class Distortion_Correction method applyTransformToImageInverse.

ImageProcessor applyTransformToImageInverse(final AbstractAffineModel2D<?> a, final ImageProcessor ip) {
    final ImageProcessor newIp = ip.duplicate();
    newIp.max(0.0);
    for (int x = 0; x < ip.getWidth(); x++) {
        for (int y = 0; y < ip.getHeight(); y++) {
            final double[] position = new double[] { x, y };
            // float[] newPosition = a.apply(position);
            double[] newPosition = new double[] { 0, 0 };
            try {
                newPosition = a.applyInverse(position);
            } catch (final NoninvertibleModelException e) {
            }
            final int xn = (int) newPosition[0];
            final int yn = (int) newPosition[1];
            if ((xn >= 0) && (yn >= 0) && (xn < ip.getWidth()) && (yn < ip.getHeight()))
                newIp.set(xn, yn, ip.get(x, y));
        }
    }
    return newIp;
}
Also used : ImageProcessor(ij.process.ImageProcessor) NoninvertibleModelException(mpicbg.models.NoninvertibleModelException)

Example 4 with NoninvertibleModelException

use of mpicbg.models.NoninvertibleModelException 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 5 with NoninvertibleModelException

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

the class Patch method toPixelCoordinate.

/**
 * @see Patch#toPixelCoordinate(double, double)
 * @param world_x The X of the world coordinate (in pixels, uncalibrated)
 * @param world_y The Y of the world coordinate (in pixels, uncalibrated)
 * @param aff The {@link AffineTransform} of the {@link Patch}.
 * @param ct The {@link CoordinateTransform} of the {@link Patch}, if any (can be null).
 * @param meshResolution The precision demanded for approximating a transform with a {@link TransformMesh}.
 * @param o_width The width of the image underlying the {@link Patch}.
 * @param o_height The height of the image underlying the {@link Patch}.
 * @return A {@code double[]} array with the x,y values.
 * @throws NoninvertibleTransformException
 */
public static final double[] toPixelCoordinate(final double world_x, final double world_y, final AffineTransform aff, final CoordinateTransform ct, final int meshResolution, final int o_width, final int o_height) throws NoninvertibleTransformException {
    // Inverse the affine
    final double[] d = new double[] { world_x, world_y };
    aff.inverseTransform(d, 0, d, 0, 1);
    // Inverse the coordinate transform
    if (null != ct) {
        final double[] f = new double[] { d[0], d[1] };
        final mpicbg.models.InvertibleCoordinateTransform t = mpicbg.models.InvertibleCoordinateTransform.class.isAssignableFrom(ct.getClass()) ? (mpicbg.models.InvertibleCoordinateTransform) ct : new mpicbg.trakem2.transform.TransformMesh(ct, meshResolution, o_width, o_height);
        try {
            t.applyInverseInPlace(f);
        } catch (final NoninvertibleModelException e) {
        }
        d[0] = f[0];
        d[1] = f[1];
    }
    return d;
}
Also used : NoninvertibleModelException(mpicbg.models.NoninvertibleModelException) TransformMesh(mpicbg.trakem2.transform.TransformMesh)

Aggregations

NoninvertibleModelException (mpicbg.models.NoninvertibleModelException)6 PointMatch (mpicbg.models.PointMatch)2 ImageProcessor (ij.process.ImageProcessor)1 AffineTransform (java.awt.geom.AffineTransform)1 Area (java.awt.geom.Area)1 NoninvertibleTransformException (java.awt.geom.NoninvertibleTransformException)1 PathIterator (java.awt.geom.PathIterator)1 Point2D (java.awt.geom.Point2D)1 AffineModel2D (mpicbg.models.AffineModel2D)1 CoordinateTransformMesh (mpicbg.models.CoordinateTransformMesh)1 IllDefinedDataPointsException (mpicbg.models.IllDefinedDataPointsException)1 NotEnoughDataPointsException (mpicbg.models.NotEnoughDataPointsException)1 Point (mpicbg.models.Point)1 SimilarityModel2D (mpicbg.models.SimilarityModel2D)1 CoordinateTransform (mpicbg.trakem2.transform.CoordinateTransform)1 CoordinateTransformList (mpicbg.trakem2.transform.CoordinateTransformList)1 TransformMesh (mpicbg.trakem2.transform.TransformMesh)1 TransformMeshMappingWithMasks (mpicbg.trakem2.transform.TransformMeshMappingWithMasks)1