Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nfeybesse committed Feb 12, 2018
1 parent fe709b4 commit f0261d9
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 58 deletions.
Expand Up @@ -186,15 +186,15 @@ private Image[] doWork() {
SuperTemplate superReferenceTemplate4 = new SuperTemplate(superDeperspectived, CvType.CV_8UC3, SuperFrameImg::getFrame);
List<SuperContour> horizontals = detectedSuperContoursCopy.stream().filter(SuperTemplate.VERTICAL_FILTER).collect(Collectors.toList());
horizontals.stream().forEach(c -> Imgproc.line(superReferenceTemplate4.getDisplay().getSrc(), c.left, c.right, new Scalar(255, 255, 255), 1));
// horizontals.stream().map(sc -> sc.center).forEach(pt -> Imgproc.circle(superReferenceTemplate4.getDisplay().getSrc(), pt, 3, new Scalar(255, 0, 0), -1));
// horizontals.stream().map(sc -> sc.left).forEach(pt -> Imgproc.circle(superReferenceTemplate4.getDisplay().getSrc(), pt, 3, new Scalar(0, 255, 0), -1));
// horizontals.stream().map(sc -> sc.right).forEach(pt -> Imgproc.circle(superReferenceTemplate4.getDisplay().getSrc(), pt, 3, new Scalar(0, 0, 255), -1));
horizontals.stream().map(sc -> sc.center).forEach(pt -> Imgproc.circle(superReferenceTemplate4.getDisplay().getSrc(), pt, 3, new Scalar(255, 0, 0), -1));
horizontals.stream().map(sc -> sc.left).forEach(pt -> Imgproc.circle(superReferenceTemplate4.getDisplay().getSrc(), pt, 3, new Scalar(0, 255, 0), -1));
horizontals.stream().map(sc -> sc.right).forEach(pt -> Imgproc.circle(superReferenceTemplate4.getDisplay().getSrc(), pt, 3, new Scalar(0, 0, 255), -1));

List<SuperContour> verticals = detectedSuperContoursCopy = detectedSuperContoursCopy.stream().filter(SuperTemplate.HORIZONTAL_FILTER).collect(Collectors.toList());
List<SuperContour> verticals = detectedSuperContoursCopy.stream().filter(SuperTemplate.HORIZONTAL_FILTER).collect(Collectors.toList());
verticals.stream().forEach(c -> Imgproc.line(superReferenceTemplate4.getDisplay().getSrc(), c.left, c.right, new Scalar(255, 255, 255), 1));
// verticals.stream().map(sc -> sc.center).forEach(pt -> Imgproc.circle(superReferenceTemplate4.getDisplay().getSrc(), pt, 3, new Scalar(255, 0, 0), -1));
// verticals.stream().map(sc -> sc.left).forEach(pt -> Imgproc.circle(superReferenceTemplate4.getDisplay().getSrc(), pt, 3, new Scalar(0, 255, 0), -1));
// verticals.stream().map(sc -> sc.right).forEach(pt -> Imgproc.circle(superReferenceTemplate4.getDisplay().getSrc(), pt, 3, new Scalar(0, 0, 255), -1));
verticals.stream().map(sc -> sc.center).forEach(pt -> Imgproc.circle(superReferenceTemplate4.getDisplay().getSrc(), pt, 3, new Scalar(255, 0, 0), -1));
verticals.stream().map(sc -> sc.left).forEach(pt -> Imgproc.circle(superReferenceTemplate4.getDisplay().getSrc(), pt, 3, new Scalar(0, 255, 0), -1));
verticals.stream().map(sc -> sc.right).forEach(pt -> Imgproc.circle(superReferenceTemplate4.getDisplay().getSrc(), pt, 3, new Scalar(0, 0, 255), -1));

images[8] = superReferenceTemplate4.getDisplay().toJfxImage();

Expand Down
@@ -1,5 +1,13 @@
package org.genericsystem.cv.application;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Size;
import org.opencv.utils.Converters;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
Expand All @@ -10,14 +18,6 @@
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Size;
import org.opencv.utils.Converters;

public class ReferenceManager {
private static final Mat IDENTITY_MAT = Mat.eye(new Size(3, 3), CvType.CV_64F);

Expand Down Expand Up @@ -62,7 +62,7 @@ public void submit(ImgDescriptor newImgDescriptor, List<Rect> detectedrects) {
bestImgDescriptor = reference;
} else {
int reconciliationTries = 0;
List<ImgDescriptor> list = getRandomPool(lastStored, reference);
List<ImgDescriptor> list = getRandomPool(lastStored, reference);
Random randomGenerator = new Random();
while (!list.isEmpty() && reconciliationTries < 5) {
ImgDescriptor randomImgDescriptor = list.get(randomGenerator.nextInt(list.size()));
Expand Down Expand Up @@ -139,23 +139,23 @@ private void updateReference() {
}

private ImgDescriptor findConsensualDescriptor() {
// double bestDistance = Double.MAX_VALUE;
// ImgDescriptor bestDescriptor = null;
// for (Entry<ImgDescriptor, Mat> entry : toReferenceGraphy.entrySet()) {
// double distance = 0;
// for (Entry<ImgDescriptor, Mat> entry2 : toReferenceGraphy.entrySet()) {
// if (!entry.getKey().equals(entry2.getKey())) {
// Mat betweenHomography = new Mat();
// Core.gemm(entry.getValue(), entry2.getValue().inv(), 1, new Mat(), 0, betweenHomography);
// distance += distance(betweenHomography);
// }
// }
// if (distance < bestDistance) {
// bestDistance = distance;
// bestDescriptor = entry.getKey();
// }
// }
// return bestDescriptor;
// double bestDistance = Double.MAX_VALUE;
// ImgDescriptor bestDescriptor = null;
// for (Entry<ImgDescriptor, Mat> entry : toReferenceGraphy.entrySet()) {
// double distance = 0;
// for (Entry<ImgDescriptor, Mat> entry2 : toReferenceGraphy.entrySet()) {
// if (!entry.getKey().equals(entry2.getKey())) {
// Mat betweenHomography = new Mat();
// Core.gemm(entry.getValue(), entry2.getValue().inv(), 1, new Mat(), 0, betweenHomography);
// distance += distance(betweenHomography);
// }
// }
// if (distance < bestDistance) {
// bestDistance = distance;
// bestDescriptor = entry.getKey();
// }
// }
// return bestDescriptor;

double minArea = Double.MAX_VALUE;
ImgDescriptor bestDescriptor = null;
Expand Down Expand Up @@ -187,7 +187,6 @@ public Rect rescale(Rect rect, double ratio) {
return new Rect((int) (rect.x * ratio), (int) (rect.y * ratio), (int) (rect.width * ratio), (int) (rect.height * ratio));
}


private List<Rect> transpose(List<Rect> rects, double minX, double minY) {
return rects.stream().map(r -> transpose(r, minX, minY)).collect(Collectors.toList());
}
Expand Down
Expand Up @@ -264,7 +264,7 @@ public Img getGrayFrame() {

private Img buildDiffFrame() {
Mat diffFrame = getGrayFrame().gaussianBlur(new Size(5, 5)).getSrc();
Core.absdiff(diffFrame, new Scalar(0), diffFrame);
Core.absdiff(diffFrame, new Scalar(150), diffFrame);
Imgproc.adaptiveThreshold(diffFrame, diffFrame, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY_INV, 7, 3);
return new Img(diffFrame, false);// .cleanTablesInv(0.05);
}
Expand Down
@@ -1,12 +1,5 @@
package org.genericsystem.cv.application;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.BiFunction;

import org.genericsystem.cv.Img;
import org.genericsystem.cv.Lines.Line;
import org.genericsystem.cv.lm.LevenbergImpl;
Expand All @@ -21,49 +14,82 @@
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.BiFunction;

public class TextOrientationLinesDetector {

static List<Line> getTextOrientationLines(SuperFrameImg superFrame) {
List<Circle> circles = detectCircles(superFrame, 30, 100);
Collection<Circle> selectedCircles = selectRandomCirles(circles, 20);
List<Circle> circles = detectCircles(superFrame.getDiffFrame(), 50, 30, 100);
Collection<Circle> selectedCircles = selectRandomObjects(circles, 30);
List<Line> result = new ArrayList<>();
for (Circle circle : selectedCircles) {
Img circledImg = getCircledImg(superFrame, (int) circle.radius, circle.center);
double angle = getBestAngle(circledImg, 42, 12, 5, 192, null) / 180 * Math.PI;
result.add(buildLine(circle.center, angle, circle.radius));
Imgproc.circle(superFrame.getDisplay().getSrc(), circle.center, (int) circle.radius, new Scalar(0, 255, 0), 1);
}

// List<Rect> rects = detectRects(superFrame.getDiffFrame(), 50, 60, 200, 10, 50);
// Collection<Rect> selectedRects = selectRandomObjects(rects, 30);
// List<Line> result2 = new ArrayList<>();
// for (Rect rect : selectedRects) {
// Img rectImg = getRectImg(superFrame, rect);
// double angle = getBestAngle(rectImg, 42, 12, 5, 192, null) / 180 * Math.PI;
// result2.add(buildLine(new Point(rect.tl().x + rect.width / 2, rect.tl().y + rect.height / 2), angle, rect.width / 2));
// Imgproc.rectangle(superFrame.getDisplay().getSrc(), rect.tl(), rect.br(), new Scalar(0, 0, 255), 1);
// }
// return result2;
return result;
}

private static List<Circle> detectCircles(SuperFrameImg superFrame, int minRadius, int maxRadius) {
private static List<MatOfPoint> getContours(Img img) {
List<MatOfPoint> contours = new ArrayList<>();
Imgproc.findContours(superFrame.getDiffFrame().getSrc(), contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
Imgproc.findContours(img.getSrc(), contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
return contours;
}

private static List<Circle> detectCircles(Img img, int maxContourArea, int minRadius, int maxRadius) {
List<MatOfPoint> contours = getContours(img);
List<Circle> circles = new ArrayList<>();
for (int i = 0; i < contours.size(); i++) {
MatOfPoint contour = contours.get(i);
for (MatOfPoint contour : contours) {
double contourarea = Imgproc.contourArea(contour);
if (contourarea > 50) {
float[] radius = new float[1];
Point center = new Point();
MatOfPoint2f contour2F = new MatOfPoint2f(contour.toArray());
Imgproc.minEnclosingCircle(contour2F, center, radius);
if (radius[0] > minRadius && radius[0] < maxRadius && center.x > radius[0] && center.y > radius[0] && ((center.x + radius[0]) < superFrame.width()) && ((center.y + radius[0]) < superFrame.height())) {
if (radius[0] > minRadius && radius[0] < maxRadius && center.x > radius[0] && center.y > radius[0] && ((center.x + radius[0]) < img.width()) && ((center.y + radius[0]) < img.height()))
circles.add(new Circle(center, radius[0]));
// Imgproc.circle(frame, center, (int) radius[0], new Scalar(0, 0, 255));
}
// Imgproc.drawContours(superFrame.getDisplay().getSrc(), Arrays.asList(contour), 0, new Scalar(0, 0, 255), 1);
}
}
return circles;
}

private static Collection<Circle> selectRandomCirles(List<Circle> circles, int circlesNumber) {
if (circles.size() <= circlesNumber)
return circles;
Set<Circle> result = new HashSet<>();
while (result.size() < circlesNumber)
result.add(circles.get((int) (Math.random() * circles.size())));
private static List<Rect> detectRects(Img img, int maxContourArea, int minWidth, int maxWidth, int minHeight, int maxHeight) {
List<MatOfPoint> contours = getContours(img);
List<Rect> rects = new ArrayList<>();
for (MatOfPoint contour : contours) {
double contourarea = Imgproc.contourArea(contour);
if (contourarea > 50) {
Rect rect = Imgproc.boundingRect(contour);
if (rect.width >= minWidth && rect.width <= maxWidth && rect.height >= minHeight && rect.height <= maxHeight)
rects.add(rect);
}
}
return rects;
}

private static <T> Collection<T> selectRandomObjects(List<T> objects, int maxReturnObjects) {
if (objects.size() <= maxReturnObjects)
return objects;
Set<T> result = new HashSet<>();
while (result.size() < maxReturnObjects)
result.add(objects.get((int) (Math.random() * objects.size())));
return result;
}

Expand All @@ -78,6 +104,10 @@ private static Img getCircledImg(SuperFrameImg superFrame, int radius, Point cen
return circledImg;
}

private static Img getRectImg(SuperFrameImg superFrame, Rect rect) {
return new Img(new Mat(superFrame.getBinarized().getSrc(), rect), false);
}

private static Line buildLine(Point center, double angle, double size) {
double x1 = center.x - Math.sin(angle) * size;
double y1 = center.y + Math.cos(angle) * size;
Expand Down

0 comments on commit f0261d9

Please sign in to comment.