Search in sources :

Example 6 with Pnt2d

use of imagingbook.pub.geometry.basic.Pnt2d in project imagingbook-common by imagingbook.

the class ImageExtractor method extractImage.

/**
 * Fills the image {@code R} from the source image
 * {@code I} (referenced by {@code this} object).
 * The image {@code R} is extracted from a quadrilateral patch of the source image,
 * defined by the transformation of the boundary of {@code R} by {@code T(x)}.
 * Grayscale and color images my not be mixed (i.e., {@code R} must be of the same type as {@code I}).
 * @param R the image to be filled.
 * @param T a {@link LinearMapping2D} object.
 */
public void extractImage(ImageProcessor R, LinearMapping2D T) {
    int prevInterpolationMethod = I.getInterpolationMethod();
    // save current interpolation method
    I.setInterpolationMethod(interpolationMethod);
    ImageAccessor iaI = ImageAccessor.create(I);
    ImageAccessor iaR = ImageAccessor.create(R);
    int wT = R.getWidth();
    int hT = R.getHeight();
    for (int u = 0; u < wT; u++) {
        for (int v = 0; v < hT; v++) {
            Pnt2d uv = PntInt.from(u, v);
            Pnt2d xy = T.applyTo(uv);
            float[] val = iaI.getPix(xy.getX(), xy.getY());
            iaR.setPix(u, v, val);
        }
    }
    // restore interpolation method
    I.setInterpolationMethod(prevInterpolationMethod);
}
Also used : Pnt2d(imagingbook.pub.geometry.basic.Pnt2d) ImageAccessor(imagingbook.lib.image.access.ImageAccessor)

Example 7 with Pnt2d

use of imagingbook.pub.geometry.basic.Pnt2d in project imagingbook-common by imagingbook.

the class LucasKanadeInverseMatcher method initializeMatch.

private void initializeMatch(ProjectiveMapping2D Tinit) {
    // n = Tinit.getParameters().length;	// number of transformation parameters
    // number of transformation parameters
    n = getParameters(Tinit).length;
    // S[u][v] holds a double vector of length n
    S = new double[wR][hR][];
    // gradient of R
    Rx = gradientX(R).getFloatArray();
    Ry = gradientY(R).getFloatArray();
    // cumulated Hessian matrix of size n x n (initially zero)
    double[][] H = new double[n][n];
    ProjectiveMapping2D Tp = Tinit;
    for (int u = 0; u < wR; u++) {
        // for all coordinates (u,v) in R do
        for (int v = 0; v < hR; v++) {
            // double[] x = {u - xc, v - yc};	// position w.r.t. the center of R
            // position w.r.t. the center of R
            Pnt2d x = PntDouble.from(u - xc, v - yc);
            double[] gradR = { Rx[u][v], Ry[u][v] };
            double[][] J = Tp.getJacobian(x);
            // column vector of length n (6)
            double[] sx = Matrix.multiply(gradR, J);
            // keep for use in main loop
            S[u][v] = sx;
            double[][] Hxy = Matrix.outerProduct(sx, sx);
            // cumulate the Hessian
            H = Matrix.add(H, Hxy);
        }
    }
    // inverse of Hessian
    Hi = Matrix.inverse(H);
    if (Hi == null) {
        IJ.log("singular Hessian!");
        throw new RuntimeException("could not invert Hessian");
    }
    iteration = 0;
    if (params.showSteepestDescentImages)
        showSteepestDescentImages(S);
    if (params.showHessians) {
        IjUtils.createImage("H", H).show();
        IjUtils.createImage("Hi", Matrix.inverse(H)).show();
    }
}
Also used : ProjectiveMapping2D(imagingbook.pub.geometry.mappings.linear.ProjectiveMapping2D) Pnt2d(imagingbook.pub.geometry.basic.Pnt2d)

Example 8 with Pnt2d

use of imagingbook.pub.geometry.basic.Pnt2d in project imagingbook-common by imagingbook.

the class PolygonSampler method pathLength.

