-
Notifications
You must be signed in to change notification settings - Fork 5
/
LayoutComparator.java
131 lines (115 loc) · 5.21 KB
/
LayoutComparator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package org.genericsystem.layout;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.genericsystem.cv.AbstractApp;
import org.genericsystem.cv.Img;
import org.genericsystem.cv.Tools;
import org.genericsystem.cv.utils.NativeLibraryLoader;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import org.opencv.videoio.VideoCapture;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
public class LayoutComparator extends AbstractApp {
static {
NativeLibraryLoader.load();
}
public static void main(String[] args) {
launch(args);
}
private final VideoCapture capture = new VideoCapture(0);
private ScheduledExecutorService timer = Executors.newSingleThreadScheduledExecutor();
@Override
protected void fillGrid(GridPane mainGrid) {
Mat frame = new Mat();
capture.read(frame);
ImageView src = new ImageView(Tools.mat2jfxImage(frame));
mainGrid.add(src, 0, 0);
ImageView src1 = new ImageView(Tools.mat2jfxImage(frame));
mainGrid.add(src1, 1, 0);
ImageView src2 = new ImageView(Tools.mat2jfxImage(frame));
mainGrid.add(src2, 2, 0);
ImageView src3 = new ImageView(Tools.mat2jfxImage(frame));
mainGrid.add(src3, 3, 0);
ImageView src4 = new ImageView(Tools.mat2jfxImage(frame));
mainGrid.add(src4, 4, 0);
ImageView src5 = new ImageView(Tools.mat2jfxImage(frame));
mainGrid.add(src5, 0, 1);
ImageView src6 = new ImageView(Tools.mat2jfxImage(frame));
mainGrid.add(src6, 1, 1);
ImageView src7 = new ImageView(Tools.mat2jfxImage(frame));
mainGrid.add(src7, 2, 1);
ImageView src8 = new ImageView(Tools.mat2jfxImage(frame));
mainGrid.add(src8, 3, 1);
ImageView src9 = new ImageView(Tools.mat2jfxImage(frame));
mainGrid.add(src9, 4, 1);
timer.scheduleAtFixedRate(() -> {
try {
capture.read(frame);
Img frameImg = new Img(frame, false);
Img img0 = frameImg.bilateralFilter(10, 80, 80).bgr2Gray().adaptativeThresHold(255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY, 13, 3).bitwise_not().morphologyEx(Imgproc.MORPH_CLOSE, Imgproc.MORPH_ELLIPSE, new Size(11, 5));
Img img1 = frameImg.canny(60, 180).morphologyEx(Imgproc.MORPH_CLOSE, Imgproc.MORPH_ELLIPSE, new Size(11, 5));
Img img2 = frameImg.bgr2Gray().grad(2.0d, 2.0d).thresHold(0, 255, Imgproc.THRESH_BINARY_INV + Imgproc.THRESH_OTSU).bitwise_not().morphologyEx(Imgproc.MORPH_CLOSE, Imgproc.MORPH_ELLIPSE, new Size(11, 3));
Img img3 = frameImg.sauvolaThreshold().morphologyEx(Imgproc.MORPH_CLOSE, Imgproc.MORPH_ELLIPSE, new Size(11, 3));
Img img4 = frameImg.bgr2Gray().gaussianBlur(new Size(3, 3)).absDiff(new Scalar(255.0)).adaptativeThresHold(255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY, 11, 5).bitwise_not().morphologyEx(Imgproc.MORPH_CLOSE,
Imgproc.MORPH_ELLIPSE, new Size(11, 3));
// Layout layout = img0.buildLayout();
// Layout layout1 = img1.buildLayout();
// Layout layout2 = img2.buildLayout();
// Layout layout3 = img3.buildLayout();
// Layout layout4 = img4.buildLayout();
//
// Img out = new Img(frame, true);
// layout.draw(img0, new Scalar(0), 1);
// Img out1 = new Img(frame, true);
// layout1.draw(img1, new Scalar(0), 1);
// Img out2 = new Img(frame, true);
// layout2.draw(img2, new Scalar(0), 1);
// Img out3 = new Img(frame, true);
// layout3.draw(img3, new Scalar(0), 1);
// Img out4 = new Img(frame, true);
// layout4.draw(img4, new Scalar(0), 1);
src.setImage(img0.toJfxImage());
src1.setImage(img1.toJfxImage());
src2.setImage(img2.toJfxImage());
src3.setImage(img3.toJfxImage());
src4.setImage(img4.toJfxImage());
src5.setImage(detectContours(frameImg, img0).toJfxImage());
src6.setImage(detectContours(frameImg, img1).toJfxImage());
src7.setImage(detectContours(frameImg, img2).toJfxImage());
src8.setImage(detectContours(frameImg, img3).toJfxImage());
src9.setImage(detectContours(frameImg, img4).toJfxImage());
} catch (Throwable t) {
t.printStackTrace();
}
}, 400, 10, TimeUnit.MILLISECONDS);
}
Img detectContours(Img frame, Img binary) {
List<MatOfPoint> contours = new ArrayList<>();
Imgproc.findContours(binary.getSrc(), contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
double minArea = 10;
Img result = new Img(frame.getSrc(), true);
contours.stream().filter(contour -> Imgproc.contourArea(contour) > minArea).peek(c -> Imgproc.drawContours(result.getSrc(), Arrays.asList(c), 0, new Scalar(0, 255, 0))).peek(c -> {
Point[] pts = new Point[4];
Imgproc.minAreaRect(new MatOfPoint2f(c.toArray())).points(pts);
Imgproc.drawContours(result.getSrc(), Arrays.asList(new MatOfPoint(pts)), 0, new Scalar(255, 0, 0), 2);
}).count();
/* .map(Imgproc::boundingRect).forEach(rect -> Imgproc.rectangle(result.getSrc(), rect.br(), rect.tl(), new Scalar(0, 0, 255), 1)) */;
return result;
}
@Override
public void stop() throws Exception {
timer.shutdown();
capture.release();
super.stop();
}
}