Search in sources :

Example 1 with AffineModel3D

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

the class MovingLeastSquaresTransform2 method init.

@Override
public final void init(final String data) throws NumberFormatException {
    final String[] fields = data.split("\\s+");
    if (fields.length > 3) {
        final int n = Integer.parseInt(fields[1]);
        if ((fields.length - 3) % (2 * n + 1) == 0) {
            final int l = (fields.length - 3) / (2 * n + 1);
            if (n == 2) {
                if (fields[0].equals("translation"))
                    model = new TranslationModel2D();
                else if (fields[0].equals("rigid"))
                    model = new RigidModel2D();
                else if (fields[0].equals("similarity"))
                    model = new SimilarityModel2D();
                else if (fields[0].equals("affine"))
                    model = new AffineModel2D();
                else
                    throw new NumberFormatException("Inappropriate parameters for " + this.getClass().getCanonicalName());
            } else if (n == 3) {
                if (fields[0].equals("affine"))
                    model = new AffineModel3D();
                else
                    throw new NumberFormatException("Inappropriate parameters for " + this.getClass().getCanonicalName());
            } else
                throw new NumberFormatException("Inappropriate parameters for " + this.getClass().getCanonicalName());
            alpha = Float.parseFloat(fields[2]);
            p = new float[n][l];
            q = new float[n][l];
            w = new float[l];
            int i = 2, j = 0;
            while (i < fields.length - 1) {
                for (int d = 0; d < n; ++d) p[d][j] = Float.parseFloat(fields[++i]);
                for (int d = 0; d < n; ++d) q[d][j] = Float.parseFloat(fields[++i]);
                w[j] = Float.parseFloat(fields[++i]);
                ++j;
            }
        } else
            throw new NumberFormatException("Inappropriate parameters for " + this.getClass().getCanonicalName());
    } else
        throw new NumberFormatException("Inappropriate parameters for " + this.getClass().getCanonicalName());
}
Also used : RigidModel2D(mpicbg.models.RigidModel2D) AffineModel2D(mpicbg.models.AffineModel2D) TranslationModel2D(mpicbg.models.TranslationModel2D) SimilarityModel2D(mpicbg.models.SimilarityModel2D) AffineModel3D(mpicbg.models.AffineModel3D)

Example 2 with AffineModel3D

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

the class MovingLeastSquaresTransform method init.

@Override
public final void init(final String data) throws NumberFormatException {
    matches.clear();
    final String[] fields = data.split("\\s+");
    if (fields.length > 3) {
        final int d = Integer.parseInt(fields[1]);
        if ((fields.length - 3) % (2 * d + 1) == 0) {
            if (d == 2) {
                if (fields[0].equals("translation"))
                    model = new TranslationModel2D();
                else if (fields[0].equals("rigid"))
                    model = new RigidModel2D();
                else if (fields[0].equals("similarity"))
                    model = new SimilarityModel2D();
                else if (fields[0].equals("affine"))
                    model = new AffineModel2D();
                else
                    throw new NumberFormatException("Inappropriate parameters for " + this.getClass().getCanonicalName());
            } else if (d == 3) {
                if (fields[0].equals("affine"))
                    model = new AffineModel3D();
                else
                    throw new NumberFormatException("Inappropriate parameters for " + this.getClass().getCanonicalName());
            } else
                throw new NumberFormatException("Inappropriate parameters for " + this.getClass().getCanonicalName());
            alpha = Double.parseDouble(fields[2]);
            int i = 2;
            while (i < fields.length - 1) {
                final double[] p1 = new double[d];
                for (int k = 0; k < d; ++k) p1[k] = Double.parseDouble(fields[++i]);
                final double[] p2 = new double[d];
                for (int k = 0; k < d; ++k) p2[k] = Double.parseDouble(fields[++i]);
                final double weight = Double.parseDouble(fields[++i]);
                final PointMatch m = new PointMatch(new Point(p1), new Point(p2), weight);
                matches.add(m);
            }
        } else
            throw new NumberFormatException("Inappropriate parameters for " + this.getClass().getCanonicalName());
    } else
        throw new NumberFormatException("Inappropriate parameters for " + this.getClass().getCanonicalName());
}
Also used : Point(mpicbg.models.Point) Point(mpicbg.models.Point) PointMatch(mpicbg.models.PointMatch) RigidModel2D(mpicbg.models.RigidModel2D) AffineModel2D(mpicbg.models.AffineModel2D) TranslationModel2D(mpicbg.models.TranslationModel2D) SimilarityModel2D(mpicbg.models.SimilarityModel2D) AffineModel3D(mpicbg.models.AffineModel3D)

Example 3 with AffineModel3D

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

the class MovingLeastSquaresTransform2 method init2.

