Skip to content

Commit

Permalink
Fix missing files
Browse files Browse the repository at this point in the history
  • Loading branch information
nfeybesse committed Jul 11, 2018
1 parent b3c2774 commit 6d82800
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 42 deletions.
Expand Up @@ -79,4 +79,12 @@ public final T[] get(int i, int j) {
// return internal.get(new Key(i + halfHeight, j + halfWidth));
}

public int getHalfWidth() {
return halfWidth;
}

public int getHalfHeight() {
return halfHeight;
}

}
Expand Up @@ -43,13 +43,13 @@ public MeshManager(int halfWidth, int halfHeight, Interpolator interpolator, dou

xBorder = 2 * (int) deltaX;
yBorder = 2 * (int) deltaY;
this.enlargedSize = new Size(frameSize.width + 2 * xBorder, frameSize.height + 2 * yBorder);
this.enlargedSize = new Size(frameSize.width + 2 * getxBorder(), frameSize.height + 2 * yBorder);
this.halfHeight = halfHeight;
this.halfWidth = halfWidth;
}

public Points getPoints() {
return points != null ? points : (points = new CandidatePoints(new Point(enlargedSize.width / 2, enlargedSize.height / 2), halfWidth, halfHeight, deltaX, deltaY, xBorder, yBorder, interpolator));
return points != null ? points : (points = new CandidatePoints(new Point(enlargedSize.width / 2, enlargedSize.height / 2), halfWidth, halfHeight, deltaX, deltaY, getxBorder(), yBorder, interpolator));
}

