Search in sources :

Example 1 with ImageProcessorWithMasks

use of mpicbg.trakem2.transform.TransformMeshMappingWithMasks.ImageProcessorWithMasks 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)

Example 2 with ImageProcessorWithMasks

use of mpicbg.trakem2.transform.TransformMeshMappingWithMasks.ImageProcessorWithMasks in project TrakEM2 by trakem2.

the class Render method render.

/**
 * Renders a patch, mapping its intensities [min, max] → [0, 1]
 *
 * @param patch the patch to be rendered
 * @param targetImage target pixels, specifies the target box
 * @param targetWeight target weight pixels, depending on alpha
 * @param x target box offset in world coordinates
 * @param y target box offset in world coordinates
 * @param scale target scale
 */
public static final void render(final Patch patch, final int coefficientsWidth, final int coefficientsHeight, final FloatProcessor targetImage, final FloatProcessor targetWeight, final ColorProcessor targetCoefficients, final double x, final double y, final double scale) {
    /* assemble coordinate transformations and add bounding box offset */
    final CoordinateTransformList<CoordinateTransform> ctl = new CoordinateTransformList<CoordinateTransform>();
    ctl.add(patch.getFullCoordinateTransform());
    final AffineModel2D affineScale = new AffineModel2D();
    affineScale.set(scale, 0, 0, scale, -x * scale, -y * scale);
    ctl.add(affineScale);
    /* estimate average scale and generate downsampled source */
    final int width = patch.getOWidth(), height = patch.getOHeight();
    final double s = sampleAverageScale(ctl, width, height, width / patch.getMeshResolution());
    final int mipmapLevel = bestMipmapLevel(s);
    final ImageProcessor ipMipmap = Downsampler.downsampleImageProcessor(patch.getImageProcessor(), mipmapLevel);
    /* create a target */
    final ImageProcessor tp = ipMipmap.createProcessor(targetImage.getWidth(), targetImage.getHeight());
    /* prepare and downsample alpha mask if there is one */
    final ByteProcessor bpMaskMipmap;
    final ByteProcessor bpMaskTarget;
    final ByteProcessor bpMask = patch.getAlphaMask();
    if (bpMask == null) {
        bpMaskMipmap = null;
        bpMaskTarget = null;
    } else {
        bpMaskMipmap = bpMask == null ? null : Downsampler.downsampleByteProcessor(bpMask, mipmapLevel);
        bpMaskTarget = new ByteProcessor(tp.getWidth(), tp.getHeight());
    }
    /* create coefficients map */
    final ColorProcessor cp = new ColorProcessor(ipMipmap.getWidth(), ipMipmap.getHeight());
    final int w = cp.getWidth();
    final int h = cp.getHeight();
    for (int yi = 0; yi < h; ++yi) {
        final int yc = yi * coefficientsHeight / h;
        final int ic = yc * coefficientsWidth;
        final int iyi = yi * w;
        for (int xi = 0; xi < w; ++xi) cp.set(iyi + xi, ic + (xi * coefficientsWidth / w) + 1);
    }
    /* attach mipmap transformation */
    final CoordinateTransformList<CoordinateTransform> ctlMipmap = new CoordinateTransformList<CoordinateTransform>();
    ctlMipmap.add(createScaleLevelTransform(mipmapLevel));
    ctlMipmap.add(ctl);
    /* create mesh */
    final CoordinateTransformMesh mesh = new CoordinateTransformMesh(ctlMipmap, patch.getMeshResolution(), ipMipmap.getWidth(), ipMipmap.getHeight());
    /* render */
    final ImageProcessorWithMasks source = new ImageProcessorWithMasks(ipMipmap, bpMaskMipmap, null);
    final ImageProcessorWithMasks target = new ImageProcessorWithMasks(tp, bpMaskTarget, null);
    final TransformMeshMappingWithMasks<TransformMesh> mapping = new TransformMeshMappingWithMasks<TransformMesh>(mesh);
    mapping.mapInterpolated(source, target, 1);
    final TransformMeshMapping<TransformMesh> coefficientsMapMapping = new TransformMeshMapping<TransformMesh>(mesh);
    coefficientsMapMapping.map(cp, targetCoefficients, 1);
    /* set alpha channel */
    final byte[] alphaPixels;
    if (bpMaskTarget != null)
        alphaPixels = (byte[]) bpMaskTarget.getPixels();
    else
        alphaPixels = (byte[]) target.outside.getPixels();
    /* convert */
    final double min = patch.getMin();
    final double max = patch.getMax();
    final double a = 1.0 / (max - min);
    final double b = 1.0 / 255.0;
    for (int i = 0; i < alphaPixels.length; ++i) targetImage.setf(i, (float) ((tp.getf(i) - min) * a));
    for (int i = 0; i < alphaPixels.length; ++i) targetWeight.setf(i, (float) ((alphaPixels[i] & 0xff) * b));
}
Also used : ByteProcessor(ij.process.ByteProcessor) TransformMeshMapping(mpicbg.ij.TransformMeshMapping) ImageProcessorWithMasks(mpicbg.trakem2.transform.TransformMeshMappingWithMasks.ImageProcessorWithMasks) CoordinateTransformList(mpicbg.models.CoordinateTransformList) Point(mpicbg.models.Point) ImageProcessor(ij.process.ImageProcessor) ColorProcessor(ij.process.ColorProcessor) CoordinateTransformMesh(mpicbg.models.CoordinateTransformMesh) AffineModel2D(mpicbg.models.AffineModel2D) TransformMeshMappingWithMasks(mpicbg.trakem2.transform.TransformMeshMappingWithMasks) CoordinateTransform(mpicbg.models.CoordinateTransform) TransformMesh(mpicbg.models.TransformMesh) CoordinateTransformMesh(mpicbg.models.CoordinateTransformMesh)

Aggregations

ImageProcessor (ij.process.ImageProcessor)2 CoordinateTransformMesh (mpicbg.models.CoordinateTransformMesh)2 ImageProcessorWithMasks (mpicbg.trakem2.transform.TransformMeshMappingWithMasks.ImageProcessorWithMasks)2 ByteProcessor (ij.process.ByteProcessor)1 ColorProcessor (ij.process.ColorProcessor)1 Rectangle (java.awt.Rectangle)1 TransformMeshMapping (mpicbg.ij.TransformMeshMapping)1 AffineModel2D (mpicbg.models.AffineModel2D)1 CoordinateTransform (mpicbg.models.CoordinateTransform)1 CoordinateTransformList (mpicbg.models.CoordinateTransformList)1 Point (mpicbg.models.Point)1 TransformMesh (mpicbg.models.TransformMesh)1 CoordinateTransform (mpicbg.trakem2.transform.CoordinateTransform)1 TransformMesh (mpicbg.trakem2.transform.TransformMesh)1 TransformMeshMapping (mpicbg.trakem2.transform.TransformMeshMapping)1 TransformMeshMappingWithMasks (mpicbg.trakem2.transform.TransformMeshMappingWithMasks)1