-
Notifications
You must be signed in to change notification settings - Fork 5
/
CamLiveRetriever.java
480 lines (411 loc) · 17.4 KB
/
CamLiveRetriever.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
package org.genericsystem.cv.classifier;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.genericsystem.cv.AbstractApp;
import org.genericsystem.cv.Img;
import org.genericsystem.cv.utils.Line;
import org.genericsystem.cv.utils.NativeLibraryLoader;
import org.genericsystem.cv.utils.Ransac;
import org.genericsystem.cv.utils.Ransac.Model;
import org.genericsystem.cv.utils.Tools;
import org.opencv.calib3d.Calib3d;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.DMatch;
import org.opencv.core.KeyPoint;
import org.opencv.core.Mat;
import org.opencv.core.MatOfDMatch;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.core.MatOfPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.features2d.DescriptorExtractor;
import org.opencv.features2d.DescriptorMatcher;
import org.opencv.imgproc.Imgproc;
import org.opencv.utils.Converters;
import org.opencv.videoio.VideoCapture;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
@SuppressWarnings({ "resource" })
public class CamLiveRetriever extends AbstractApp {
static {
NativeLibraryLoader.load();
}
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private static final int OCR_DELAY = 250;
private static final int STABILIZATION_DELAY = 5_000;
private static final int FRAME_DELAY = 15;
private final ScheduledExecutorService timerFields = Executors.newSingleThreadScheduledExecutor();
private final ScheduledExecutorService timerOcr = Executors.newSingleThreadScheduledExecutor();
private final DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.ORB);
private final DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
private final VideoCapture capture = new VideoCapture(0);
private final Fields fields = new Fields();
private MatOfKeyPoint oldKeypoints;
private MatOfKeyPoint newKeypoints;
private Mat oldDescriptors;
private Mat newDescriptors;
private Mat homographyFromStabilized = new Mat(3, 3, CvType.CV_64FC1);
private Mat perspectiveHomography = new Mat(3, 3, CvType.CV_64FC1);
private Mat frame = new Mat();
private Img stabilized;
private boolean stabilizationHasChanged = true;
public static void main(String[] args) {
launch(args);
}
@Override
public void stop() throws Exception {
super.stop();
timerFields.shutdown();
timerFields.awaitTermination(5, TimeUnit.SECONDS);
timerOcr.shutdown();
timerOcr.awaitTermination(5, TimeUnit.SECONDS);
}
@Override
protected void fillGrid(GridPane mainGrid) {
capture.read(frame);
stabilized = new Img(frame, true);
ImageView src0 = new ImageView(Tools.mat2jfxImage(frame));
mainGrid.add(src0, 0, 0);
ImageView src1 = new ImageView(Tools.mat2jfxImage(frame));
mainGrid.add(src1, 1, 0);
oldKeypoints = new MatOfKeyPoint();
oldDescriptors = new Mat();
// Perform the OCR
timerOcr.scheduleWithFixedDelay(() -> consolidateOcr(), 500, OCR_DELAY, TimeUnit.MILLISECONDS);
// Stabilize the image
timerFields.scheduleWithFixedDelay(() -> onSpace(), 0, STABILIZATION_DELAY, TimeUnit.MILLISECONDS);
// Detect the rectangles
timerFields.scheduleWithFixedDelay(() -> {
synchronized (this) {
try {
capture.read(frame);
if (frame == null)
return;
buildStabilizedImg(frame);
if (stabilized != null) {
if (stabilizationHasChanged) {
// Store a copy of the last homographies
Mat lastPerspectiveHomography = perspectiveHomography.clone();
Mat lastHomographyFromStabilized = homographyFromStabilized.clone();
Mat fullHomography = new Mat();
Core.gemm(lastHomographyFromStabilized.inv(), lastPerspectiveHomography, 1, new Mat(), 0, fullHomography); // Inverse homography
// Store the last homographies
if (homographyFromStabilized != null)
fields.storeHomographyFromStabilized(homographyFromStabilized);
if (perspectiveHomography != null)
fields.storePerspectiveHomographyInv(perspectiveHomography.inv());
// Update the keypoints and descriptors
oldKeypoints = newKeypoints;
oldDescriptors = newDescriptors;
// Restabilize the image
buildStabilizedImg(frame);
fields.updateFieldsWithHomography(fullHomography);
// Detect the new fields and merge them
List<Rect> newRects = detectRects(stabilized);
fields.merge(newRects);
stabilizationHasChanged = false;
}
Img display = new Img(frame, true);
Img stabilizedDisplay = new Img(stabilized.getSrc(), true);
fields.drawOcrPerspectiveInverse(display, homographyFromStabilized.inv(), new Scalar(0, 64, 255), 1);
src0.setImage(display.toJfxImage());
src1.setImage(stabilizedDisplay.toJfxImage());
}
} catch (Throwable e) {
logger.warn("Exception while computing layout.", e);
}
}
}, 400, FRAME_DELAY, TimeUnit.MILLISECONDS);
}
private synchronized void consolidateOcr() {
try {
if (stabilized != null)
fields.consolidateOcr(stabilized);
} catch (Throwable e) {
logger.warn("Exception while computing OCR", e);
}
}
@Override
protected synchronized void onSpace() {
stabilizationHasChanged = true;
}
private void buildStabilizedImg(Mat frame) {
Img frameImg = new Img(frame, true);
Img dePerspectived = dePerspectivate(frameImg.getSrc()); // Store homography in perspectiveHomography
newKeypoints = detect(dePerspectived);
newDescriptors = new Mat();
extractor.compute(dePerspectived.getSrc(), newKeypoints, newDescriptors);
Img img = stabilize(frameImg, matcher); // Compute and store homography from stabilized
if (img != null)
stabilized = img;
fields.storeHomographyFromStabilized(homographyFromStabilized);
}
// XXX see whether one want to return null or the original frame
private Img stabilize(Img frame, DescriptorMatcher matcher) {
MatOfDMatch matches = new MatOfDMatch();
if (oldDescriptors != null && !oldDescriptors.empty() && !newDescriptors.empty()) {
Mat stabilizedMat = new Mat(); // frame.getSrc().clone();
matcher.match(oldDescriptors, newDescriptors, matches);
List<DMatch> goodMatches = new ArrayList<>();
for (DMatch dMatch : matches.toArray()) {
if (dMatch.distance <= 40) {
goodMatches.add(dMatch);
}
}
List<KeyPoint> newKeypoints_ = newKeypoints.toList();
List<KeyPoint> oldKeypoints_ = oldKeypoints.toList();
List<Point> goodNewKeypoints = new ArrayList<>();
List<Point> goodOldKeypoints = new ArrayList<>();
for (DMatch goodMatch : goodMatches) {
goodNewKeypoints.add(newKeypoints_.get(goodMatch.trainIdx).pt);
goodOldKeypoints.add(oldKeypoints_.get(goodMatch.queryIdx).pt);
}
if (goodMatches.size() > 30) {
Mat goodNewPoints = Converters.vector_Point2f_to_Mat(goodNewKeypoints);
MatOfPoint2f originalNewPoints = new MatOfPoint2f();
Core.perspectiveTransform(goodNewPoints, originalNewPoints, perspectiveHomography.inv());
homographyFromStabilized = Calib3d.findHomography(originalNewPoints, new MatOfPoint2f(goodOldKeypoints.stream().toArray(Point[]::new)), Calib3d.RANSAC, 10);
Mat tmp = new Mat();
Mat mask = new Mat(frame.size(), CvType.CV_8UC1, new Scalar(255));
Mat maskWarpped = new Mat();
Imgproc.warpPerspective(mask, maskWarpped, homographyFromStabilized, frame.size());
Imgproc.warpPerspective(frame.getSrc(), tmp, homographyFromStabilized, frame.size(), Imgproc.INTER_LINEAR, Core.BORDER_REPLICATE, Scalar.all(255));
tmp.copyTo(stabilizedMat, maskWarpped);
return new Img(stabilizedMat, false);
} else {
System.out.println("Not enough matches (" + goodMatches.size() + ")");
return null;
}
}
System.out.println("No stabilized image");
return null;
}
private List<Rect> detectRects(Img stabilized) {
Img closed = stabilized.adaptativeGaussianInvThreshold(7, 3).morphologyEx(Imgproc.MORPH_CLOSE, Imgproc.MORPH_RECT, new Size(9, 1));
List<MatOfPoint> contours = new ArrayList<>();
Imgproc.findContours(closed.getSrc(), contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
double minArea = 200;
List<Rect> res = contours.stream().filter(contour -> Imgproc.contourArea(contour) > minArea).map(c -> Imgproc.boundingRect(c)).collect(Collectors.toList());
// res.forEach(rect -> Imgproc.rectangle(stabilized.getSrc(), rect.tl(), rect.br(), new Scalar(255, 200, 0)));
return res;
}
private MatOfKeyPoint detect(Img frame) {
Img closed = frame.adaptativeGaussianInvThreshold(17, 3).morphologyEx(Imgproc.MORPH_CLOSE, Imgproc.MORPH_ELLIPSE, new Size(5, 5));
List<MatOfPoint> contours = new ArrayList<>();
Imgproc.findContours(closed.getSrc(), contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
double minArea = 100;
List<KeyPoint> keyPoints = new ArrayList<>();
contours.stream().filter(contour -> Imgproc.contourArea(contour) > minArea).map(Imgproc::boundingRect).forEach(rect -> {
keyPoints.add(new KeyPoint((float) rect.tl().x, (float) rect.tl().y, 6));
keyPoints.add(new KeyPoint((float) rect.tl().x, (float) rect.br().y, 6));
keyPoints.add(new KeyPoint((float) rect.br().x, (float) rect.tl().y, 6));
keyPoints.add(new KeyPoint((float) rect.br().x, (float) rect.br().y, 6));
});
return new MatOfKeyPoint(keyPoints.stream().toArray(KeyPoint[]::new));
}
private Img dePerspectivate(Mat frame) {
Lines lines = getLines(frame);
if (lines.size() > 10) {
perspectiveHomography = computeHomography(frame, lines);
Mat dePerspectived = new Mat(frame.size(), CvType.CV_8UC3, Scalar.all(255));
Mat tmp = new Mat();
Mat mask = new Mat(frame.size(), CvType.CV_8UC1, new Scalar(255));
Mat maskWarpped = new Mat();
Imgproc.warpPerspective(mask, maskWarpped, perspectiveHomography, frame.size());
Imgproc.warpPerspective(frame, tmp, perspectiveHomography, frame.size(), Imgproc.INTER_LINEAR, Core.BORDER_REPLICATE, Scalar.all(255));
tmp.copyTo(dePerspectived, maskWarpped);
return new Img(dePerspectived, false);
} else {
logger.warn("Not enough lines to compute perspective transform");
return null;
}
}
private Lines getLines(Mat frame) {
Img grad = new Img(frame, false).morphologyEx(Imgproc.MORPH_GRADIENT, Imgproc.MORPH_RECT, new Size(2, 2)).otsu();
return new Lines(grad.houghLinesP(1, Math.PI / 180, 10, 100, 10));
}
private Mat computeHomography(Mat frame, Lines lines) {
Ransac<Line> ransac = lines.vanishingPointRansac(frame.width(), frame.height());
Mat vpMat = (Mat) ransac.getBestModel().getParams()[0];
Point vp = new Point(vpMat.get(0, 0)[0], vpMat.get(1, 0)[0]);
Point bary = new Point(frame.width() / 2, frame.height() / 2);
return findHomography(vp, bary, frame.width(), frame.height());
}
private Mat findHomography(Point vp, Point bary, double width, double height) {
double alpha_ = Math.atan2((vp.y - bary.y), (vp.x - bary.x));
if (alpha_ < -Math.PI / 2 && alpha_ > -Math.PI)
alpha_ = alpha_ + Math.PI;
if (alpha_ < Math.PI && alpha_ > Math.PI / 2)
alpha_ = alpha_ - Math.PI;
double alpha = alpha_;
Point rotatedVp = rotate(bary, alpha, vp)[0];
Point A = new Point(0, 0);
Point B = new Point(width, 0);
Point C = new Point(width, height);
Point D = new Point(0, height);
Point AB2 = new Point(width / 2, 0);
Point CD2 = new Point(width / 2, height);
Point A_, B_, C_, D_;
if (rotatedVp.x >= width / 2) {
A_ = new Line(AB2, rotatedVp).intersection(0);
D_ = new Line(CD2, rotatedVp).intersection(0);
C_ = new Line(A_, bary).intersection(new Line(CD2, rotatedVp));
B_ = new Line(D_, bary).intersection(new Line(AB2, rotatedVp));
} else {
B_ = new Line(AB2, rotatedVp).intersection(width);
C_ = new Line(CD2, rotatedVp).intersection(width);
A_ = new Line(C_, bary).intersection(new Line(AB2, rotatedVp));
D_ = new Line(B_, bary).intersection(new Line(CD2, rotatedVp));
}
// System.out.println("vp : " + vp);
// System.out.println("rotated vp : " + rotatedVp);
// System.out.println("Alpha : " + alpha * 180 / Math.PI);
// System.out.println("A : " + A + " " + A_);
// System.out.println("B : " + B + " " + B_);
// System.out.println("C : " + C + " " + C_);
// System.out.println("D : " + D + " " + D_);
Mat src = new MatOfPoint2f(rotate(bary, -alpha, A_, B_, C_, D_));
Mat dst = new MatOfPoint2f(A, B, C, D);
return Imgproc.getPerspectiveTransform(src, dst);
}
private Point[] rotate(Point bary, double alpha, Point... p) {
Mat matrix = Imgproc.getRotationMatrix2D(bary, alpha / Math.PI * 180, 1);
MatOfPoint2f points = new MatOfPoint2f(p);
MatOfPoint2f results = new MatOfPoint2f();
Core.transform(points, results, matrix);
return results.toArray();
}
public static class Lines extends org.genericsystem.cv.utils.Lines {
private static Mat K;
public Lines(Mat src) {
super(src);
}
public Lines(Collection<Line> lines) {
super(lines);
}
public static Lines of(Collection<Line> lines) {
return new Lines(lines);
}
private Mat getLineMat(Line line) {
Mat a = new Mat(3, 1, CvType.CV_32F);
Mat b = new Mat(3, 1, CvType.CV_32F);
a.put(0, 0, new float[] { Double.valueOf(line.getX1()).floatValue() });
a.put(1, 0, new float[] { Double.valueOf(line.getY1()).floatValue() });
a.put(2, 0, new float[] { Double.valueOf(1d).floatValue() });
b.put(0, 0, new float[] { Double.valueOf(line.getX2()).floatValue() });
b.put(1, 0, new float[] { Double.valueOf(line.getY2()).floatValue() });
b.put(2, 0, new float[] { Double.valueOf(1d).floatValue() });
Mat an = new Mat(3, 1, CvType.CV_32F);
Mat bn = new Mat(3, 1, CvType.CV_32F);
Core.gemm(K.inv(), a, 1, new Mat(), 0, an);
Core.gemm(K.inv(), b, 1, new Mat(), 0, bn);
Mat li = an.cross(bn);
Core.normalize(li, li);
return li;
}
public Ransac<Line> vanishingPointRansac(int width, int height) {
int minimal_sample_set_dimension = 2;
double maxError = (float) 0.01623 * 2;
if (K == null) {
K = new Mat(3, 3, CvType.CV_32F, new Scalar(0));
K.put(0, 0, new float[] { width });
K.put(0, 2, new float[] { width / 2 });
K.put(1, 1, new float[] { height });
K.put(1, 2, new float[] { height / 2 });
K.put(2, 2, new float[] { 1 });
}
return new Ransac<>(getLines(), getModelProvider(minimal_sample_set_dimension, maxError), minimal_sample_set_dimension, 200, maxError, Double.valueOf(Math.floor(this.size() * 0.7)).intValue());
}
private Function<Collection<Line>, Model<Line>> getModelProvider(int minimal_sample_set_dimension, double maxError) {
return datas -> {
Mat vp;
if (datas.size() == minimal_sample_set_dimension) {
Iterator<Line> it = datas.iterator();
vp = getLineMat(it.next()).cross(getLineMat(it.next()));
Core.normalize(vp, vp);
} else {
// Extract the line segments corresponding to the indexes contained in the set
Mat li_set = new Mat(3, datas.size(), CvType.CV_32F);
Mat tau = new Mat(datas.size(), datas.size(), CvType.CV_32F, new Scalar(0, 0, 0));
int i = 0;
for (Line line : datas) {
Mat li = getLineMat(line);
li_set.put(0, i, li.get(0, 0));
li_set.put(1, i, li.get(1, 0));
li_set.put(2, i, li.get(2, 0));
tau.put(i, i, line.size());
i++;
}
// Least squares solution
// Generate the matrix ATA (from LSS_set=A)
Mat L = li_set.t();
Mat ATA = new Mat(3, 3, CvType.CV_32F);
Mat dst = new Mat();
Core.gemm(L.t(), tau.t(), 1, new Mat(), 0, dst);
Core.gemm(dst, tau, 1, new Mat(), 0, dst);
Core.gemm(dst, L, 1, new Mat(), 0, ATA);
// Obtain eigendecomposition
Mat v = new Mat();
Core.SVDecomp(ATA, new Mat(), v, new Mat());
// Check eigenvecs after SVDecomp
if (v.rows() < 3)
throw new IllegalStateException();
// Assign the result (the last column of v, corresponding to the eigenvector with lowest eigenvalue)
vp = new Mat(3, 1, CvType.CV_32F);
vp.put(0, 0, v.get(0, 2));
vp.put(1, 0, v.get(1, 2));
vp.put(2, 0, v.get(2, 2));
Core.normalize(vp, vp);
Core.gemm(K, vp, 1, new Mat(), 0, vp);
if (vp.get(2, 0)[0] != 0) {
vp.put(0, 0, new float[] { Double.valueOf(vp.get(0, 0)[0] / vp.get(2, 0)[0]).floatValue() });
vp.put(1, 0, new float[] { Double.valueOf(vp.get(1, 0)[0] / vp.get(2, 0)[0]).floatValue() });
vp.put(2, 0, new float[] { 1 });
} else {
// Since this is infinite, it is better to leave it calibrated
Core.gemm(K.inv(), vp, 1, new Mat(), 0, vp);
}
}
return new Model<Line>() {
@Override
public double computeError(Line line) {
Mat lineMat = getLineMat(line);
double di = vp.dot(lineMat);
di /= (Core.norm(vp) * Core.norm(lineMat));
return di * di;
}
@Override
public double computeGlobalError(List<Line> datas, Collection<Line> consensusDatas) {
double globalError = 0;
for (Line line : datas) {
double error = computeError(line);
if (error > maxError)
error = maxError;
globalError += error;
}
globalError = globalError / datas.size();
return globalError;
}
@Override
public Object[] getParams() {
return new Object[] { vp };
}
};
};
}
}
}