Skip to content

Commit

Permalink
Always displays frame image
Browse files Browse the repository at this point in the history
  • Loading branch information
nfeybesse committed Nov 22, 2017
1 parent fc73b13 commit e19183c
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected void fillGrid(GridPane mainGrid) {

timerFields.scheduleAtFixedRate(() -> onSpace(), 0, STABILIZATION_DELAY, TimeUnit.MILLISECONDS);

// Detect the rectangles
Img display = new Img(frame, false);
timerFields.scheduleAtFixedRate(() -> {
try {
Stats.beginTask("frame");
Expand All @@ -99,68 +99,71 @@ protected void fillGrid(GridPane mainGrid) {
}

Mat deperspectivGraphy = computeFrameToDeperspectivedHomography(frame);
if (deperspectivGraphy == null) {
if (deperspectivGraphy != null) {
if (stabilizedImgDescriptor == null) {
stabilizedImgDescriptor = new ImgDescriptor(frame, deperspectivGraphy);
return;
}
if (stabilizationHasChanged && stabilizationErrors > 20) {
// TODO: clean fields
fields.reset();
stabilizationErrors = 0;
stabilizedImgDescriptor = new ImgDescriptor(frame, deperspectivGraphy);
return;
}

ImgDescriptor newImgDescriptor = new ImgDescriptor(frame, deperspectivGraphy);
Stats.beginTask("stabilization homography");
Mat stabilizationHomography = stabilizedImgDescriptor.computeStabilizationGraphy(newImgDescriptor);
Stats.endTask("stabilization homography");
if (stabilizationHomography != null) {
Img stabilized = warpPerspective(frame, stabilizationHomography);
Img stabilizedDisplay = new Img(stabilized.getSrc(), true);
if (stabilizationHasChanged) {
Stats.beginTask("stabilizationHasChanged");
stabilized = newImgDescriptor.getDeperspectivedImg();
stabilizedDisplay = new Img(stabilized.getSrc(), true);
Mat fieldsHomography = new Mat();
Core.gemm(deperspectivGraphy, stabilizationHomography.inv(), 1, new Mat(), 0, fieldsHomography);
Stats.beginTask("restabilizeFields");
fields.restabilizeFields(fieldsHomography);
Stats.endTask("restabilizeFields");
stabilizedImgDescriptor = newImgDescriptor;
stabilizationHomography = deperspectivGraphy;
stabilizationHasChanged = false;
Stats.endTask("stabilizationHasChanged");
}
Stats.beginTask("consolidate fields");
fields.consolidate(stabilizedDisplay);
Stats.endTask("consolidate fields");
Stats.beginTask("consolidateOcr");
fields.performOcr(stabilized);
Stats.endTask("consolidateOcr");

Img stabilizedDebug = new Img(stabilizedDisplay.getSrc(), true);
fields.drawFieldsOnStabilizedDebug(stabilizedDebug);

fields.drawOcrPerspectiveInverse(display, stabilizationHomography.inv(), 1);
fields.drawFieldsOnStabilized(stabilizedDisplay);

src0.setImage(display.toJfxImage());
src1.setImage(stabilizedDisplay.toJfxImage());
src2.setImage(stabilizedDebug.toJfxImage());

Stats.endTask("frame");

if (++counter % 20 == 0) {
System.out.println(Stats.getStatsAndReset());
counter = 0;
}
} else {
stabilizationErrors++;
logger.warn("Unable to compute a valid stabilization ({} times)", stabilizationErrors);
}
} else {
logger.warn("Unable to compute a valid deperspectivation");
return;
}
if (stabilizationErrors > 20) {
// TODO: clean fields
fields.reset();
stabilizationErrors = 0;
stabilizedImgDescriptor = null;
}
if (stabilizedImgDescriptor == null) {
stabilizedImgDescriptor = new ImgDescriptor(frame, deperspectivGraphy);
return;
}
ImgDescriptor newImgDescriptor = new ImgDescriptor(frame, deperspectivGraphy);
Mat stabilizationHomography = stabilizedImgDescriptor.computeStabilizationGraphy(newImgDescriptor);
if (stabilizationHomography == null) {
stabilizationErrors++;
logger.warn("Unable to compute a valid stabilization ({} times)", stabilizationErrors);
return;
}
Img stabilized = warpPerspective(frame, stabilizationHomography);
Img stabilizedDisplay = new Img(stabilized.getSrc(), true);

if (stabilizationHasChanged) {
Stats.beginTask("stabilizationHasChanged");
stabilized = newImgDescriptor.getDeperspectivedImg();
stabilizedDisplay = new Img(stabilized.getSrc(), true);
Mat fieldsHomography = new Mat();
Core.gemm(deperspectivGraphy, stabilizationHomography.inv(), 1, new Mat(), 0, fieldsHomography);
Stats.beginTask("restabilizeFields");
fields.restabilizeFields(fieldsHomography);
Stats.endTask("restabilizeFields");
stabilizedImgDescriptor = newImgDescriptor;
stabilizationHomography = deperspectivGraphy;
stabilizationHasChanged = false;
Stats.endTask("stabilizationHasChanged");
}
Stats.beginTask("consolidate fields");
fields.consolidate(stabilizedDisplay);
Stats.endTask("consolidate fields");
Img display = new Img(frame, false);
Stats.beginTask("consolidateOcr");
fields.performOcr(stabilized);
Stats.endTask("consolidateOcr");

Img stabilizedDebug = new Img(stabilizedDisplay.getSrc(), true);
fields.drawFieldsOnStabilizedDebug(stabilizedDebug);

fields.drawOcrPerspectiveInverse(display, stabilizationHomography.inv(), 1);
fields.drawFieldsOnStabilized(stabilizedDisplay);

src0.setImage(display.toJfxImage());
src1.setImage(stabilizedDisplay.toJfxImage());
src2.setImage(stabilizedDebug.toJfxImage());

Stats.endTask("frame");

if (++counter % 20 == 0) {
System.out.println(Stats.getStatsAndReset());
counter = 0;
}
} catch (Throwable e) {
logger.warn("Exception while computing layout.", e);
}
Expand Down Expand Up @@ -270,7 +273,11 @@ private static Mat findHomography(Point vp, double width, double height) {
A_ = new Line(C_, bary).intersection(new Line(AB2, rotatedVp));
D_ = new Line(B_, bary).intersection(new Line(CD2, rotatedVp));
}
return Imgproc.getPerspectiveTransform(new MatOfPoint2f(rotate(bary, -alpha, A_, B_, C_, D_)), new MatOfPoint2f(A, B, C, D));
Mat result = Imgproc.getPerspectiveTransform(new MatOfPoint2f(rotate(bary, -alpha, A_, B_, C_, D_)), new MatOfPoint2f(A, B, C, D));
// System.out.println("Determinant vanishing : " + Core.determinant(result));
// if (Core.determinant(result) < 0.3)
// return null;
return result;
}

private static Point[] rotate(Point bary, double alpha, Point... points) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public Mat computeStabilizationGraphy(ImgDescriptor frameDescriptor) {
MATCHER.match(getDescriptors(), frameDescriptor.getDescriptors(), matches);
List<DMatch> goodMatches = new ArrayList<>();
for (DMatch dMatch : matches.toArray()) {
if (dMatch.distance <= 40) {
if (dMatch.distance <= 20) {
goodMatches.add(dMatch);
}
}
Expand All @@ -93,7 +93,19 @@ public Mat computeStabilizationGraphy(ImgDescriptor frameDescriptor) {
Mat goodNewPoints = Converters.vector_Point2f_to_Mat(goodNewKeypoints);
MatOfPoint2f originalNewPoints = new MatOfPoint2f();
Core.perspectiveTransform(goodNewPoints, originalNewPoints, frameDescriptor.getHomography().inv());
return Calib3d.findHomography(originalNewPoints, new MatOfPoint2f(goodOldKeypoints.stream().toArray(Point[]::new)), Calib3d.RANSAC, 5);
Mat result = Calib3d.findHomography(originalNewPoints, new MatOfPoint2f(goodOldKeypoints.stream().toArray(Point[]::new)), Calib3d.RANSAC, 1);
if (result.size().empty()) {
CamLiveRetriever.logger.warn("Stabilization homography is empty");
return null;
}

System.out.println("Determinant Stabilization : " + Core.determinant(result));
double det = Core.determinant(result);
if (det < 0.7 || det > 3) {
CamLiveRetriever.logger.warn("Stabilization homography has wrong determinant : " + det);
return null;
}
return result;
} else {
CamLiveRetriever.logger.warn("Not enough matches ({})", goodMatches.size());
return null;
Expand Down

0 comments on commit e19183c

Please sign in to comment.