-
Notifications
You must be signed in to change notification settings - Fork 5
/
RadonTransformDemo.java
178 lines (148 loc) · 6.1 KB
/
RadonTransformDemo.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
package org.genericsystem.cv.application;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import org.genericsystem.cv.AbstractApp;
import org.genericsystem.cv.Img;
import org.genericsystem.cv.lm.LevenbergImpl;
import org.genericsystem.cv.utils.NativeLibraryLoader;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import javafx.application.Platform;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
public class RadonTransformDemo extends AbstractApp {
public static void main(String[] args) {
launch(args);
}
static {
NativeLibraryLoader.load();
}
private final double f = 6.053 / 0.009;
private final GSCapture gsCapture = new GSVideoCapture(0, f, GSVideoCapture.HD, GSVideoCapture.VGA);
private SuperFrameImg superFrame = gsCapture.read();
private ScheduledExecutorService timer = new BoundedScheduledThreadPoolExecutor();
private Config config = new Config();
private final ImageView[][] imageViews = new ImageView[][] { new ImageView[3], new ImageView[3], new ImageView[3], new ImageView[3] };
private void startTimer() {
timer.scheduleAtFixedRate(() -> {
try {
Image[] images = doWork();
if (images != null)
Platform.runLater(() -> {
Iterator<Image> it = Arrays.asList(images).iterator();
for (int row = 0; row < imageViews.length; row++)
for (int col = 0; col < imageViews[row].length; col++)
if (it.hasNext())
imageViews[row][col].setImage(it.next());
});
} catch (Throwable e) {
e.printStackTrace();
}
}, 30, 30, TimeUnit.MILLISECONDS);
}
@Override
protected void fillGrid(GridPane mainGrid) {
double displaySizeReduction = 1.5;
for (int col = 0; col < imageViews.length; col++)
for (int row = 0; row < imageViews[col].length; row++) {
ImageView imageView = new ImageView();
imageViews[col][row] = imageView;
mainGrid.add(imageViews[col][row], col, row);
imageView.setFitWidth(superFrame.width() / displaySizeReduction);
imageView.setFitHeight(superFrame.height() / displaySizeReduction);
}
startTimer();
}
private Image[] doWork() {
System.out.println("do work");
if (!config.stabilizedMode)
superFrame = gsCapture.read();
Image[] images = new Image[8];
Img binarized = superFrame.getFrame().gaussianBlur(new Size(3, 3)).adaptativeGaussianInvThreshold(3, 2);
Rect roi = new Rect(new Point(300, 0), new Point(360, 360));
Img display = new Img(binarized.getSrc(), true);
Imgproc.rectangle(display.getSrc(), roi.br(), roi.tl(), new Scalar(255));
images[0] = display.toJfxImage();
Img strip = new Img(new Mat(binarized.getSrc(), roi));
// System.out.println(strip.getSrc());
Mat mat = Mat.zeros(360, 640, CvType.CV_8UC1);
strip.getSrc().copyTo(new Mat(mat, roi));
// System.out.println(mat);
images[1] = new Img(mat, false).toJfxImage();
Mat radon = RadonTransform.transform(strip.getSrc(), 45);
images[2] = new Img(radon, false).toJfxImage();
Mat projectionMap = RadonTransform.projectionMap(radon);
// Mat projectionMapLarge = Mat.zeros(360, 640, CvType.CV_64FC1);
// projectionMap.copyTo(new Mat(projectionMapLarge, new Rect(new Point(0, 0), new Point(projectionMap.width(), projectionMap.height()))));
images[3] = new Img(projectionMap, false).toJfxImage();
// Imgproc.Sobel(projectionMap, projectionMap, CvType.CV_64FC1, 0, 1);
// DirectionalFilter.cleanContour(projectionMap);
Imgproc.morphologyEx(projectionMap, projectionMap, Imgproc.MORPH_GRADIENT, Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(1, 2)));
// Imgproc.GaussianBlur(projectionMap, projectionMap, new Size(21, 21), 0);
// Imgproc.threshold(projectionMap, projectionMap, 50, 255, Imgproc.THRESH_TOZERO);
images[4] = new Img(projectionMap, false).toJfxImage();
int[] traj = RadonTransform.bestTraject(projectionMap, -5000, 2);
BiFunction<Double, double[], Double> f = (x, params) -> params[0] + params[1] * x + params[2] * x * x;
BiFunction<double[], double[], Double> error = (xy, params) -> f.apply(xy[0], params) - xy[1];
List<double[]> values = new ArrayList<>();
for (int k = 0; k < traj.length; k++)
values.add(new double[] { k, traj[k] });
double[] params = LevenbergImpl.fromBiFunction(f, values, new double[] { 0, 0, 0 }).getParams();
// System.out.println(Arrays.toString(traj));
Mat trajs = Mat.zeros(projectionMap.size(), CvType.CV_8UC3);
// Mat lines = Mat.zeros(superFrame.getFrame().size(), CvType.CV_8UC3);
for (int k = 0; k < trajs.rows(); k++) {
trajs.put(k, traj[k], 0, 0, 255);
double approx = f.apply((double) k, params);
if (approx < 0)
approx = 0;
if (approx >= 2 * 45)
approx = 2 * 45 - 1;
trajs.put(k, (int) Math.round(approx), 0, 255, 0);
if (projectionMap.get(k, (int) Math.round(approx))[0] > 70) {
double angle = (approx - 45) / 180 * Math.PI;
Imgproc.line(superFrame.getFrame().getSrc(), new Point(330 - Math.cos(angle) * 30, k - Math.sin(angle) * 30), new Point(330 + Math.cos(angle) * 30, k + Math.sin(angle) * 30), new Scalar(0, 255, 0));
}
}
images[5] = new Img(trajs, false).toJfxImage();
images[6] = new Img(superFrame.getFrame().getSrc(), false).toJfxImage();
// images[7] = new Img(RadonTransform.estimateBaselines(superFrame.getFrame().getSrc(), 0), false).toJfxImage();
return images;
}
@Override
protected void onS() {
config.stabilizedMode = !config.stabilizedMode;
}
@Override
protected void onSpace() {
if (config.isOn)
timer.shutdown();
else {
timer = new BoundedScheduledThreadPoolExecutor();
startTimer();
}
config.isOn = !config.isOn;
}
@Override
protected void onT() {
config.textsEnabledMode = !config.textsEnabledMode;
}
@Override
public void stop() throws Exception {
super.stop();
timer.shutdown();
timer.awaitTermination(5000, TimeUnit.MILLISECONDS);
gsCapture.release();
}
}