Search in sources :

Example 6 with TransformMesh

use of mpicbg.trakem2.transform.TransformMesh in project TrakEM2 by trakem2.

the class Patch method getFullCoordinateTransform.

/**
 * Create a {@link CoordinateTransform} that incorporates both the
 * {@link CoordinateTransform} of this {@link Patch} (if present) and its
 * {@link AffineTransform}.  The returned {@link CoordinateTransform} directly
 * transfers the {@link Patch} into world coordinates.  An image can be rendered
 * e.g. using {@link mpicbg.ij.TransformMeshMapping} with an
 * {@link mpicbg.models.TransformMesh}.  Note that you may prefer to use
 * {@link mpicbg.models.TransformMesh} which does not perform auto-boxing as
 * opposed to {@link TransformMesh} in the mpicbg.trakem2 package.
 *
 * @return
 */
public final CoordinateTransform getFullCoordinateTransform() {
    final CoordinateTransform ctp = getCoordinateTransform();
    if (ctp == null) {
        final AffineModel2D affine = new AffineModel2D();
        affine.set(at);
        return affine;
    } else {
        final Rectangle box = getCoordinateTransformBoundingBox();
        final AffineTransform at2 = new AffineTransform(at);
        at2.translate(-box.x, -box.y);
        final AffineModel2D affine = new AffineModel2D();
        affine.set(at2);
        final CoordinateTransformList<CoordinateTransform> ctl = new CoordinateTransformList<CoordinateTransform>();
        ctl.add(ctp);
        ctl.add(affine);
        return ctl;
    }
}
Also used : CoordinateTransformList(mpicbg.trakem2.transform.CoordinateTransformList) AffineModel2D(mpicbg.trakem2.transform.AffineModel2D) Rectangle(java.awt.Rectangle) AffineTransform(java.awt.geom.AffineTransform) CoordinateTransform(mpicbg.trakem2.transform.CoordinateTransform)

Example 7 with TransformMesh

use of mpicbg.trakem2.transform.TransformMesh in project TrakEM2 by trakem2.

the class Patch method setCoordinateTransform.

/**
 * Set a CoordinateTransform to this Patch.
 *  The resulting image of applying the coordinate transform does not need to be rectangular: an alpha mask will take care of the borders. You should call updateMipMaps() afterwards to update the mipmap images used for painting this Patch to the screen.
 */
public final void setCoordinateTransform(final CoordinateTransform ct) {
    if (isLinked()) {
        Utils.log("Cannot set coordinate transform: patch is linked!");
        return;
    }
    CoordinateTransform this_ct = hasCoordinateTransform() ? getCoordinateTransform() : null;
    if (null != this_ct) {
        // restore image without the transform
        final TransformMesh mesh = new TransformMesh(this_ct, meshResolution, o_width, o_height);
        final Rectangle box = mesh.getBoundingBox();
        this.at.translate(-box.x, -box.y);
        updateInDatabase("transform+dimensions");
    }
    try {
        setNewCoordinateTransform(ct);
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
    this_ct = ct;
    updateInDatabase("ict_transform");
    if (null == this_ct) {
        width = o_width;
        height = o_height;
        updateBucket();
        return;
    }
    // Adjust the AffineTransform to correct for bounding box displacement
    final TransformMesh mesh = new TransformMesh(this_ct, meshResolution, o_width, o_height);
    final Rectangle box = mesh.getBoundingBox();
    this.at.translate(box.x, box.y);
    width = box.width;
    height = box.height;
    // the AffineTransform
    updateInDatabase("transform+dimensions");
    updateBucket();
// Updating the mipmaps will call createTransformedImage below if ct is not null
/* DISABLED */
// updateMipMaps();
}
Also used : Rectangle(java.awt.Rectangle) CoordinateTransform(mpicbg.trakem2.transform.CoordinateTransform) NoninvertibleModelException(mpicbg.models.NoninvertibleModelException) NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) IOException(java.io.IOException) TransformMesh(mpicbg.trakem2.transform.TransformMesh) CoordinateTransformMesh(mpicbg.models.CoordinateTransformMesh)

Example 8 with TransformMesh