public void init2(final String s) throws Exception {
    // WARNING: assumes all whitespace is single
    final int len = s.length();
    int i = 0;
    // Advance to the first white space
    while (' ' != s.charAt(++i)) {
    }
    // Interpret model by the last letter of the name
    final char modelLastChar = s.charAt(i - 1);
    // Determine dimension 2 or 3
    final int n = (s.charAt(i + 1)) - 48;
    switch(n) {
        case 3:
            model = new AffineModel3D();
            break;
        case 2:
            switch(modelLastChar) {
                case // translation
                'n':
                    model = new TranslationModel2D();
                    break;
                case // rigid
                'd':
                    model = new RigidModel2D();
                    break;
                case // similarity
                'y':
                    model = new SimilarityModel2D();
                    break;
                case // affine
                'e':
                    model = new AffineModel2D();
                    break;
                default:
                    throw new Exception("Unknown model " + s.substring(0, i));
            }
            break;
        default:
            throw new NumberFormatException("Unsupported model dimensions: " + n + " for " + this.getClass().getCanonicalName());
    }
    // 'i' is at whitespace before n
    // Move i to whitespace before alpha
    i += 2;
    // Mark last char before whitespace
    int cut = i - 1;
    // 'i' ends up at the whitespace after alpha
    while (' ' != s.charAt(++i)) {
    }
    // Parse alpha
    float[] f = new float[1];
    parse(s, cut, i - 1, f, 0);
    this.alpha = f[0];
    // Count numbers by counting one whitespace before each number
    int nVals = 0;
    for (int k = i; k < len; ++k) {
        if (' ' == s.charAt(k))
            ++nVals;
    }
    // The size of a unit of numbers
    final int cell = n + n + 1;
    // Detect inconsistency:
    if (0 != nVals % cell) {
        throw new NumberFormatException("Inappropriate parameters for " + this.getClass().getCanonicalName());
    }
    // Create arrays
    this.p = new float[n][nVals / cell];
    this.q = new float[n][this.p[0].length];
    this.w = new float[this.p[0].length];
    // Mark the whitespace char before the first number
    cut = i - 1;
    // Start parsing from the end
    i = len - 1;
    int count = 0;
    if (2 == n) {
        while (i > cut) {
            // Determine which array from {p,q,w} and which position in the array, using n and count:
            switch(// n for dimensions, +1 for the weight
            count % cell) {
                case 0:
                    f = this.w;
                    break;
                case 1:
                    f = this.q[1];
                    break;
                case 2:
                    f = this.q[0];
                    break;
                case 3:
                    f = this.p[1];
                    break;
                case 4:
                    f = this.p[0];
                    break;
            }
            i = parse(s, cut, i, f, this.w.length - (count / cell) - 1);
            ++count;
        }
    } else {
        while (i > cut) {
            // Determine which array from {p,q,w} and which position in the array, using n and count:
            switch(// n for dimensions, +1 for the weight
            count % (n + n + 1)) {
                case 0:
                    f = this.w;
                    break;
                case 1:
                    f = this.q[2];
                    break;
                case 2:
                    f = this.q[1];
                    break;
                case 3:
                    f = this.q[0];
                    break;
                case 4:
                    f = this.p[2];
                    break;
                case 5:
                    f = this.p[1];
                    break;
                case 6:
                    f = this.p[0];
                    break;
            }
            i = parse(s, cut, i, f, this.w.length - (count / cell) - 1);
            ++count;
        }
    }
}
Also used : RigidModel2D(mpicbg.models.RigidModel2D) AffineModel2D(mpicbg.models.AffineModel2D) TranslationModel2D(mpicbg.models.TranslationModel2D) SimilarityModel2D(mpicbg.models.SimilarityModel2D) NotEnoughDataPointsException(mpicbg.models.NotEnoughDataPointsException) IllDefinedDataPointsException(mpicbg.models.IllDefinedDataPointsException) AffineModel3D(mpicbg.models.AffineModel3D)

Example 4 with AffineModel3D

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

the class Stack method fetchFutureImage.

