Skip to content

Commit

Permalink
Start spline interpolator
Browse files Browse the repository at this point in the history
  • Loading branch information
nfeybesse committed May 31, 2018
1 parent 02c730b commit 52a66ad
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 46 deletions.
Expand Up @@ -43,7 +43,7 @@ public double interpolateHorizontals(double x, double y) {
double sumHCoefs = 0; // somme des coefficients pour l'angle horizontal
double hAngle = 0; // angle horizontal
for (OrientedPoint op : horizontals) {
double geoCoef = Math.pow(1 / squaredEuclidianDistance(x, y, op), pow / 2);
double geoCoef = 1 / Math.pow(squaredEuclidianDistance(x, y, op), pow / 2);
double hCoef = geoCoef * op.strenght;
hAngle += hCoef * op.angle;
sumHCoefs += hCoef;
Expand Down
@@ -1,5 +1,15 @@
package org.genericsystem.cv.application;

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

import org.apache.commons.math3.analysis.interpolation.LinearInterpolator;
import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction;
import org.genericsystem.cv.AbstractApp;
Expand All @@ -15,16 +25,6 @@
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;

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

import javafx.application.Platform;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
Expand Down Expand Up @@ -138,9 +138,9 @@ private Image[] doWork() {
for (int hStrip = 0; hStrip < hHoughTrajs.size(); hStrip++)
fhtVerticals.add(RadonTransform.toVerticalOrientedPoints(hHoughTrajs.get(hStrip), (hStrip + 1) * hStep, 0.2, 0.08));

List<List<Segment>>[] horizontalSegments = connect(fhtHorizontals, hStep, 2, false);
List<List<Segment>>[] horizontalSegments = connect(fhtHorizontals, hStep, 1, false);
List<PolynomialSplineFunction>[] horizontalSplines = toSplines(horizontalSegments, false);
List<List<Segment>>[] verticalSegments = connect(fhtVerticals, vStep, 2, true);
List<List<Segment>>[] verticalSegments = connect(fhtVerticals, vStep, 1, true);
List<PolynomialSplineFunction>[] verticalSplines = toSplines(verticalSegments, true);

Img splineDisplay = new Img(superFrame.getFrame().getSrc().clone(), false);
Expand All @@ -167,6 +167,7 @@ private Image[] doWork() {
List<OrientedPoint> flatFhtVerticals = fhtVerticals.stream().flatMap(h -> Stream.of(h)).flatMap(h -> h.stream()).collect(Collectors.toList());
List<OrientedPoint> flatFhtHorizontals = fhtHorizontals.stream().flatMap(h -> Stream.of(h)).flatMap(h -> h.stream()).collect(Collectors.toList());
GeneralInterpolator interpolatorFHT = new GeneralInterpolator(flatHorizontalSegments, flatVerticalSegments, 4, 0.0001);
SplineInterpolator superInterpolator = new SplineInterpolator(interpolatorFHT, horizontalSplines, verticalSplines);

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

Expand Down Expand Up @@ -214,7 +215,7 @@ private Image[] doWork() {
images[3] = frameDisplayFHT.toJfxImage();
ref = trace("Display lines", ref);

MeshManager meshManager = new MeshManager(6, 4, interpolatorFHT, superFrame.getFrame().getSrc());
MeshManager meshManager = new MeshManager(6, 4, superInterpolator, superFrame.getFrame().getSrc());
ref = trace("Build mesh", ref);

images[4] = new Img(meshManager.drawOnCopy(new Scalar(0, 255, 0), new Scalar(0, 0, 255)), false).toJfxImage();
Expand Down
@@ -1,45 +1,48 @@
package org.genericsystem.cv.application;

import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction;

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

import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction;

public class SplineInterpolator implements Interpolator {

private final List<PolynomialSplineFunction[]> horizontals;
private final List<PolynomialSplineFunction[]> verticals;
private final double pow;
private final double minDist;

public SplineInterpolator(List<PolynomialSplineFunction>[] horizontals, List<PolynomialSplineFunction>[] verticals, double pow, double minDist) {
private final Interpolator subInterpolator;

public SplineInterpolator(Interpolator subInterpolator, List<PolynomialSplineFunction>[] horizontals, List<PolynomialSplineFunction>[] verticals) {
this.horizontals = Arrays.stream(horizontals).flatMap(h -> h.stream()).map(f -> new PolynomialSplineFunction[] { f, f.polynomialSplineDerivative() }).collect(Collectors.toList());
this.verticals = Arrays.stream(verticals).flatMap(h -> h.stream()).map(f -> new PolynomialSplineFunction[] { f, f.polynomialSplineDerivative() }).collect(Collectors.toList());
this.pow = pow;
this.minDist = minDist;

this.subInterpolator = subInterpolator;
}

@Override
public double interpolateVerticals(double x, double y) {
PolynomialSplineFunction[] left = findLeftVerticalSplines(x, y);
PolynomialSplineFunction[] right = findRightVerticalSplines(x, y);
if (left != null) {
if (right != null) {
double x1 = left[0].value(y);
double x2 = right[0].value(y);
return (x - x1) * left[1].value(y) / (x2 - x1) + (x2 - x) * right[1].value(y) / (x2 - x1);
} else
return left[1].value(y);
} else {
if (right != null)
return right[1].value(y);
else {
// TODO
return 0;
// galere
}
}
if (left != null && right != null) {
double x1 = left[0].value(y);
double x2 = right[0].value(y);
return -((x - x1) / (x2 - x1) * Math.atan(right[1].value(y)) + (x2 - x) / (x2 - x1) * Math.atan(left[1].value(y)));
} else
return subInterpolator.interpolateVerticals(x, y);
// if (left != null) {
// if (right != null) {
// double x1 = left[0].value(y);
// double x2 = right[0].value(y);
// return (x - x1) * left[1].value(y) / (x2 - x1) + (x2 - x) * right[1].value(y) / (x2 - x1);
// } else
// return left[1].value(y);
// } else {
// if (right != null)
// return right[1].value(y);
// else
// return subInterpolator.interpolateVerticals(x, y);
// }

}

Expand All @@ -49,7 +52,7 @@ private PolynomialSplineFunction[] findRightVerticalSplines(double x, double y)
for (PolynomialSplineFunction[] function : verticals) {
if (function[0].isValidPoint(y)) {
double polyValue = function[0].value(y);
if (polyValue < value) {
if (polyValue < value && polyValue >= x) {
value = polyValue;
result = function;
}
Expand All @@ -64,7 +67,7 @@ private PolynomialSplineFunction[] findLeftVerticalSplines(double x, double y) {
for (PolynomialSplineFunction[] function : verticals) {
if (function[0].isValidPoint(y)) {
double polyValue = function[0].value(y);
if (polyValue > value) {
if (polyValue > value && polyValue <= x) {
value = polyValue;
result = function;
}
Expand All @@ -77,10 +80,13 @@ private PolynomialSplineFunction[] findLeftVerticalSplines(double x, double y) {
public double interpolateHorizontals(double x, double y) {
PolynomialSplineFunction[] top = findTopHorizontalSplines(x, y);
PolynomialSplineFunction[] bottom = findBottomVerticalSplines(x, y);
double y1 = top[0].value(x);
double y2 = bottom[0].value(x);
if (top != null && bottom != null) {
double y1 = top[0].value(x);
double y2 = bottom[0].value(x);

return (y - y1) * top[1].value(x) / (y2 - y1) + (y2 - y) * bottom[1].value(x) / (y2 - y1);
return (y - y1) / (y2 - y1) * Math.atan(bottom[1].value(x)) + (y2 - y) / (y2 - y1) * Math.atan(top[1].value(x));
} else
return subInterpolator.interpolateHorizontals(x, y);
}

private PolynomialSplineFunction[] findBottomVerticalSplines(double x, double y) {
Expand All @@ -89,7 +95,7 @@ private PolynomialSplineFunction[] findBottomVerticalSplines(double x, double y)
for (PolynomialSplineFunction[] function : horizontals) {
if (function[0].isValidPoint(x)) {
double polyValue = function[0].value(x);
if (polyValue < value) {
if (polyValue < value && polyValue >= y) {
value = polyValue;
result = function;
}
Expand All @@ -104,7 +110,7 @@ private PolynomialSplineFunction[] findTopHorizontalSplines(double x, double y)
for (PolynomialSplineFunction[] function : horizontals) {
if (function[0].isValidPoint(x)) {
double polyValue = function[0].value(x);
if (polyValue > value) {
if (polyValue > value && polyValue <= y) {
value = polyValue;
result = function;
}
Expand Down
@@ -1,6 +1,5 @@
package org.genericsystem.cv.application.mesh;

import org.genericsystem.cv.application.GeneralInterpolator;
import org.genericsystem.cv.application.Interpolator;
import org.opencv.core.Core;
import org.opencv.core.Mat;
Expand All @@ -27,7 +26,7 @@ public class MeshManager {
private Mesh mesh;
private Mesh3D mesh3D;

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

Expand Down

0 comments on commit 52a66ad

Please sign in to comment.