-
Notifications
You must be signed in to change notification settings - Fork 5
/
Deperspectiver.java
220 lines (176 loc) · 6.09 KB
/
Deperspectiver.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package org.genericsystem.cv;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.genericsystem.cv.Calibrated.AngleCalibrated;
import org.genericsystem.cv.utils.NativeLibraryLoader;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;
import org.opencv.utils.Converters;
import org.opencv.videoio.VideoCapture;
import javafx.application.Platform;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
public class Deperspectiver 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();;
private AngleCalibrated calibrated0;
private final double f = 6.053 / 0.009;
private boolean stabilizedMode = false;
private boolean textsEnabledMode = false;
private SuperFrameImg superFrame;
@Override
protected void fillGrid(GridPane mainGrid) {
superFrame = SuperFrameImg.create(capture);
Image jfx = superFrame.getDisplay().toJfxImage();
ImageView view00 = new ImageView(jfx);
mainGrid.add(view00, 0, 0);
ImageView view01 = new ImageView(jfx);
mainGrid.add(view01, 0, 1);
ImageView view10 = new ImageView(jfx);
mainGrid.add(view10, 1, 0);
ImageView view11 = new ImageView(jfx);
mainGrid.add(view11, 1, 1);
double[] pp = superFrame.getPrincipalPoint();
calibrated0 = new AngleCalibrated(0, Math.PI / 2);
timer.scheduleAtFixedRate(() -> {
try {
if (!stabilizedMode) {
superFrame = SuperFrameImg.create(capture);
}
Lines lines = superFrame.detectLines();
if (textsEnabledMode)
lines.lines.addAll(TextOrientationLinesDetector.getTextOrientationLines(superFrame));
if (lines.size() > 4) {
superFrame.draw(lines, new Scalar(0, 0, 255), 1);
calibrated0 = superFrame.findVanishingPoint(lines, calibrated0, pp, f);
AngleCalibrated[] calibratedVps = calibrated0.findOtherVps(lines, pp, f);
calibratedVps[0].draw(superFrame, lines, pp, f, new Scalar(0, 255, 0), 1);
calibratedVps[1].draw(superFrame, lines, pp, f, new Scalar(255, 0, 0), 1);
Image displayImage = superFrame.getDisplay().toJfxImage();
Image deperspectivedImage = superFrame.dePerspective(calibratedVps, pp, f).toJfxImage();
Image closed = superFrame.getBinaryClosed30().toJfxImage();
Image diff = superFrame.getDiffFrame().toJfxImage();
Platform.runLater(() -> {
view00.setImage(displayImage);
view01.setImage(deperspectivedImage);
view10.setImage(closed);
view11.setImage(diff);
});
} else
System.out.println("Not enough lines : " + lines.size());
} catch (Throwable e) {
e.printStackTrace();
}
}, 30, 30, TimeUnit.MILLISECONDS);
}
@Override
protected void onT() {
textsEnabledMode = !textsEnabledMode;
}
public static class Lines {
final List<Line> lines;
public Lines(Mat src) {
lines = new ArrayList<>();
for (int i = 0; i < src.rows(); i++) {
double[] val = src.get(i, 0);
Line line = new Line(val[0], val[1], val[2], val[3]);
lines.add(line);
}
}
public Lines filter(Predicate<Line> predicate) {
return new Lines(lines.stream().filter(predicate).collect(Collectors.toList()));
}
public Lines reduce(int max) {
if (lines.size() <= max)
return this;
Set<Line> newLines = new HashSet<>();
while (newLines.size() < max)
newLines.add(lines.get((int) (Math.random() * size())));
return new Lines((newLines));
}
public Lines(Collection<Line> lines) {
this.lines = new ArrayList<>(lines);
}
public Lines rotate(Mat matrix) {
return new Lines(lines.stream().map(line -> line.transform(matrix)).collect(Collectors.toList()));
}
public Lines perspectivTransform(Mat matrix) {
return new Lines(lines.stream().map(line -> line.perspectivTransform(matrix)).collect(Collectors.toList()));
}
public void draw(Mat frame, Scalar color, int thickness) {
lines.forEach(line -> line.draw(frame, color, thickness));
}
public int size() {
return lines.size();
}
}
public static class Line {
final double x1, y1, x2, y2;
public Line(Point p1, Point p2) {
this(p1.x, p1.y, p2.x, p2.y);
}
public Line(double x1, double y1, double x2, double y2) {
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
}
public double size() {
return Math.sqrt(Math.pow(y2 - y1, 2) + Math.pow(x2 - x1, 2));
}
public Line transform(Mat rotationMatrix) {
MatOfPoint2f results = new MatOfPoint2f();
Core.transform(Converters.vector_Point2f_to_Mat(Arrays.asList(new Point(x1, y1), new Point(x2, y2))), results, rotationMatrix);
Point[] targets = results.toArray();
return new Line(targets[0].x, targets[0].y, targets[1].x, targets[1].y);
}
public Line perspectivTransform(Mat homography) {
MatOfPoint2f results = new MatOfPoint2f();
Core.perspectiveTransform(Converters.vector_Point2f_to_Mat(Arrays.asList(new Point(x1, y1), new Point(x2, y2))), results, homography);
Point[] targets = results.toArray();
return new Line(targets[0].x, targets[0].y, targets[1].x, targets[1].y);
}
public void draw(Mat frame, Scalar color, int thickness) {
Imgproc.line(frame, new Point(x1, y1), new Point(x2, y2), color, thickness);
}
}
static class Circle {
public Circle(Point center, float radius) {
this.center = center;
this.radius = radius;
}
Point center;
float radius;
}
@Override
public void stop() throws Exception {
super.stop();
timer.shutdown();
timer.awaitTermination(5000, TimeUnit.MILLISECONDS);
capture.release();
}
@Override
protected void onSpace() {
stabilizedMode = !stabilizedMode;
}
}