-
Notifications
You must be signed in to change notification settings - Fork 5
/
RadonTransformDemo.java
221 lines (194 loc) · 8.11 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
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
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 java.util.stream.Collectors;
import org.genericsystem.cv.AbstractApp;
import org.genericsystem.cv.Img;
import org.genericsystem.cv.application.GeneralInterpolator.OrientedPoint;
import org.genericsystem.cv.lm.LevenbergImpl;
import org.genericsystem.cv.utils.NativeLibraryLoader;
import org.opencv.core.Mat;
import org.opencv.core.Point;
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];
long ref = System.currentTimeMillis();
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] = binarized.toJfxImage();
long last = System.currentTimeMillis();
System.out.println("Binarization : " + (last - ref));
ref = last;
int stripWidth = 80;
List<Mat> strips = RadonTransform.extractStrips(binarized.getSrc(), stripWidth);
last = System.currentTimeMillis();
System.out.println("Extract strips : " + (last - ref));
ref = last;
List<Mat> radons = strips.stream().map(strip -> RadonTransform.transform(strip, 45)).collect(Collectors.toList());
last = System.currentTimeMillis();
System.out.println("Compute radons : " + (last - ref));
ref = last;
List<Mat> projectionMaps = radons.stream().map(radon -> RadonTransform.projectionMap(radon)).collect(Collectors.toList());
last = System.currentTimeMillis();
System.out.println("Compute projections : " + (last - ref));
ref = last;
// 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[1] = new Img(projectionMap, false).toJfxImage();
// Imgproc.Sobel(projectionMap, projectionMap, CvType.CV_64FC1, 0, 1);
// DirectionalFilter.cleanContour(projectionMap);
projectionMaps.stream().forEach(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();
last = System.currentTimeMillis();
System.out.println("Compute gradients : " + (last - ref));
ref = last;
List<int[]> trajs = projectionMaps.stream().map(projectionMap -> RadonTransform.bestTraject(projectionMap, -5000, 2)).collect(Collectors.toList());
BiFunction<Double, double[], Double> f = (x, params) -> params[0] + params[1] * x + params[2] * x * x;
List<double[]> approxParams = new ArrayList<>();
for (int[] traj : trajs) {
List<double[]> values = new ArrayList<>();
for (int k = 0; k < binarized.height(); k++)
values.add(new double[] { k, traj[k] });
approxParams.add(LevenbergImpl.fromBiFunction(f, values, new double[] { 0, 0, 0 }).getParams());
}
// System.out.println(Arrays.toString(traj));
last = System.currentTimeMillis();
System.out.println("Compute approx : " + (last - ref));
ref = last;
Img frameDisplay = superFrame.getDisplay();
int strip = 0;
for (double[] approxParam : approxParams) {
for (int k = 0; k < binarized.height(); k += 20) {
double angle = (f.apply((double) k, approxParam) - 45) / 180 * Math.PI;
Imgproc.line(frameDisplay.getSrc(), new Point((strip + 1) * stripWidth / 2 - Math.cos(angle) * stripWidth / 2, k - Math.sin(angle) * stripWidth / 2),
new Point((strip + 1) * stripWidth / 2 + Math.cos(angle) * stripWidth / 2, k + Math.sin(angle) * stripWidth / 2), new Scalar(0, 255, 0), 1);
}
strip++;
}
images[1] = frameDisplay.toJfxImage();
last = System.currentTimeMillis();
System.out.println("Display lines : " + (last - ref));
ref = last;
strip = 0;
List<OrientedPoint> horizontals = new ArrayList<>();
for (double[] approxParam : approxParams) {
for (int k = 0; k < binarized.height(); k += 20) {
double angle = (f.apply((double) k, approxParam) - 45) / 180 * Math.PI;
horizontals.add(new OrientedPoint(new Point((strip + 1) * stripWidth / 2, k), angle, 1));
}
strip++;
}
strip = 0;
List<OrientedPoint> verticals = new ArrayList<>();
for (double[] approxParam : approxParams) {
for (int k = 0; k < binarized.height(); k += 20) {
double angle = (f.apply((double) k, approxParam) - 45) / 180 * Math.PI;
verticals.add(new OrientedPoint(new Point((strip + 1) * stripWidth / 2, k), Math.PI / 2, 1));
}
strip++;
}
Mat frame2 = superFrame.getFrame().getSrc().clone();
GeneralInterpolator interpolator = new GeneralInterpolator(horizontals, verticals, 2);
last = System.currentTimeMillis();
System.out.println("Prepare interpolator : " + (last - ref));
ref = last;
MeshGrid meshGrid = new MeshGrid(new Size(16, 9), interpolator, 20, 20, frame2);
meshGrid.build();
last = System.currentTimeMillis();
System.out.println("Build mesh : " + (last - ref));
ref = last;
meshGrid.draw(frame2, new Scalar(0, 255, 0));
last = System.currentTimeMillis();
System.out.println("Draw mesh : " + (last - ref));
ref = last;
images[3] = new Img(frame2, 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();
}
}