Search in sources :

Example 1 with ProjectiveMapping2D

use of imagingbook.pub.geometry.mappings.linear.ProjectiveMapping2D in project imagingbook-common by imagingbook.

the class ImageExtractor method extractImage.

public ImageProcessor extractImage(int width, int height, Pnt2d[] sourcePnts) {
    ImageProcessor R = I.createProcessor(width, height);
    ProjectiveMapping2D T = getMapping(width, height, sourcePnts);
    extractImage(R, T);
    return R;
}
Also used : ImageProcessor(ij.process.ImageProcessor) ProjectiveMapping2D(imagingbook.pub.geometry.mappings.linear.ProjectiveMapping2D)

Example 2 with ProjectiveMapping2D

use of imagingbook.pub.geometry.mappings.linear.ProjectiveMapping2D in project imagingbook-common by imagingbook.

the class ImageExtractor method extractImage.

/**
 * Extracts a warped sub-image of the associated target image I,
 * defined by a sequence of 3 or 4 points. In the case of 3
 * points in sourcePnts, an {@link AffineMapping2D} is used; with 4 points,
 * a {@link ProjectiveMapping2D} is used. The 3 or 4 points map clockwise to
 * the corner points of the target image R, starting with the top-left corner.
 * @param R the target image;
 * @param sourcePnts an array of 3 or 4 {@link Pnt2d} objects.
 */
public void extractImage(ImageProcessor R, Pnt2d[] sourcePnts) {
    ProjectiveMapping2D T = getMapping(R.getWidth(), R.getHeight(), sourcePnts);
    extractImage(R, T);
}
Also used : ProjectiveMapping2D(imagingbook.pub.geometry.mappings.linear.ProjectiveMapping2D)

Example 3 with ProjectiveMapping2D

use of imagingbook.pub.geometry.mappings.linear.ProjectiveMapping2D 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 4 with ProjectiveMapping2D

use of imagingbook.pub.geometry.mappings.linear.ProjectiveMapping2D 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 5 with ProjectiveMapping2D

use of imagingbook.pub.geometry.mappings.linear.ProjectiveMapping2D 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

ProjectiveMapping2D (imagingbook.pub.geometry.mappings.linear.ProjectiveMapping2D)7 Pnt2d (imagingbook.pub.geometry.basic.Pnt2d)5 RealMatrix (org.apache.commons.math3.linear.RealMatrix)2 ImageProcessor (ij.process.ImageProcessor)1 ArrayList (java.util.ArrayList)1