protected double pathLength(Pnt2d[] V) {
    double L = 0;
    final int N = V.length;
    for (int i = 0; i < N; i++) {
        Pnt2d p0 = V[i];
        Pnt2d p1 = V[(i + 1) % N];
        L = L + p0.distance(p1);
    }
    return L;
}
Also used : Pnt2d(imagingbook.pub.geometry.basic.Pnt2d)

Example 9 with Pnt2d

use of imagingbook.pub.geometry.basic.Pnt2d in project imagingbook-common by imagingbook.

the class HomographyEstimator method runTestLeastSquares.

private static void runTestLeastSquares(Pnt2d[] pntsA, Pnt2d[] pntsB) {
    ProjectiveMapping2D mapping = ProjectiveMapping2D.fromPoints(pntsA, pntsB);
    double[][] Hmap = mapping.getTransformationMatrix();
    System.out.println("H (mapping) = ");
    System.out.println(Matrix.toString(Hmap));
    {
        Pnt2d[] pntsC = new Pnt2d[pntsA.length];
        for (int i = 0; i < pntsA.length; i++) {
            // (Pnt2d)
            pntsC[i] = mapping.applyTo(PntDouble.from(pntsA[i]));
        // mapping.applyTo(Pnt2d.PntDouble.from(pntsA[i].getX(), pntsA[i].getY()));
        }
        System.out.println("\nPoints mapped:");
        double sumDist2 = 0;
        double maxDist2 = Double.NEGATIVE_INFINITY;
        for (int i = 0; i < pntsA.length; i++) {
            Pnt2d a = pntsA[i];
            Pnt2d b = pntsB[i];
            Pnt2d c = pntsC[i];
            double dist2 = b.distanceSq(c);
            sumDist2 += dist2;
            maxDist2 = Math.max(maxDist2, dist2);
            System.out.format("(%.3f, %.3f) -> (%.3f, %.3f) d=%.4f\n", a.getX(), a.getY(), c.getX(), c.getY(), dist2);
        }
        System.out.format("\nTotal error = %.2f\n", Math.sqrt(sumDist2));
        System.out.format("Max. dist = %.2f\n", Math.sqrt(maxDist2));
    }
    {
        System.out.println("   +++ running refinement ++++");
        RealMatrix Hinit = MatrixUtils.createRealMatrix(Hmap);
        RealMatrix Hest = refineHomography(Hinit, pntsA, pntsB);
        System.out.println("H (estim.) = ");
        System.out.println(Matrix.toString(Hest.getData()));
        Pnt2d[] pntsC = new Pnt2d[pntsA.length];
        for (int i = 0; i < pntsA.length; i++) {
            pntsC[i] = mapPoint(Hest, pntsA[i]);
        }
        // System.out.println("\nPoints mapped:");
        double sumDist2 = 0;
        double maxDist2 = Double.NEGATIVE_INFINITY;
        for (int i = 0; i < pntsA.length; i++) {
            @SuppressWarnings("unused") Pnt2d a = pntsA[i];
            Pnt2d b = pntsB[i];
            Pnt2d c = pntsC[i];
            double dist2 = b.distanceSq(c);
            sumDist2 += dist2;
            maxDist2 = Math.max(maxDist2, dist2);
        // System.out.format("(%.3f, %.3f) -> (%.3f, %.3f) d=%.4f\n", a.getX(), a.getY(), c.getX(), c.getY(), dist2);
        }
        System.out.format("\nTotal error = %.2f\n", Math.sqrt(sumDist2));
        System.out.format("Max. dist = %.2f\n", Math.sqrt(maxDist2));
    }
}
Also used : ProjectiveMapping2D(imagingbook.pub.geometry.mappings.linear.ProjectiveMapping2D) Pnt2d(imagingbook.pub.geometry.basic.Pnt2d) RealMatrix(org.apache.commons.math3.linear.RealMatrix)

Example 10 with Pnt2d

use of imagingbook.pub.geometry.basic.Pnt2d in project imagingbook-common by imagingbook.