use of mpicbg.trakem2.transform.TransformMesh in project TrakEM2 by trakem2.

the class Patch method addAlphaMaskLocal.

/**
 * Add the given area, in local coordinates, to the alpha mask, using the given fill value.
 */
public void addAlphaMaskLocal(final Area aLocal, int value) {
    if (value < 0)
        value = 0;
    if (value > 255)
        value = 255;
    // 
    CoordinateTransform ct = null;
    if (hasCoordinateTransform() && null == (ct = getCT())) {
        return;
    }
    // When the area is larger than the image, sometimes the area fails to be set at all
    // Also, intersection accelerates calls to contains(x,y) for complex polygons
    final Area a = new Area(new Rectangle(0, 0, (int) (width + 1), (int) (height + 1)));
    a.intersect(aLocal);
    if (M.isEmpty(a)) {
        Utils.log("ROI does not intersect the active image!");
        return;
    }
    ByteProcessor mask = getAlphaMask();
    // Use imglib to bypass all the problems with ShapeROI
    // Create a Shape image with background and the Area on it with 'value'
    final int background = (null != mask && 255 == value) ? 0 : 255;
    final ShapeList<UnsignedByteType> shapeList = new ShapeList<UnsignedByteType>(new int[] { (int) width, (int) height, 1 }, new UnsignedByteType(background));
    shapeList.addShape(a, new UnsignedByteType(value), new int[] { 0 });
    final mpicbg.imglib.image.Image<UnsignedByteType> shapeListImage = new mpicbg.imglib.image.Image<UnsignedByteType>(shapeList, shapeList.getBackground(), "mask");
    ByteProcessor rmask = (ByteProcessor) ImageJFunctions.copyToImagePlus(shapeListImage, ImagePlus.GRAY8).getProcessor();
    if (hasCoordinateTransform()) {
        // inverse the coordinate transform
        final TransformMesh mesh = new TransformMesh(ct, meshResolution, o_width, o_height);
        final TransformMeshMapping mapping = new TransformMeshMapping(mesh);
        rmask = (ByteProcessor) mapping.createInverseMappedImageInterpolated(rmask);
    }
    if (null == mask) {
        // There wasn't a mask, hence just set it
        mask = rmask;
    } else {
        final byte[] b1 = (byte[]) mask.getPixels();
        final byte[] b2 = (byte[]) rmask.getPixels();
        // Whatever is not background in the new mask gets set on the old mask
        for (int i = 0; i < b1.length; i++) {
            // background pixel in new mask
            if (background == (b2[i] & 0xff))
                continue;
            // replace old pixel with new pixel
            b1[i] = b2[i];
        }
    }
    setAlphaMask(mask);
}
Also used : ByteProcessor(ij.process.ByteProcessor) TransformMeshMapping(mpicbg.trakem2.transform.TransformMeshMapping) UnsignedByteType(mpicbg.imglib.type.numeric.integer.UnsignedByteType) ShapeList(mpicbg.imglib.container.shapelist.ShapeList) Rectangle(java.awt.Rectangle) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) ExportBestFlatImage(mpicbg.trakem2.transform.ExportBestFlatImage) Area(java.awt.geom.Area) CoordinateTransform(mpicbg.trakem2.transform.CoordinateTransform) TransformMesh(mpicbg.trakem2.transform.TransformMesh) CoordinateTransformMesh(mpicbg.models.CoordinateTransformMesh)

Example 9 with TransformMesh

use of mpicbg.trakem2.transform.TransformMesh in project TrakEM2 by trakem2.

the class Patch method getLocalAffine.

/**
 * Return the local affine transformation for a passed location in world
 * coordinates.   This affine transform is either the global affine
 * transform of the patch or the combined affine transform of the local
 * affine transform in the transform mesh and its global affine transform.
 *
 * @param wx
 * @param wy
 * @return
 */
