Skip to content

Commit

Permalink
gs-cv: Deskewer: added safety checks to prevent throwing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
plassalas committed Oct 19, 2017
1 parent 82f4c5f commit 4413fbb
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions gs-cv/src/main/java/org/genericsystem/cv/utils/Deskewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,12 @@ private static void drawSingleLine(Mat mat, final Line line, final Scalar scalar
line.draw(mat, scalar, thickness);
}

private static Img deskew(final Img img, METHOD method) {
public static Img deskew(final Img img, METHOD method) {
final Img closed = getClosedImg(img);
final double angle = detectAngle(closed.getSrc(), method);
logger.debug("Deskew angle = {}", angle);
logger.trace("Deskew angle = {}", angle);
if (Double.isNaN(angle))
return img;

final Point center = new Point(img.width() / 2, img.height() / 2);
// Rotation matrix
Expand Down Expand Up @@ -226,7 +228,7 @@ private static double detectAngle(final Mat dilated, METHOD method) {
rotatedRect.size.height = tmp;
}
}
return getRansacInliersRects(rotatedRects, ransacError).stream().mapToDouble(i -> i.angle).average().getAsDouble();
return getRansacInliersRects(rotatedRects, ransacError).stream().mapToDouble(i -> i.angle).average().orElse(rotatedRects.stream().mapToDouble(r -> r.angle).average().getAsDouble());
}
}

Expand All @@ -250,6 +252,13 @@ private static List<RotatedRect> getRansacInliersRects(final List<RotatedRect> d
int k = 50; // number of iterations
double t = error; // error margin
int d = data.size() * 2 / 3; // number of minimum matches
if (d < n) {
if (d >= n - 1)
n = 2;
else
return data;
}

Map<Integer, RotatedRect> bestFit = new HashMap<>();
for (int i = 1, maxAttempts = 10; bestFit.size() <= 3 && i <= maxAttempts; ++i) {
Ransac<RotatedRect> ransac = new Ransac<>(data, getModelProviderRects(), n, k * i, t, d);
Expand All @@ -270,6 +279,12 @@ private static Lines getRansacInliersLines(final Lines data, final double error)
int k = 50; // number of iterations
double t = error; // error margin
int d = data.size() * 2 / 3; // number of minimum matches
if (d < n) {
if (d >= n - 1)
n = 2;
else
return data;
}

Map<Integer, Line> bestFit = new HashMap<>();
for (int i = 1, maxAttempts = 10; bestFit.size() <= 3 && i <= maxAttempts; ++i) {
Expand Down

0 comments on commit 4413fbb

Please sign in to comment.