Skip to content

Commit

Permalink
gs-cv: refactored LineDetectors(1,2,3) and Line(s) to extract common …
Browse files Browse the repository at this point in the history
…code
  • Loading branch information
plassalas committed Oct 25, 2017
1 parent 92b2f0d commit ed4eb34
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 187 deletions.
6 changes: 3 additions & 3 deletions gs-cv/src/main/java/org/genericsystem/cv/LinesDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;

import org.genericsystem.cv.utils.Line;
import org.genericsystem.cv.utils.NativeLibraryLoader;
import org.genericsystem.cv.utils.Ransac;
Expand All @@ -26,6 +23,9 @@
import org.opencv.imgproc.Imgproc;
import org.opencv.videoio.VideoCapture;

import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;

public class LinesDetector extends AbstractApp {

static {
Expand Down
64 changes: 32 additions & 32 deletions gs-cv/src/main/java/org/genericsystem/cv/LinesDetector2.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;

import org.genericsystem.cv.LinesDetector.Damper;
import org.genericsystem.cv.utils.Line;
import org.genericsystem.cv.utils.NativeLibraryLoader;
Expand All @@ -27,6 +24,9 @@
import org.opencv.imgproc.Imgproc;
import org.opencv.videoio.VideoCapture;

import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;

public class LinesDetector2 extends AbstractApp {

static {
Expand Down Expand Up @@ -61,33 +61,33 @@ protected void fillGrid(GridPane mainGrid) {
if (lines.size() > 10) {
// lines.draw(frame, new Scalar(0, 0, 255));

frameView.setImage(Tools.mat2jfxImage(frame));
// Mat dePerspectived = new Mat(frame.size(), CvType.CV_8UC3, new Scalar(255, 255, 255));
Ransac<Line> ransac = lines.vanishingPointRansac(frame.width(), frame.height());
Mat homography = (Mat) ransac.getBestModel().getParams()[0];
lines = Lines.of(ransac.getBestDataSet().values());
lines = Lines.of(lines.perspectivTransform(homography));

System.out.println("Ransac angle : " + ((double) ransac.getBestModel().getParams()[1]) / Math.PI * 180);
// System.out.println("vpx : " + ((double) ransac.getBestModel().getParams()[2]));
// System.out.println("vpy : " + ((double) ransac.getBestModel().getParams()[3]));
Mat mask = new Mat(frame.size(), CvType.CV_8UC1, new Scalar(255));
Mat maskWarpped = new Mat();
Imgproc.warpPerspective(mask, maskWarpped, homography, frame.size());
Mat tmp = new Mat();
Imgproc.warpPerspective(frame, tmp, homography, frame.size(), Imgproc.INTER_LINEAR, Core.BORDER_REPLICATE, Scalar.all(255));
tmp.copyTo(dePerspectived, maskWarpped);
lines.draw(dePerspectived, new Scalar(0, 255, 0));
deskiewedView.setImage(Tools.mat2jfxImage(dePerspectived));

} else
System.out.println("Not enough lines : " + lines.size());

} catch (Throwable e) {
e.printStackTrace();
}

}, 33, 33, TimeUnit.MILLISECONDS);
frameView.setImage(Tools.mat2jfxImage(frame));
// Mat dePerspectived = new Mat(frame.size(), CvType.CV_8UC3, new Scalar(255, 255, 255));
Ransac<Line> ransac = lines.vanishingPointRansac(frame.width(), frame.height());
Mat homography = (Mat) ransac.getBestModel().getParams()[0];
lines = Lines.of(ransac.getBestDataSet().values());
lines = Lines.of(lines.perspectivTransform(homography));

System.out.println("Ransac angle : " + ((double) ransac.getBestModel().getParams()[1]) / Math.PI * 180);
// System.out.println("vpx : " + ((double) ransac.getBestModel().getParams()[2]));
// System.out.println("vpy : " + ((double) ransac.getBestModel().getParams()[3]));
Mat mask = new Mat(frame.size(), CvType.CV_8UC1, new Scalar(255));
Mat maskWarpped = new Mat();
Imgproc.warpPerspective(mask, maskWarpped, homography, frame.size());
Mat tmp = new Mat();
Imgproc.warpPerspective(frame, tmp, homography, frame.size(), Imgproc.INTER_LINEAR, Core.BORDER_REPLICATE, Scalar.all(255));
tmp.copyTo(dePerspectived, maskWarpped);
lines.draw(dePerspectived, new Scalar(0, 255, 0));
deskiewedView.setImage(Tools.mat2jfxImage(dePerspectived));

} else
System.out.println("Not enough lines : " + lines.size());

} catch (Throwable e) {
e.printStackTrace();
}

}, 33, 33, TimeUnit.MILLISECONDS);

}

Expand Down Expand Up @@ -143,8 +143,8 @@ public Ransac<Line> vanishingPointRansac(int width, int height) {
Core.transform(new MatOfPoint2f(new Point(rotTargets[0].x, bary.y), new Point(rotTargets[1].x, bary.y)), resultsInv, matrixInv);
Point[] rotInvTargets = resultsInv.toArray();

homography[0] = Imgproc.getPerspectiveTransform(new MatOfPoint2f(new Point(line.getX1(), line.getY1()), new Point(line.getX2(), line.getY2()), rotInvTargets[1], rotInvTargets[0]), new MatOfPoint2f(new Point[] {
new Point(rotTargets[0].x, newy1), new Point(rotTargets[1].x, newy1), new Point(rotTargets[1].x, bary.y), new Point(rotTargets[0].x, bary.y) }));
homography[0] = Imgproc.getPerspectiveTransform(new MatOfPoint2f(new Point(line.getX1(), line.getY1()), new Point(line.getX2(), line.getY2()), rotInvTargets[1], rotInvTargets[0]),
new MatOfPoint2f(new Point[] { new Point(rotTargets[0].x, newy1), new Point(rotTargets[1].x, newy1), new Point(rotTargets[1].x, bary.y), new Point(rotTargets[0].x, bary.y) }));
}
return new Model<Line>() {

Expand Down
Loading

0 comments on commit ed4eb34

Please sign in to comment.