private final Future<Image> fetchFutureImage(final Long imageId, final double magnification, final Layer active_layer, final boolean trigger_repaint_event) {
    synchronized (futureImages) {
        Future<Image> fu = futureImages.get(imageId);
        if (null == fu) {
            fu = project.getLoader().doLater(new Callable<Image>() {

                @Override
                public Image call() {
                    final InvertibleCoordinateTransformList<mpicbg.models.InvertibleCoordinateTransform> ictl = new InvertibleCoordinateTransformList<mpicbg.models.InvertibleCoordinateTransform>();
                    if (ict != null) {
                        // Utils.log2( "ictScale of " + getTitle() + " is " + ictScale );
                        ictl.add(ict);
                        /* Remove boundingBox shift ict ... */
                        final TranslationModel3D unShiftBounds = new TranslationModel3D();
                        unShiftBounds.set(-boundsMin[0], -boundsMin[1], 0);
                        ictl.add(unShiftBounds);
                        if (ictScale != 1.0) {
                            final AffineModel3D unScaleXY = new AffineModel3D();
                            unScaleXY.set(1.0 / ictScale, 0, 0, 0, 0, 1.0 / ictScale, 0, 0, 0, 0, 1.0, 0);
                            ictl.add(unScaleXY);
                        }
                    }
                    /* TODO remove that scale from ict and put it into atp */
                    final ImagePlus imp = project.getLoader().fetchImagePlus(Stack.this);
                    final ImageProcessor ip = imp.getStack().getProcessor(1).createProcessor((int) Math.ceil((boundsMax[0] - boundsMin[0]) / ictScale), (int) Math.ceil((boundsMax[1] - boundsMin[1]) / ictScale));
                    // Utils.log2( "ictScale is " + ictScale );
                    // Utils.log2( "rendering an image of " + ip.getWidth() + " x " + ip.getHeight() + " px" );
                    final double currentZ = active_layer.getZ();
                    final TranslationModel3D sliceShift = new TranslationModel3D();
                    sliceShift.set(0, 0, -currentZ);
                    ictl.add(sliceShift);
                    /* optimization: if ict is affine, reduce ictl into a single affine */
                    final InverseTransformMapping<mpicbg.models.InvertibleCoordinateTransform> mapping;
                    if (AffineModel3D.class.isInstance(ict)) {
                        final AffineModel3D ictAffine = new AffineModel3D();
                        boolean isAffine = true;
                        for (final mpicbg.models.InvertibleCoordinateTransform t : ictl.getList(null)) {
                            if (AffineModel3D.class.isInstance(t))
                                ictAffine.preConcatenate((AffineModel3D) t);
                            else if (TranslationModel3D.class.isInstance(t))
                                ictAffine.preConcatenate((TranslationModel3D) t);
                            else {
                                isAffine = false;
                                break;
                            }
                        }
                        if (isAffine)
                            mapping = new InverseTransformMapping<mpicbg.models.InvertibleCoordinateTransform>(ictAffine);
                        else
                            mapping = new InverseTransformMapping<mpicbg.models.InvertibleCoordinateTransform>(ictl);
                    } else
                        mapping = new InverseTransformMapping<mpicbg.models.InvertibleCoordinateTransform>(ictl);
                    mapping.mapInterpolated(imp.getStack(), ip);
                    // wast: atp
                    final double s = estimateAffineScale(new AffineTransform(at));
                    final double smoothMag = magnification * s * ictScale;
                    if (smoothMag < 1.0f) {
                        Filter.smoothForScale(ip, smoothMag, 0.5f, 0.5f);
                    }
                    final Image image = ip.createImage();
                    if (null == image) {
                        Utils.log2("Stack.paint: null image, returning");
                        // TEMPORARY from lazy
                        return null;
                    // repaints after closing a
                    // Project
                    }
                    project.getLoader().cacheAWT(imageId, image);
                    synchronized (futureImages) {
                        futureImages.remove(imageId);
                    }
                    if (trigger_repaint_event) {
                        // Display.repaint( active_layer, Stack.this );
                        Display.repaint(active_layer);
                    }
                    return image;
                }
            });
        }
        // else {
        // Utils.log2( "fu is not null" );
        // // We don't do anything: we wait for itself to launch a
        // repaint event
        // }
        futureImages.put(imageId, fu);
        return fu;
    }
}
Also used : Image(java.awt.Image) ImagePlus(ij.ImagePlus) Callable(java.util.concurrent.Callable) InvertibleCoordinateTransformList(mpicbg.models.InvertibleCoordinateTransformList) ImageProcessor(ij.process.ImageProcessor) InvertibleCoordinateTransform(mpicbg.trakem2.transform.InvertibleCoordinateTransform) TranslationModel3D(mpicbg.models.TranslationModel3D) AffineTransform(java.awt.geom.AffineTransform) AffineModel3D(mpicbg.models.AffineModel3D) InverseTransformMapping(mpicbg.ij.stack.InverseTransformMapping)

Aggregations

AffineModel3D (mpicbg.models.AffineModel3D)4 AffineModel2D (mpicbg.models.AffineModel2D)3 RigidModel2D (mpicbg.models.RigidModel2D)3 SimilarityModel2D (mpicbg.models.SimilarityModel2D)3 TranslationModel2D (mpicbg.models.TranslationModel2D)3 ImagePlus (ij.ImagePlus)1 ImageProcessor (ij.process.ImageProcessor)1 Image (java.awt.Image)1 AffineTransform (java.awt.geom.AffineTransform)1 Callable (java.util.concurrent.Callable)1 InverseTransformMapping (mpicbg.ij.stack.InverseTransformMapping)1 IllDefinedDataPointsException (mpicbg.models.IllDefinedDataPointsException)1 InvertibleCoordinateTransformList (mpicbg.models.InvertibleCoordinateTransformList)1 NotEnoughDataPointsException (mpicbg.models.NotEnoughDataPointsException)1 Point (mpicbg.models.Point)1 PointMatch (mpicbg.models.PointMatch)1 TranslationModel3D (mpicbg.models.TranslationModel3D)1 InvertibleCoordinateTransform (mpicbg.trakem2.transform.InvertibleCoordinateTransform)1