Search in sources :

Example 16 with AffineModel2D

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

the class TransformMeshMappingWithMasks method map.

/**
 * Render source into master using alpha composition.
 * Interpolation is specified by the interpolation methods
 * set in source and alpha.
 *
 * Mapping for color images with alpha composition.
 *
 * @param source
 * @param alpha
 * @param target
 * @param targetMask
 * @param numThreads
 */
public final void map(final ColorProcessor source, final ByteProcessor alpha, final ColorProcessor target, final ByteProcessor targetMask, final int numThreads) {
    final List<AffineModel2D> l = new ArrayList<AffineModel2D>();
    l.addAll(transform.getAV().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 MapColorAlphaTriangleThread(i, l, transform, source, alpha, target, targetMask);
        threads.add(mtt);
        mtt.start();
    }
    for (final Thread mtt : threads) {
        try {
            mtt.join();
        } catch (final InterruptedException e) {
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AffineModel2D(mpicbg.models.AffineModel2D) ArrayList(java.util.ArrayList)

Example 17 with AffineModel2D

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

the class TransformMeshMappingWithMasks method map.

/**
 * Render source into target using alpha composition.
 * Interpolation is specified by the interpolation methods
 * set in source and alpha.
 *
 * @param source
 * @param alpha
 * @param target
 * @param numThreads
 */
public final void map(final ByteProcessor source, final ByteProcessor alpha, final ByteProcessor target, final ByteProcessor targetMask, final int numThreads) {
    final List<AffineModel2D> l = new ArrayList<AffineModel2D>();
    l.addAll(transform.getAV().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 MapByteAlphaTriangleThread(i, l, transform, source, alpha, target, targetMask);
        threads.add(mtt);
        mtt.start();
    }
    for (final Thread mtt : threads) {
        try {
            mtt.join();
        } catch (final InterruptedException e) {
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AffineModel2D(mpicbg.models.AffineModel2D) ArrayList(java.util.ArrayList)

Example 18 with AffineModel2D

use of mpicbg.models.AffineModel2D 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 19 with AffineModel2D

use of mpicbg.models.AffineModel2D 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 20 with AffineModel2D

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

the class Distortion_Correction method extractSIFTPointsThreaded.

protected static void extractSIFTPointsThreaded(final int index, final List<Feature>[] siftFeatures, final List<PointMatch>[] inliers, final AbstractAffineModel2D<?>[] models) {
    // save all matching candidates
    final List<PointMatch>[] candidates = new List[siftFeatures.length - 1];
    final Thread[] threads = MultiThreading.newThreads();
    // start at second
    final AtomicInteger ai = new AtomicInteger(0);
    for (int ithread = 0; ithread < threads.length; ++ithread) {
        threads[ithread] = new Thread() {

            @Override
            public void run() {
                setPriority(Thread.NORM_PRIORITY);
                for (int j = ai.getAndIncrement(); j < candidates.length; j = ai.getAndIncrement()) {
                    final int i = (j < index ? j : j + 1);
                    candidates[j] = FloatArray2DSIFT.createMatches(siftFeatures[index], siftFeatures[i], 1.5f, null, Float.MAX_VALUE, 0.5f);
                }
            }
        };
    }
    MultiThreading.startAndJoin(threads);
    // get rid of the outliers and save the rigid transformations to match
    // the inliers
    final AtomicInteger ai2 = new AtomicInteger(0);
    for (int ithread = 0; ithread < threads.length; ++ithread) {
        threads[ithread] = new Thread() {

            @Override
            public void run() {
                setPriority(Thread.NORM_PRIORITY);
                for (int i = ai2.getAndIncrement(); i < candidates.length; i = ai2.getAndIncrement()) {
                    final List<PointMatch> tmpInliers = new ArrayList<PointMatch>();
                    // RigidModel2D m =
                    // RigidModel2D.estimateBestModel(candidates.get(i),
                    // tmpInliers, sp.min_epsilon, sp.max_epsilon,
                    // sp.min_inlier_ratio);
                    final AbstractAffineModel2D<?> m;
                    switch(sp.expectedModelIndex) {
                        case 0:
                            m = new TranslationModel2D();
                            break;
                        case 1:
                            m = new RigidModel2D();
                            break;
                        case 2:
                            m = new SimilarityModel2D();
                            break;
                        case 3:
                            m = new AffineModel2D();
                            break;
                        default:
                            return;
                    }
                    boolean modelFound = false;
                    try {
                        modelFound = m.filterRansac(candidates[i], tmpInliers, 1000, sp.maxEpsilon, sp.minInlierRatio, 10);
                    } catch (final NotEnoughDataPointsException e) {
                        modelFound = false;
                    }
                    if (modelFound)
                        IJ.log("Model found:\n  " + candidates[i].size() + " candidates\n  " + tmpInliers.size() + " inliers\n  " + String.format("%.2f", m.getCost()) + "px average displacement");
                    else
                        IJ.log("No Model found.");
                    inliers[index * (sp.numberOfImages - 1) + i] = tmpInliers;
                    models[index * (sp.numberOfImages - 1) + i] = m;
                // System.out.println("**** MODEL ADDED: " +
                // (index*(sp.numberOfImages-1)+i));
                }
            }
        };
    }
    MultiThreading.startAndJoin(threads);
}
Also used : NotEnoughDataPointsException(mpicbg.models.NotEnoughDataPointsException) AbstractAffineModel2D(mpicbg.models.AbstractAffineModel2D) PointMatch(mpicbg.models.PointMatch) RigidModel2D(mpicbg.models.RigidModel2D) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractAffineModel2D(mpicbg.models.AbstractAffineModel2D) AffineModel2D(mpicbg.models.AffineModel2D) ArrayList(java.util.ArrayList) List(java.util.List) TranslationModel2D(mpicbg.models.TranslationModel2D) SimilarityModel2D(mpicbg.models.SimilarityModel2D)

Aggregations

AffineModel2D (mpicbg.models.AffineModel2D)27 ArrayList (java.util.ArrayList)18 PointMatch (mpicbg.models.PointMatch)15 SimilarityModel2D (mpicbg.models.SimilarityModel2D)15 NotEnoughDataPointsException (mpicbg.models.NotEnoughDataPointsException)12 Point (mpicbg.models.Point)12 AbstractAffineModel2D (mpicbg.models.AbstractAffineModel2D)11 RigidModel2D (mpicbg.models.RigidModel2D)9 TranslationModel2D (mpicbg.models.TranslationModel2D)9 Patch (ini.trakem2.display.Patch)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 Layer (ini.trakem2.display.Layer)7 Rectangle (java.awt.Rectangle)7 AffineTransform (java.awt.geom.AffineTransform)7 RigidModel2D (mpicbg.trakem2.transform.RigidModel2D)6 TranslationModel2D (mpicbg.trakem2.transform.TranslationModel2D)6 SIFT (mpicbg.ij.SIFT)5 Feature (mpicbg.imagefeatures.Feature)5 FloatArray2DSIFT (mpicbg.imagefeatures.FloatArray2DSIFT)5 ByteProcessor (ij.process.ByteProcessor)4