the class HomographyEstimator method main.

/**
 * Used for testing only.
 * @param args ignored
 */
public static void main(String[] args) {
    RealMatrix Hreal = MatrixUtils.createRealMatrix(new double[][] { { 3, 2, -1 }, { 5, 0, 2 }, { 4, 4, 9 } });
    System.out.println("H (real) = ");
    System.out.println(Matrix.toString(Hreal.scalarMultiply(1 / Hreal.getEntry(2, 2)).getData()));
    // System.out.println(Matrix.toString(Hreal.getData()));
    List<Pnt2d> pntlistA = new ArrayList<Pnt2d>();
    pntlistA.add(PntInt.from(10, 7));
    pntlistA.add(PntInt.from(3, -1));
    pntlistA.add(PntInt.from(5, 5));
    pntlistA.add(PntInt.from(-6, 13));
    pntlistA.add(PntInt.from(0, 1));
    pntlistA.add(PntInt.from(2, 3));
    List<Pnt2d> pntlistB = new ArrayList<Pnt2d>();
    for (Pnt2d a : pntlistA) {
        pntlistB.add(mapPointWithNoise(Hreal, a, NOISE));
    }
    Pnt2d[] pntsA = pntlistA.toArray(new Pnt2d[0]);
    Pnt2d[] pntsB = pntlistB.toArray(new Pnt2d[0]);
    System.out.println("\nPoint correspondences:");
    for (int i = 0; i < pntsA.length; i++) {
        Pnt2d a = pntsA[i];
        Pnt2d b = pntsB[i];
        System.out.format("(%.3f, %.3f) -> (%.3f, %.3f)\n", a.getX(), a.getY(), b.getX(), b.getY());
    }
    System.out.println();
    System.out.println("\n*************** WITHOUT NONLINEAR REFINEMENT *****************");
    runTestDLT(new HomographyEstimator(true, false), pntsA, pntsB);
    System.out.println("\n*************** WITH NONLINEAR REFINEMENT *****************");
    runTestDLT(new HomographyEstimator(true, true), pntsA, pntsB);
    System.out.println("\n*************** USING LEAST-SQUARES MAPPING *****************");
    runTestLeastSquares(pntsA, pntsB);
    System.out.println("\n*************** GENERATE A MAPPING *****************");
    ProjectiveMapping2D pm = new HomographyEstimator().getHomographyMapping(pntsA, pntsB);
    System.out.println("projective mapping = \n" + pm.toString());
}
Also used : ProjectiveMapping2D(imagingbook.pub.geometry.mappings.linear.ProjectiveMapping2D) Pnt2d(imagingbook.pub.geometry.basic.Pnt2d) RealMatrix(org.apache.commons.math3.linear.RealMatrix) ArrayList(java.util.ArrayList)

Aggregations

Pnt2d (imagingbook.pub.geometry.basic.Pnt2d)39 Test (org.junit.Test)6 ProjectiveMapping2D (imagingbook.pub.geometry.mappings.linear.ProjectiveMapping2D)5 RealMatrix (org.apache.commons.math3.linear.RealMatrix)5 FloatPolygon (ij.process.FloatPolygon)2 Polygon (java.awt.Polygon)2 ArrayList (java.util.ArrayList)2 ImageProcessor (ij.process.ImageProcessor)1 ImageAccessor (imagingbook.lib.image.access.ImageAccessor)1 PntInt (imagingbook.pub.geometry.basic.Pnt2d.PntInt)1 AlgebraicLine (imagingbook.pub.geometry.lines.AlgebraicLine)1 Mapping2D (imagingbook.pub.geometry.mappings.Mapping2D)1 LinearMapping2D (imagingbook.pub.geometry.mappings.linear.LinearMapping2D)1 Point (java.awt.Point)1 Path2D (java.awt.geom.Path2D)1 ArrayRealVector (org.apache.commons.math3.linear.ArrayRealVector)1 RealVector (org.apache.commons.math3.linear.RealVector)1