use of mpicbg.models.AffineModel2D in project TrakEM2 by trakem2.
the class Render method createScaleLevelTransform.
/**
* Create an affine transformation that compensates for both scale and
* pixel shift of a mipmap level that was generated by top-left pixel
* averaging.
*
* @param scaleLevel
* @return
*/
protected static final AffineModel2D createScaleLevelTransform(final int scaleLevel) {
final AffineModel2D a = new AffineModel2D();
final int scale = 1 << scaleLevel;
final double t = (scale - 1) * 0.5;
a.set(scale, 0, 0, scale, t, t);
return a;
}
use of mpicbg.models.AffineModel2D in project TrakEM2 by trakem2.
the class TransformMesh method closestSourceAffine.
/**
* Return the {@link AffineModel2D} used by the triangle enclosing or
* closest to the passed target location.
*
* @param location
* @return
*/
public AffineModel2D closestSourceAffine(final double[] location) {
assert location.length == 2 : "2d transform meshs can be applied to 2d points only.";
final Set<AffineModel2D> s = av.keySet();
for (final AffineModel2D ai : s) {
final ArrayList<PointMatch> pm = av.get(ai);
if (isInSourcePolygon(pm, location))
return ai;
}
/* not in the mesh, find the closest affine */
double dMin = Double.MAX_VALUE;
AffineModel2D closestAffine = new AffineModel2D();
final double x = location[0];
final double y = location[1];
for (final AffineModel2D ai : s) {
final ArrayList<PointMatch> pm = av.get(ai);
double d = 0;
for (final PointMatch p : pm) {
final double[] l = p.getP1().getL();
final double dx = l[0] - x;
final double dy = l[1] - y;
d += Math.sqrt(dx * dx + dy * dy);
}
if (d < dMin) {
dMin = d;
closestAffine = ai;
}
}
return closestAffine;
}
use of mpicbg.models.AffineModel2D in project TrakEM2 by trakem2.
the class TransformMesh method closestTargetAffine.
/**
* Return the {@link AffineModel2D} used by the triangle enclosing or
* closest to the passed target location.
*
* @param location
* @return
*/
public AffineModel2D closestTargetAffine(final double[] location) {
assert location.length == 2 : "2d transform meshs can be applied to 2d points only.";
final Set<AffineModel2D> s = av.keySet();
for (final AffineModel2D ai : s) {
final ArrayList<PointMatch> pm = av.get(ai);
if (isInConvexTargetPolygon(pm, location))
return ai;
}
/* not in the mesh, find the closest affine */
double dMin = Double.MAX_VALUE;
AffineModel2D closestAffine = new AffineModel2D();
final double x = location[0];
final double y = location[1];
for (final AffineModel2D ai : s) {
final ArrayList<PointMatch> pm = av.get(ai);
double d = 0;
for (final PointMatch p : pm) {
final double[] w = p.getP2().getW();
final double dx = w[0] - x;
final double dy = w[1] - y;
d += Math.sqrt(dx * dx + dy * dy);
}
if (d < dMin) {
dMin = d;
closestAffine = ai;
}
}
return closestAffine;
}
use of mpicbg.models.AffineModel2D in project TrakEM2 by trakem2.
the class TransformMeshMappingWithMasks method map.
public final void map(final ImageProcessorWithMasks source, final ImageProcessorWithMasks target, final int numThreads) {
target.outside = new ByteProcessor(target.getWidth(), target.getHeight());
final HashMap<AffineModel2D, ArrayList<PointMatch>> av = transform.getAV();
if (numThreads > 1) {
final ArrayList<AffineModel2D> triangles = new ArrayList<AffineModel2D>(av.keySet());
final AtomicInteger i = new AtomicInteger(0);
final ArrayList<Thread> threads = new ArrayList<Thread>(numThreads);
for (int k = 0; k < numThreads; ++k) {
final Thread mtt = new MapTriangleThread(i, triangles, transform, source, target);
threads.add(mtt);
mtt.start();
}
for (final Thread mtt : threads) {
try {
mtt.join();
} catch (final InterruptedException e) {
}
}
} else if (source.mask == null) {
for (final AffineModel2D triangle : av.keySet()) {
mapTriangle(transform, triangle, source.ip, target.ip, target.outside);
}
} else {
for (final AffineModel2D triangle : av.keySet()) {
mapTriangle(transform, triangle, source.ip, source.mask, target.ip, target.mask, target.outside);
}
}
}
use of mpicbg.models.AffineModel2D in project TrakEM2 by trakem2.
the class TransformMeshMappingWithMasks method mapInterpolated.
public final void mapInterpolated(final ImageProcessorWithMasks source, final ImageProcessorWithMasks target, final int numThreads) {
target.outside = new ByteProcessor(target.getWidth(), target.getHeight());
source.ip.setInterpolationMethod(ImageProcessor.BILINEAR);
if (source.mask != null) {
source.mask.setInterpolationMethod(ImageProcessor.BILINEAR);
}
final HashMap<AffineModel2D, ArrayList<PointMatch>> av = transform.getAV();
if (numThreads > 1) {
final ArrayList<AffineModel2D> triangles = new ArrayList<AffineModel2D>(av.keySet());
final AtomicInteger i = new AtomicInteger(0);
final ArrayList<Thread> threads = new ArrayList<Thread>(numThreads);
for (int k = 0; k < numThreads; ++k) {
final Thread mtt = new MapTriangleInterpolatedThread(i, triangles, transform, source, target);
threads.add(mtt);
mtt.start();
}
for (final Thread mtt : threads) {
try {
mtt.join();
} catch (final InterruptedException e) {
}
}
} else if (source.mask == null) {
for (final AffineModel2D triangle : av.keySet()) {
mapTriangleInterpolated(transform, triangle, source.ip, target.ip, target.outside);
}
} else {
for (final AffineModel2D triangle : av.keySet()) {
mapTriangleInterpolated(transform, triangle, source.ip, source.mask, target.ip, target.mask, target.outside);
}
}
}
Aggregations