Skip to content

Commit

Permalink
Begin extrapolator
Browse files Browse the repository at this point in the history
  • Loading branch information
nfeybesse committed May 22, 2018
1 parent 202631c commit f94ac8a
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 31 deletions.
@@ -1,5 +1,13 @@
package org.genericsystem.cv.application;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import org.genericsystem.cv.AbstractApp;
import org.genericsystem.cv.Img;
import org.genericsystem.cv.application.GeneralInterpolator.OrientedPoint;
Expand All @@ -13,14 +21,6 @@
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import javafx.application.Platform;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
Expand Down Expand Up @@ -65,7 +65,7 @@ private void startTimer() {

@Override
protected void fillGrid(GridPane mainGrid) {
double displaySizeReduction = 1;
double displaySizeReduction = 1.5;
for (int col = 0; col < imageViews.length; col++)
for (int row = 0; row < imageViews[col].length; row++) {
ImageView imageView = new ImageView();
Expand Down Expand Up @@ -135,7 +135,7 @@ private Image[] doWork() {
for (int hStrip = 0; hStrip < hHoughTrajs.size(); hStrip++)
fhtVerticals.addAll(RadonTransform.toVerticalOrientedPoints(hHoughTrajs.get(hStrip), (hStrip + 1) * hStep, 0.5, 0.3));

GeneralInterpolator interpolatorFHT = new GeneralInterpolator(fhtHorizontals, fhtVerticals, 4, 12);
GeneralInterpolator interpolatorFHT = new GeneralInterpolator(fhtHorizontals, fhtVerticals, 4, 1);

ref = trace("Prepare interpolator", ref);

Expand Down
@@ -1,5 +1,9 @@
package org.genericsystem.cv.application.mesh;

import java.util.Arrays;
import java.util.List;
import java.util.stream.DoubleStream;

import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
Expand All @@ -8,10 +12,6 @@
import org.opencv.core.Scalar;
import org.opencv.core.Size;

import java.util.Arrays;
import java.util.List;
import java.util.stream.DoubleStream;

public class Mesh3D extends AbstractMesh<Point3> {

private final Mesh mesh;
Expand Down Expand Up @@ -99,11 +99,11 @@ public Mat dewarp(Mat src, Size originalSize) {
double height = DoubleStream.of(heights).sum();
double width = DoubleStream.of(widths).sum();

double coefHeight = originalSize.height / height;
double coefHeight = (originalSize.height * MeshManager.sizeCoeff) / height;
for (int i = 0; i < heights.length; i++)
heights[i] *= coefHeight;

double coefWidth = originalSize.width / width;
double coefWidth = originalSize.width * MeshManager.sizeCoeff / width;
for (int i = 0; i < widths.length; i++)
widths[i] *= coefWidth;
System.out.println(Arrays.toString(heights));
Expand All @@ -120,22 +120,13 @@ public Mat dewarp(Mat src, Size originalSize) {
deWarp(src, enlargedImage, mesh.getPoints(i, j), x, y, widths[j + halfWidth], heights[i + halfHeight]);
x += widths[j + halfWidth];
}
// if (x < enlargedImage.width() - 1) {
// if (x + widths[halfWidth - 1 + halfWidth] <= enlargedImage.width() - 1)
// deWarp(src, enlargedImage, mesh.extrapoleRight(i, halfWidth), x, y, widths[halfWidth - 1 + halfWidth], heights[i + halfHeight]);
// }

x = enlargedImage.width() / 2;
for (int j = -1; j >= -halfWidth; j--) {
x -= widths[j + halfWidth];
if (x >= 0 && (x + widths[j + halfWidth] < enlargedImage.width()) && y >= 0 && (y + heights[i + halfHeight] < enlargedImage.height()))
deWarp(src, enlargedImage, mesh.getPoints(i, j), x, y, widths[j + halfWidth], heights[i + halfHeight]);
}
// if (x > 0) {
// x -= widths[0];
// if (x >= 0)
// deWarp(src, enlargedImage, mesh.extrapoleLeft(i, -halfWidth - 1), x, y, widths[0], heights[i + halfHeight]);
// }
y += heights[i + halfHeight];
}
y = enlargedImage.height() / 2;
Expand Down
Expand Up @@ -27,8 +27,10 @@ public class MeshManager {
private Mesh mesh;
private Mesh3D mesh3D;

public static double sizeCoeff = 1.2;

public MeshManager(int halfWidth, int halfHeight, GeneralInterpolator interpolatorFHT, Mat src) {
this(halfWidth, halfHeight, interpolatorFHT, src.width() / 12, src.height() / 8, src);
this(halfWidth, halfHeight, interpolatorFHT, src.width() / (2 * halfWidth), src.height() / (2 * halfHeight), src);
}

public MeshManager(int halfWidth, int halfHeight, Interpolator interpolator, double deltaX, double deltaY, Mat image) {
Expand Down
125 changes: 120 additions & 5 deletions gs-cv/src/main/java/org/genericsystem/cv/application/mesh/Points.java
@@ -1,27 +1,75 @@
package org.genericsystem.cv.application.mesh;

import org.genericsystem.cv.application.Interpolator;
import org.opencv.core.Point;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import org.genericsystem.cv.application.Interpolator;
import org.opencv.core.Point;

public class Points {
private HashMap<Key, IndexedPoint> internal = new HashMap<>();
private List<IndexedPoint> pointIndex = new ArrayList<>();
private final Interpolator interpolator;
private final int xBorder;
private final int yBorder;
private final int extraWidth = 1;
private final int extraHeiht = 1;
private final int extraHeight = 1;

public Points(Point imgCenter, int halfWidth, int halfHeight, double deltaX, double deltaY, int xBorder, int yBorder, Interpolator interpolator) {
this.interpolator = interpolator;
this.xBorder = xBorder;
this.yBorder = yBorder;
put(0, 0, createIndexedPoint(imgCenter));
// for (int j = 1; j <= halfWidth; j++)
// put(0, j, createIndexedPoint((j <= (halfWidth - extraWidth)) ? horizontalMove(getPoint(0, j - 1), deltaX) : extrapole(getPoint(0, j - 2), getPoint(0, j - 1))));
// for (int j = -1; j >= -halfWidth; j--)
// put(0, j, createIndexedPoint((j >= (-halfWidth + extraWidth)) ? horizontalMove(getPoint(0, j + 1), -deltaX) : extrapole(getPoint(0, j + 2), getPoint(0, j + 1))));
// for (int i = 1; i <= halfHeight; i++) {
// put(i, 0, createIndexedPoint((i <= (halfHeight - extraHeight)) ? verticalMove(getPoint(i - 1, 0), deltaY) : extrapole(getPoint(i - 2, 0), getPoint(i - 1, 0))));
// for (int j = 1; j <= halfWidth; j++)
// put(i, j,
// createIndexedPoint((i <= (halfHeight - extraHeight) && (j <= halfWidth - extraWidth)) ? intersect(getPoint(i, j - 1), getPoint(i - 1, j))
// : intersectExtra(j != 1 ? getPoint(i, j - 2) : mock(getPoint(i - 1, 0), getPoint(i - 1, 1), getPoint(i, 0)), getPoint(i, j - 1), i != 1 ? getPoint(i - 2, j) : mock(getPoint(0, j - 1), getPoint(1, j - 1), getPoint(0, j)),
// getPoint(i - 1, j))));
// for (int j = -1; j >= -halfWidth; j--)
// put(i, j, createIndexedPoint((i <= (halfHeight - extraHeight) && j >= (-halfWidth + extraWidth)) ? intersect(getPoint(i, j + 1), getPoint(i - 1, j))
// : intersectExtra(getPoint(i, j + 2), getPoint(i, j + 1), i != 1 ? getPoint(i - 2, j) : mock(getPoint(0, j + 1), getPoint(1, j + 1), getPoint(0, j)), getPoint(i - 1, j))));
// }
// for (int i = -1; i >= -halfHeight; i--) {
// put(i, 0, createIndexedPoint((i >= (-halfHeight + extraHeight)) ? verticalMove(getPoint(i + 1, 0), -deltaY) : extrapole(getPoint(i + 2, 0), getPoint(i + 1, 0))));
// for (int j = 1; j <= halfWidth; j++)
// put(i, j, createIndexedPoint(((i >= -halfHeight + extraHeight) && j <= (halfWidth - extraWidth)) ? intersect(getPoint(i, j - 1), getPoint(i + 1, j))
// : intersectExtra(j != 1 ? getPoint(i, j - 2) : mock(getPoint(i + 1, 0), getPoint(i + 1, 1), getPoint(i, 0)), getPoint(i, j - 1), getPoint(i + 2, j), getPoint(i + 1, j))));
// for (int j = -1; j >= -halfWidth; j--)
// put(i, j, createIndexedPoint(
// (i >= (-halfHeight + extraHeight) && (j >= -halfWidth + extraWidth)) ? intersect(getPoint(i, j + 1), getPoint(i + 1, j)) : intersectExtra(getPoint(i, j + 2), getPoint(i, j + 1), getPoint(i + 2, j), getPoint(i + 1, j))));
// }

// for (int j = 1; j <= halfWidth; j++)
// put(0, j, createIndexedPoint((j <= (halfWidth - extraWidth)) ? horizontalMove(getPoint(0, j - 1), deltaX) : extrapole(getPoint(0, j - 2), getPoint(0, j - 1))));
// for (int j = -1; j >= -halfWidth; j--)
// put(0, j, createIndexedPoint((j >= (-halfWidth + extraWidth)) ? horizontalMove(getPoint(0, j + 1), -deltaX) : extrapole(getPoint(0, j + 2), getPoint(0, j + 1))));
// for (int i = 1; i <= halfHeight; i++) {
// put(i, 0, createIndexedPoint((i <= (halfHeight - extraHeight)) ? verticalMove(getPoint(i - 1, 0), deltaY) : extrapole(getPoint(i - 2, 0), getPoint(i - 1, 0))));
// for (int j = 1; j <= halfWidth; j++)
// put(i, j, createIndexedPoint((i <= (halfHeight - extraHeight) && (j <= halfWidth - extraWidth)) ? intersect(getPoint(i, j - 1), getPoint(i - 1, j))
// : intersectExtra(getPoint(i, j - 1), mock(getPoint(i - 1, j - 1), getPoint(i - 1, j), getPoint(i, j - 1)), getPoint(i - 1, j), mock(getPoint(i - 1, j - 1), getPoint(i, j - 1), getPoint(i - 1, j)))));
// for (int j = -1; j >= -halfWidth; j--)
// put(i, j, createIndexedPoint((i <= (halfHeight - extraHeight) && j >= (-halfWidth + extraWidth)) ? intersect(getPoint(i, j + 1), getPoint(i - 1, j))
// : intersectExtra(getPoint(i, j + 1), mock(getPoint(i - 1, j + 1), getPoint(i - 1, j), getPoint(i, j + 1)), getPoint(i - 1, j), mock(getPoint(i - 1, j + 1), getPoint(i, j + 1), getPoint(i - 1, j)))));
// }
// for (int i = -1; i >= -halfHeight; i--) {
// put(i, 0, createIndexedPoint((i >= (-halfHeight + extraHeight)) ? verticalMove(getPoint(i + 1, 0), -deltaY) : extrapole(getPoint(i + 2, 0), getPoint(i + 1, 0))));
// for (int j = 1; j <= halfWidth; j++)
// put(i, j, createIndexedPoint(((i >= -halfHeight + extraHeight) && j <= (halfWidth - extraWidth)) ? intersect(getPoint(i, j - 1), getPoint(i + 1, j))
// : intersectExtra(getPoint(i, j - 1), mock(getPoint(i + 1, j - 1), getPoint(i + 1, j), getPoint(i, j - 1)), getPoint(i + 1, j), mock(getPoint(i + 1, j - 1), getPoint(i, j - 1), getPoint(i + 1, j)))));
// for (int j = -1; j >= -halfWidth; j--)
// put(i, j, createIndexedPoint((i >= (-halfHeight + extraHeight) && (j >= -halfWidth + extraWidth)) ? intersect(getPoint(i, j + 1), getPoint(i + 1, j))
// : intersectExtra(getPoint(i, j + 1), mock(getPoint(i + 1, j + 1), getPoint(i + 1, j), getPoint(i, j + 1)), getPoint(i + 1, j), mock(getPoint(i + 1, j + 1), getPoint(i, j + 1), getPoint(i + 1, j)))));
// }

for (int j = 1; j <= halfWidth; j++)
put(0, j, createIndexedPoint(horizontalMove(getPoint(0, j - 1), deltaX)));
for (int j = -1; j >= -halfWidth; j--)
Expand All @@ -40,12 +88,78 @@ public Points(Point imgCenter, int halfWidth, int halfHeight, double deltaX, dou
for (int j = -1; j >= -halfWidth; j--)
put(i, j, createIndexedPoint(intersect(getPoint(i, j + 1), getPoint(i + 1, j))));
}
//
// for (int j = halfWidth + 1; j <= halfWidth + extraWidth; j++)
// put(0, j, createIndexedPoint(extrapole(getPoint(0, j - 2), getPoint(0, j - 1))));
// for (int j = -halfWidth - 1; j >= -halfWidth - extraWidth; j--)
// put(0, j, createIndexedPoint(extrapole(getPoint(0, j + 2), getPoint(0, j + 1))));
// for (int i = halfHeight + 1; i <= halfHeight + extraHeight; i++) {
// put(i, 0, createIndexedPoint(extrapole(getPoint(i - 2, 0), getPoint(i - 1, 0))));
// for (int j = 1; j <= halfWidth; j++)
// put(i, j, createIndexedPoint(intersectExtra(getPoint(i, j - 1), mock(getPoint(i - 1, j - 1), getPoint(i - 1, j), getPoint(i, j - 1)), getPoint(i - 1, j), mock(getPoint(i - 1, j - 1), getPoint(i, j - 1), getPoint(i - 1, j)))));
// for (int j = -1; j >= -halfWidth; j--)
// put(i, j, createIndexedPoint(intersectExtra(getPoint(i, j + 1), mock(getPoint(i - 1, j + 1), getPoint(i - 1, j), getPoint(i, j + 1)), getPoint(i - 1, j), mock(getPoint(i - 1, j + 1), getPoint(i, j + 1), getPoint(i - 1, j)))));
// }
// for (int i = -halfHeight - 1; i >= -halfHeight - extraHeight; i--) {
// put(i, 0, createIndexedPoint(extrapole(getPoint(i + 2, 0), getPoint(i + 1, 0))));
// for (int j = 1; j <= halfWidth; j++)
// put(i, j, createIndexedPoint(intersectExtra(getPoint(i, j - 1), mock(getPoint(i + 1, j - 1), getPoint(i + 1, j), getPoint(i, j - 1)), getPoint(i + 1, j), mock(getPoint(i + 1, j - 1), getPoint(i, j - 1), getPoint(i + 1, j)))));
// for (int j = -1; j >= -halfWidth; j--)
// put(i, j, createIndexedPoint(intersectExtra(getPoint(i, j + 1), mock(getPoint(i + 1, j + 1), getPoint(i + 1, j), getPoint(i, j + 1)), getPoint(i + 1, j), mock(getPoint(i + 1, j + 1), getPoint(i, j + 1), getPoint(i + 1, j)))));
// }
// for (int i = 1; i <= halfHeight + extraHeight; i++) {
// put(i, 0, createIndexedPoint(extrapole(getPoint(i - 2, 0), getPoint(i - 1, 0))));
// for (int j = halfWidth + 1; j <= halfWidth + extraWidth; j++)
// put(i, j, createIndexedPoint(intersectExtra(getPoint(i, j - 1), mock(getPoint(i - 1, j - 1), getPoint(i - 1, j), getPoint(i, j - 1)), getPoint(i - 1, j), mock(getPoint(i - 1, j - 1), getPoint(i, j - 1), getPoint(i - 1, j)))));
// for (int j = -halfWidth - 1; j >= -halfWidth - extraWidth; j--)
// put(i, j, createIndexedPoint(intersectExtra(getPoint(i, j + 1), mock(getPoint(i - 1, j + 1), getPoint(i - 1, j), getPoint(i, j + 1)), getPoint(i - 1, j), mock(getPoint(i - 1, j + 1), getPoint(i, j + 1), getPoint(i - 1, j)))));
// }
// for (int i = -1; i >= -halfHeight - extraHeight; i--) {
// put(i, 0, createIndexedPoint(extrapole(getPoint(i + 2, 0), getPoint(i + 1, 0))));
// for (int j = halfWidth + 1; j <= halfWidth + extraWidth; j++)
// put(i, j, createIndexedPoint(intersectExtra(getPoint(i, j - 1), mock(getPoint(i + 1, j - 1), getPoint(i + 1, j), getPoint(i, j - 1)), getPoint(i + 1, j), mock(getPoint(i + 1, j - 1), getPoint(i, j - 1), getPoint(i + 1, j)))));
// for (int j = -halfWidth - 1; j >= -halfWidth - extraWidth; j--)
// put(i, j, createIndexedPoint(intersectExtra(getPoint(i, j + 1), mock(getPoint(i + 1, j + 1), getPoint(i + 1, j), getPoint(i, j + 1)), getPoint(i + 1, j), mock(getPoint(i + 1, j + 1), getPoint(i, j + 1), getPoint(i + 1, j)))));
// }

}

private Point mock(Point p1, Point p2, Point p3) {
return new Point(p3.x + p2.x - p1.x, p3.y + p2.y - p1.y);
}

private Point extrapole(Point p1, Point p2) {
return new Point(p2.x + p2.x - p1.x, p2.y + p2.y - p1.y);
}

private Point intersectExtra(Point p1, Point p2, Point p3, Point p4) {
double x1 = p1.x;
double x2 = p2.x;
double x3 = p3.x;
double x4 = p4.x;
double y1 = p1.y;
double y2 = p2.y;
double y3 = p3.y;
double y4 = p4.y;

double x = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4));
double y = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4));
return new Point(x, y);

}

static double[] cross(double[] a, double b[]) {
return new double[] { a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0] };
}

static double[] cross2D(double[] a, double b[]) {
return uncalibrate(cross(a, b));
}

static double[] uncalibrate(double[] a) {
return new double[] { a[0] / a[2], a[1] / a[2], 1 };
}

private IndexedPoint createIndexedPoint(Point point) {
IndexedPoint indexedPoint = new IndexedPoint(pointIndex.size(), point);
pointIndex.add(indexedPoint);
Expand Down Expand Up @@ -106,6 +220,7 @@ private Point horizontalMove(Point startingPoint, double deltaX) {

// points = new Point[2 * halfHeight + 1][2 * halfWidth + 1];
public void put(int i, int j, IndexedPoint point) {
System.out.println(i + " " + j);
internal.put(new Key(i, j), point);
}

Expand All @@ -115,8 +230,8 @@ public IndexedPoint get(int i, int j) {
}

public Point getPoint(int i, int j) {
assert internal.get(new Key(i, j)) != null : "None : " + i + " " + j;
return internal.get(new Key(i, j)).getPoint();
// return internal.get(new Key(i + halfHeight, j + halfWidth));
}

public List<Point> getPointIndex() {
Expand Down

0 comments on commit f94ac8a

Please sign in to comment.