Search in sources :

Example 1 with CoordinateTransform

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

the class ExportUnsignedShort method map.

protected static final void map(final PatchTransform pt, final double x, final double y, final double scale, final ShortProcessor mappedIntensities, final ShortProcessor target, final ByteProcessor alphaTarget) {
    final TranslationModel2D t = new TranslationModel2D();
    t.set(-x, -y);
    final CoordinateTransformList<CoordinateTransform> ctl = new CoordinateTransformList<CoordinateTransform>();
    ctl.add(pt.ct);
    if (!Double.isNaN(scale)) {
        final AffineModel2D s = new AffineModel2D();
        s.set(scale, 0, 0, scale, 0, 0);
        ctl.add(s);
    }
    ctl.add(t);
    final CoordinateTransformMesh mesh = new CoordinateTransformMesh(ctl, pt.pir.patch.getMeshResolution(), pt.pir.patch.getOWidth(), pt.pir.patch.getOHeight());
    final TransformMeshMappingWithMasks<CoordinateTransformMesh> mapping = new TransformMeshMappingWithMasks<CoordinateTransformMesh>(mesh);
    ByteProcessor alpha = null;
    mappedIntensities.setInterpolationMethod(ImageProcessor.BILINEAR);
    if (pt.pir.patch.hasAlphaMask()) {
        alpha = pt.pir.patch.getAlphaMask();
        alpha.setInterpolationMethod(ImageProcessor.BILINEAR);
        mapping.map(mappedIntensities, alpha, target);
    } else {
        mapping.mapInterpolated(mappedIntensities, target);
    }
    // If alphaTarget is present, repeat the mapping but just for the alpha channel
    if (null != alphaTarget) {
        if (null == alpha) {
            // Simulate full alpha: no transparency
            alpha = new ByteProcessor(pt.pir.patch.getOWidth(), pt.pir.patch.getOHeight());
            alpha.setInterpolationMethod(ImageProcessor.BILINEAR);
            final byte[] as = (byte[]) alpha.getPixels();
            for (int i = 0; i < as.length; ++i) {
                as[i] = (byte) 255;
            }
        }
        mapping.mapInterpolated(alpha, alphaTarget);
    }
}
Also used : ByteProcessor(ij.process.ByteProcessor) CoordinateTransformMesh(mpicbg.models.CoordinateTransformMesh) CoordinateTransformList(mpicbg.models.CoordinateTransformList) TranslationModel2D(mpicbg.models.TranslationModel2D) CoordinateTransform(mpicbg.models.CoordinateTransform)

Example 2 with CoordinateTransform

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

the class Render method render.

/**
 * Renders a patch, mapping its intensities [min, max] &rarr; [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

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