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);
}
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();
}
}
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;
}
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));
}
}
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());
}
Aggregations