use of mpicbg.models.CoordinateTransformMesh in project TrakEM2 by trakem2.
the class ExportUnsignedByte method makeFlatImage.
public static final Pair<ByteProcessor, ByteProcessor> makeFlatImage(final List<Patch> patches, final Rectangle roi, final double backgroundValue, final double scale, final ImageSource fetcher) {
final ByteProcessor target = new ByteProcessor((int) (roi.width * scale), (int) (roi.height * scale));
target.setInterpolationMethod(ImageProcessor.BILINEAR);
final ByteProcessor targetMask = new ByteProcessor(target.getWidth(), target.getHeight());
targetMask.setInterpolationMethod(ImageProcessor.NEAREST_NEIGHBOR);
for (final Patch patch : patches) {
final ImageData imgd = fetcher.fetch(patch, scale);
// The affine to apply to the MipMap.image
final AffineTransform atc = new AffineTransform();
atc.scale(scale, scale);
atc.translate(-roi.x, -roi.y);
final AffineTransform at = new AffineTransform();
at.preConcatenate(atc);
at.concatenate(patch.getAffineTransform());
at.scale(imgd.scaleX, imgd.scaleY);
final AffineModel2D aff = new AffineModel2D();
aff.set(at);
final CoordinateTransformMesh mesh = new CoordinateTransformMesh(aff, patch.getMeshResolution(), imgd.bp.getWidth(), imgd.bp.getHeight());
final TransformMeshMappingWithMasks<CoordinateTransformMesh> mapping = new TransformMeshMappingWithMasks<CoordinateTransformMesh>(mesh);
imgd.bp.setInterpolationMethod(ImageProcessor.BILINEAR);
// no interpolation
imgd.alpha.setInterpolationMethod(ImageProcessor.NEAREST_NEIGHBOR);
mapping.map(imgd.bp, imgd.alpha, target, targetMask);
}
return new Pair<ByteProcessor, ByteProcessor>(target, targetMask);
}
use of mpicbg.models.CoordinateTransformMesh 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);
}
}
use of mpicbg.models.CoordinateTransformMesh 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));
}
Aggregations