public AffineTransform getLocalAffine(final double wx, final double wy) {
    final AffineTransform affine = new AffineTransform(at);
    if (hasCoordinateTransform()) {
        final CoordinateTransform ct = getCoordinateTransform();
        final double[] w = new double[] { wx, wy };
        try {
            at.inverseTransform(w, 0, w, 0, 1);
        } catch (final NoninvertibleTransformException e) {
        }
        final TransformMesh mesh = new TransformMesh(ct, meshResolution, o_width, o_height);
        final mpicbg.models.AffineModel2D triangle = mesh.closestTargetAffine(new double[] { w[0], w[1] });
        affine.concatenate(triangle.createAffine());
    }
    return affine;
}
Also used : NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) AffineTransform(java.awt.geom.AffineTransform) CoordinateTransform(mpicbg.trakem2.transform.CoordinateTransform) TransformMesh(mpicbg.trakem2.transform.TransformMesh) CoordinateTransformMesh(mpicbg.models.CoordinateTransformMesh)

Example 10 with TransformMesh

use of mpicbg.trakem2.transform.TransformMesh in project TrakEM2 by trakem2.

the class Patch method createCoordinateTransformedImage.

public final Patch.PatchImage createCoordinateTransformedImage() {
    if (!hasCoordinateTransform())
        return null;
    final CoordinateTransform ct = getCoordinateTransform();
    final ImageProcessor source = getImageProcessor();
    // some error occurred
    if (null == source)
        return null;
    // Utils.log2("source image dimensions: " + source.getWidth() + ", " + source.getHeight());
    final TransformMesh mesh = new TransformMesh(ct, meshResolution, o_width, o_height);
    final Rectangle box = mesh.getBoundingBox();
    /* We can calculate the exact size of the image to be rendered, so let's do it */
    // project.getLoader().releaseToFit(o_width, o_height, type, 5);
    final long b = // outside and mask source
    2 * o_width * o_height + // outside and mask target
    2 * box.width * box.height + // image source
    5 * o_width * o_height + // image target
    5 * box.width * box.height;
    project.getLoader().releaseToFit(b);
    final TransformMeshMapping mapping = new TransformMeshMapping(mesh);
    final ImageProcessorWithMasks target = mapping.createMappedMaskedImageInterpolated(source, getAlphaMask());
    // Set the LUT
    target.ip.setColorModel(source.getColorModel());
    return new PatchImage(target.ip, (ByteProcessor) target.mask, target.outside, box, true);
}
Also used : ImageProcessor(ij.process.ImageProcessor) TransformMeshMapping(mpicbg.trakem2.transform.TransformMeshMapping) ImageProcessorWithMasks(mpicbg.trakem2.transform.TransformMeshMappingWithMasks.ImageProcessorWithMasks) Rectangle(java.awt.Rectangle) CoordinateTransform(mpicbg.trakem2.transform.CoordinateTransform) TransformMesh(mpicbg.trakem2.transform.TransformMesh) CoordinateTransformMesh(mpicbg.models.CoordinateTransformMesh)

Aggregations

TransformMesh (mpicbg.trakem2.transform.TransformMesh)9 CoordinateTransformMesh (mpicbg.models.CoordinateTransformMesh)8 CoordinateTransform (mpicbg.trakem2.transform.CoordinateTransform)8 Rectangle (java.awt.Rectangle)6 ImageProcessor (ij.process.ImageProcessor)4 AffineTransform (java.awt.geom.AffineTransform)4 NoninvertibleTransformException (java.awt.geom.NoninvertibleTransformException)4 NoninvertibleModelException (mpicbg.models.NoninvertibleModelException)4 CoordinateTransformList (mpicbg.trakem2.transform.CoordinateTransformList)4 TransformMeshMapping (mpicbg.trakem2.transform.TransformMeshMapping)4 ByteProcessor (ij.process.ByteProcessor)3 Area (java.awt.geom.Area)3 AffineModel2D (mpicbg.trakem2.transform.AffineModel2D)3 ImagePlus (ij.ImagePlus)2 Roi (ij.gui.Roi)2 ShapeRoi (ij.gui.ShapeRoi)2 Patch (ini.trakem2.display.Patch)2 Point (mpicbg.models.Point)2 ImageProcessorWithMasks (mpicbg.trakem2.transform.TransformMeshMappingWithMasks.ImageProcessorWithMasks)2 ThresholdToSelection (ij.plugin.filter.ThresholdToSelection)1