private Mesh getMesh() {
Expand All @@ -64,55 +64,28 @@ public Mesh getReverseMesh() {
return reverseMesh != null ? reverseMesh : (reverseMesh = getMesh3D().reverseMesh());
}

// private void reverseCoeffs(double[] coeffs) {
// for (int i = 0; i < coeffs.length; i++)
// coeffs[i] = 1 / coeffs[i];
// }

// public void recomputeGrid() {
// double[] widths = mesh3D.getWidths();
// reverseCoeffs(widths);
// mesh3D.normalize(widths, getOriginalSize().width);
//
// double[] heights = mesh3D.getHeights();
// reverseCoeffs(heights);
// mesh3D.normalize(heights, getOriginalSize().height);
// points = new Points(new Point(image.width() / 2, image.height() / 2), halfWidth, halfHeight, deltaX, deltaY, xBorder, yBorder, interpolator) {
// @Override
// double getHeightCoeff(double deltaY, int j) {
// return heights[j];
// }
//
// @Override
// double getWidthCoeff(double deltaX, int j) {
// return widths[j];
// }
// };
// mesh = null;
// mesh3D = null;
// }

// --------------------------------------------------

public Mat draw(Mat image, Scalar meshColor, Scalar ptsColor) {
Mat enlarged = new Mat();
Core.copyMakeBorder(image, enlarged, yBorder, yBorder, xBorder, xBorder, Core.BORDER_REPLICATE);
Core.copyMakeBorder(image, enlarged, yBorder, yBorder, getxBorder(), getxBorder(), Core.BORDER_REPLICATE);
getMesh().draw(enlarged, meshColor, ptsColor);
getReverseMesh().draw(enlarged, new Scalar(0, 0, 255), new Scalar(255, 0, 0));
return enlarged;
}

public Mat drawReverse(Mat image, Scalar meshColor, Scalar ptsColor) {
Mat enlarged = new Mat();
Core.copyMakeBorder(image, enlarged, yBorder, yBorder, xBorder, xBorder, Core.BORDER_REPLICATE);
Mat enlarged = buildEnlarged(image);
getReverseMesh().draw(enlarged, meshColor, ptsColor);
return enlarged;
}

public Mat dewarpReverse(Mat image) {
public Mat buildEnlarged(Mat image) {
Mat enlarged = new Mat();
Core.copyMakeBorder(image, enlarged, yBorder, yBorder, xBorder, xBorder, Core.BORDER_REPLICATE);
return getReverseMesh().dewarp(enlarged, image.size());
Core.copyMakeBorder(image, enlarged, yBorder, yBorder, getxBorder(), getxBorder(), Core.BORDER_REPLICATE);
return enlarged;
}

public Mat dewarpReverse(Mat image) {
return getReverseMesh().dewarp(buildEnlarged(image), image.size());
}

private Size getOriginalSize() {
Expand All @@ -125,14 +98,22 @@ public Mat draw3Dsurface(Scalar colorStart, Scalar colorEnd) {

public Mat dewarp(Mat image) {
Mat enlarged = new Mat();
Core.copyMakeBorder(image, enlarged, yBorder, yBorder, xBorder, xBorder, Core.BORDER_REPLICATE);
Core.copyMakeBorder(image, enlarged, yBorder, yBorder, getxBorder(), getxBorder(), Core.BORDER_REPLICATE);
return getMesh().dewarp(enlarged, getOriginalSize());
}

public Mat dewarp3D(Mat image) {
Mat enlarged = new Mat();
Core.copyMakeBorder(image, enlarged, yBorder, yBorder, xBorder, xBorder, Core.BORDER_REPLICATE);
Core.copyMakeBorder(image, enlarged, yBorder, yBorder, getxBorder(), getxBorder(), Core.BORDER_REPLICATE);
return getMesh3D().dewarp(enlarged, getOriginalSize());
}

public int getxBorder() {
return xBorder;
}

public int getyBorder() {
return yBorder;
}

}
@@ -0,0 +1,81 @@
package org.genericsystem.cv.application.mesh;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.core.Range;
import org.opencv.imgproc.Imgproc;
import org.opencv.utils.Converters;

public class ReverseMap {
private final Map<Range[], Mat> reverseMap = new HashMap<>();

public ReverseMap(Mesh mesh, int halfWidth, int halfHeight, Point imageCenter, double[] widths, double[] heights) {

double y = imageCenter.y;
for (int i = 0; i < halfHeight; i++) {
double x = imageCenter.x;
for (int j = 0; j < halfWidth; j++) {
putReverse(mesh.getCell(i, j), x, y, widths[j + halfWidth], heights[i + halfHeight]);
x += widths[j + halfWidth];
}

x = imageCenter.x;
for (int j = -1; j >= -halfWidth; j--) {
x -= widths[j + halfWidth];
putReverse(mesh.getCell(i, j), x, y, widths[j + halfWidth], heights[i + halfHeight]);
}
y += heights[i + halfHeight];
}
y = imageCenter.y;

for (int i = -1; i >= -halfHeight; i--) {
y -= heights[i + halfHeight];
double x = imageCenter.x;
for (int j = 0; j < halfWidth; j++) {
putReverse(mesh.getCell(i, j), x, y, widths[j + halfWidth], heights[i + halfHeight]);
x += widths[j + halfWidth];
}
x = imageCenter.x;
for (int j = -1; j >= -halfWidth; j--) {
x -= widths[j + halfWidth];
putReverse(mesh.getCell(i, j), x, y, widths[j + halfWidth], heights[i + halfHeight]);
}
}

}

public Point reverse(Point point) {
// System.out.println("Reverse : " + point.x + " " + point.y);
Mat homography = reverseMap.entrySet().stream().filter(entry -> entry.getKey()[0].start <= point.x && entry.getKey()[0].end >= point.x && entry.getKey()[1].start <= point.y && entry.getKey()[1].end >= point.y).map(entry -> entry.getValue())
.findFirst().get();
return transform(Arrays.asList(point), homography).get(0);
}

private List<Point> transform(List<Point> originals, Mat homography) {
Mat original = Converters.vector_Point2d_to_Mat(originals);
Mat results = new Mat();
Core.perspectiveTransform(original, results, homography);
List<Point> res = new ArrayList<>();
Converters.Mat_to_vector_Point2d(results, res);
return res;
}

public void putReverse(Point[] polygon, double x, double y, double rectWidth, double rectHeight) {
Point dewarpedTopLeft = new Point(x, y);
Point dewarpedTopRight = new Point(x + rectWidth, y);
Point dewarpedBottomRight = new Point(x + rectWidth, y + rectHeight);
Point dewarpedBottomLeft = new Point(x, y + rectHeight);
Mat homography = Imgproc.getPerspectiveTransform(new MatOfPoint2f(polygon), new MatOfPoint2f(dewarpedTopLeft, dewarpedTopRight, dewarpedBottomRight, dewarpedBottomLeft));
reverseMap.put(new Range[] { new Range((int) Math.round(x), (int) Math.round(x + rectWidth)), new Range((int) Math.round(y), (int) Math.round(y + rectHeight)) }, homography.inv());
// System.out.println(x + " " + y);
}

}
@@ -0,0 +1,15 @@
package org.genericsystem.cv.application.mesh;

import org.opencv.core.Point;
import org.opencv.core.Size;

public class ReversePoints extends Points {

public ReversePoints(ReverseMap reverseMap, int xBorder, int yBorder, Point imgCenter, int halfWidth, int halfHeight, Size size) {
super(xBorder, yBorder);
for (int i = -halfHeight; i <= halfHeight; i++)
for (int j = -halfWidth; j <= halfWidth; j++)
put(i, j, createIndexedPoint(reverseMap.reverse(new Point(imgCenter.x + j * (size.width / (2 * halfWidth)), imgCenter.y + i * (size.height / (2 * halfHeight))))));
}

}
Expand Up @@ -15,11 +15,13 @@
import org.genericsystem.cv.application.GSCapture;
import org.genericsystem.cv.application.GSVideoCapture;
import org.genericsystem.cv.application.fht.FHTManager;
import org.genericsystem.cv.application.mesh.Points;
import org.genericsystem.cv.utils.NativeLibraryLoader;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
Expand Down Expand Up @@ -126,13 +128,39 @@ private Image[] doWork() {

Mat stabilized = referenceManager.dewarp(newImgDescriptor, homography);
Mat binarized = new Img(stabilized, false).adaptativeGaussianInvThreshold(7, 5).getSrc();
if (!fhtManager.isInitialized() || frameCount % 30 == 0)
fhtManager.init(binarized);
// if (!fhtManager.isInitialized() || frameCount % 30 == 0)
fhtManager.init(binarized);
images[1] = new Img(fhtManager.getMeshManager().drawReverse(stabilized, new Scalar(255, 0, 0), new Scalar(0, 255, 0)), false).toJfxImage();

// Mesh reverseMesh = fhtManager.getMeshManager().getReverseMesh();
// Mat result = reverseMesh.dewarp(stabilized, stabilized.size());
images[2] = new Img(fhtManager.getMeshManager().dewarpReverse(stabilized), false).toJfxImage();

int nbrCell = 6;
Points pts = fhtManager.getMeshManager().getReverseMesh().getPoints();
Point[] basePts = new Point[] { pts.getPoint(-nbrCell, -nbrCell), pts.getPoint(-nbrCell, nbrCell), pts.getPoint(nbrCell, nbrCell), pts.getPoint(nbrCell, -nbrCell) };

double width = stabilized.size().width / (2 * fhtManager.getHalfGridWidth().get());
double height = stabilized.size().height / (2 * fhtManager.getHalfGridHeight().get());
Point[] targetPts = new Point[] { new Point(stabilized.size().width / 2 - nbrCell * width, stabilized.size().height / 2 - nbrCell * height), new Point(stabilized.size().width / 2 + nbrCell * width, stabilized.size().height / 2 - nbrCell * height),
new Point(stabilized.size().width / 2 + nbrCell * width, stabilized.size().height / 2 + nbrCell * height), new Point(stabilized.size().width / 2 - nbrCell * width, stabilized.size().height / 2 + nbrCell * height) };

Mat homography2 = Imgproc.getPerspectiveTransform(new MatOfPoint2f(basePts), new MatOfPoint2f(targetPts));
Mat stabilized2 = new Mat();
Imgproc.warpPerspective(fhtManager.getMeshManager().buildEnlarged(stabilized), stabilized2, homography2, stabilized.size());

Mat binarized2 = new Img(stabilized2, false).adaptativeGaussianInvThreshold(7, 5).getSrc();
// if (!fhtManager.isInitialized() || frameCount % 30 == 0)
fhtManager.init(binarized2);
images[3] = new Img(fhtManager.getMeshManager().drawReverse(stabilized2, new Scalar(255, 0, 0), new Scalar(0, 255, 0)), false).toJfxImage();

// Mesh reverseMesh = fhtManager.getMeshManager().getReverseMesh();
// Mat result = reverseMesh.dewarp(stabilized, stabilized.size());
images[4] = new Img(fhtManager.getMeshManager().dewarpReverse(stabilized2), false).toJfxImage();

// images[3] = new Img(stabilized2, false).toJfxImage();
// Calib3d.findHomography(new MatOfPoint2f(basePts), new MatOfPoint2f(targetPts), Calib3d.RANSAC, 1);

// Mat patch = Mat.zeros(frame.size(), frame.type());
// int deltaX = (int) (frame.size().width / 8);
// int deltaY = (int) (frame.size().height / 4);
Expand Down

0 comments on commit 6d82800

Please sign in to comment.