-
Notifications
You must be signed in to change notification settings - Fork 5
/
Layout.java
358 lines (308 loc) · 11.8 KB
/
Layout.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
package org.genericsystem.layout;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Function;
import org.genericsystem.cv.Img;
import org.genericsystem.cv.Ocr;
import org.opencv.core.Core;
import org.opencv.core.Mat;
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.imgproc.Imgproc;
import org.opencv.utils.Converters;
public class Layout {
private double x1;
private double x2;
private double y1;
private double y2;
private Map<String, Integer> labels = new HashMap<>();
private List<Layout> children = new ArrayList<>();
private Layout parent = null;
public Layout(Layout parent, double x1, double x2, double y1, double y2) {
this.parent = parent;
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
}
public Img getRoi(Img img) {
return new Img(img, this);
}
public Rect getRect(Img imgRoot) {
Rect parentRect = getParent() != null ? getParent().getRect(imgRoot) : new Rect(0, 0, imgRoot.width(), imgRoot.height());
return new Rect(new Point(parentRect.tl().x + parentRect.width * getX1(), parentRect.tl().y + parentRect.height * getY1()), new Point(parentRect.tl().x + parentRect.width * getX2(), parentRect.tl().y + parentRect.height * getY2()));
}
public double area(Img imgRoot) {
double result = 0;
if (this.getChildren().isEmpty()) {
result += getRect(imgRoot).area() / (imgRoot.height() * imgRoot.width());
} else {
for (Layout child : getChildren()) {
result += child.area(imgRoot);
}
}
return result;
}
public Rect getLargeRect(Img imgRoot, int delta) {
Rect rect = getRect(imgRoot);
return new Rect(new Point(rect.tl().x - delta >= 0 ? rect.tl().x : 0, rect.tl().y - delta >= 0 ? rect.tl().y : 0),
new Point(rect.br().x + delta <= imgRoot.width() ? rect.br().x + delta : imgRoot.width(), rect.br().y + delta <= imgRoot.height() ? rect.br().y + delta : imgRoot.height()));
}
public Layout traverse(Img img, BiConsumer<Img, Layout> visitor) {
for (Layout shard : getChildren())
shard.traverse(shard.getRoi(img), visitor);
visitor.accept(img, this);
return this;
}
public void draw(Img img, Scalar color, int thickness) {
traverse(getRoi(img), (roi, shard) -> {
if (shard.getChildren().isEmpty())
Imgproc.rectangle(roi.getSrc(), new Point(0, 0), new Point(roi.width() - 1, roi.height() - 1), color, thickness);
else
Imgproc.rectangle(roi.getSrc(), new Point(0, 0), new Point(roi.width() - 1, roi.height() - 1), new Scalar(0, 0, 255), thickness);
});
}
public void drawPerspective(Img img, Mat homography, Scalar color, int thickness) {
traverse(getRoi(img), (roi, shard) -> {
if (shard.getChildren().isEmpty()) {
MatOfPoint2f results = new MatOfPoint2f();
Rect rect = shard.getRect(img);
List<Point> points = Arrays.asList(new Point(rect.tl().x, rect.tl().y), new Point(rect.tl().x + roi.width() - 1, rect.tl().y), new Point(rect.tl().x + roi.width() - 1, rect.tl().y + roi.height() - 1),
new Point(rect.tl().x, rect.tl().y + roi.height() - 1));
Mat pts = Converters.vector_Point2f_to_Mat(points);
Core.perspectiveTransform(pts, results, homography);
Point[] targets = results.toArray();
Imgproc.line(img.getSrc(), targets[0], targets[1], color, thickness);
Imgproc.line(img.getSrc(), targets[1], targets[2], color, thickness);
Imgproc.line(img.getSrc(), targets[2], targets[3], color, thickness);
Imgproc.line(img.getSrc(), targets[3], targets[0], color, thickness);
}
});
}
public void ocrTree(Img rootImg, int delta) {
traverse(rootImg, (root, layout) -> {
if (layout.getChildren().isEmpty()) {
String ocr = Ocr.doWork(new Mat(rootImg.getSrc(), layout.getLargeRect(rootImg, delta)));
if (!"".equals(ocr)) {
Integer count = layout.getLabels().get(ocr);
layout.getLabels().put(ocr, 1 + (count != null ? count : 0));
int all = layout.getLabels().values().stream().reduce(0, (i, j) -> i + j);
layout.getLabels().entrySet().forEach(entry -> {
if (entry.getValue() > all / 10)
Imgproc.putText(rootImg.getSrc(), Normalizer.normalize(entry.getKey(), Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", ""), layout.getRect(rootImg).tl(), Core.FONT_HERSHEY_PLAIN, 1, new Scalar(255, 0, 0), 1);
});
// Imgproc.putText(rootImg.getSrc(), layout.getBestLabel(), layout.getRect(rootImg).tl(), Core.FONT_HERSHEY_PLAIN, 1, new Scalar(255, 0, 0), 1);
// System.out.println(layout.getBestLabel());
}
}
});
}
private String getBestLabel() {
String result = "";
int occurence = 0;
for (String key : getLabels().keySet()) {
if (getLabels().get(key) > occurence) {
result = key;
occurence = getLabels().get(key);
}
}
return result;
}
public void addChild(Layout child) {
if (!children.contains(child))
children.add(child);
}
public void removeChild(Layout child) {
if (children.contains(child))
children.remove(child);
}
public boolean equiv(Layout s, double xTolerance, double yTolerance) {
if (Math.abs(s.x1 - x1) <= xTolerance && Math.abs(s.x2 - x2) <= xTolerance && Math.abs(s.y1 - y1) <= yTolerance && Math.abs(s.y2 - y2) <= yTolerance)
return true;
return false;
}
public List<Layout> getChildren() {
return children;
}
public double getX1() {
return x1;
}
public void setX1(double x1) {
this.x1 = x1;
}
public double getX2() {
return x2;
}
public void setX2(double x2) {
this.x2 = x2;
}
public double getY1() {
return y1;
}
public void setY1(double y1) {
this.y1 = y1;
}
public double getY2() {
return y2;
}
public void setY2(double y2) {
this.y2 = y2;
}
public Layout getParent() {
return parent;
}
public void setParent(Layout parent) {
this.parent = parent;
}
public Map<String, Integer> getLabels() {
return labels;
}
public boolean hasChildren() {
return !getChildren().isEmpty();
}
public String recursiveToString() {
StringBuilder sb = new StringBuilder();
recursivToString(this, sb, 0);
sb.append("\n");
return sb.toString();
}
private void recursivToString(Layout shard, StringBuilder sb, int depth) {
sb.append("depth : " + depth + " : ");
sb.append("[(" + shard.x1 + "," + shard.x2 + "),(" + shard.y1 + "," + shard.y2 + ")]".toString());
if (!shard.hasChildren())
sb.append(" : Labels : " + shard.labels);
else {
depth++;
for (Layout s : shard.getChildren()) {
sb.append("\n");
for (int i = 0; i < depth; i++)
sb.append(" ");
recursivToString(s, sb, depth);
}
}
}
@Override
public String toString() {
return "tl : (" + this.x1 + "," + this.y1 + "), br :(" + this.x2 + "," + this.y2 + ")";
}
public Layout tighten(Img binary) {
double[] x = getHistoLimits(binary.projectHorizontally());
double[] y = getHistoLimits(binary.projectVertically());
if (x[0] <= x[1] || y[0] <= y[1]) {
return new Layout(this.getParent(), getX1() + x[0] * (getX2() - getX1()), getX1() + x[1] * (getX2() - getX1()), getY1() + y[0] * (getY2() - getY1()), getY1() + y[1] * (getY2() - getY1()));
} else {
return new Layout(this.getParent(), 0, 0, 0, 0);
}
}
public static double[] getHistoLimits(List<Float> hist) {
int start = 0;
int end = hist.size() - 1;
while (start < hist.size() && hist.get(start) >= 255.0)
start++;
while (end >= 0 && hist.get(end) >= 255.0)
end--;
return new double[] { Integer.valueOf(start).doubleValue() / hist.size(), Integer.valueOf(end + 1).doubleValue() / hist.size() };
}
public List<Layout> split(Size morph, Img binary) {
// Adjust the percentage according to equation: m / (alpha + beta * x)
Double alpha = Double.valueOf(0);
Double beta = Double.valueOf(1);
Double adjustMorph = 1_000 / (alpha + beta * binary.width());
System.out.println(String.format("width: %dpx; adj: %.2f", binary.width(), adjustMorph));
// Replaced the morph.width parameter with the computed one
Double morphW = morph.width * adjustMorph;
Double morphH = morph.height;
Double verticalParam = Math.floor(morphH * binary.height());
Double horizontalParam = Math.floor(morphW * binary.width());
// System.out.printf("width: %dpx; a: %.2f\n", binary.width(), morphW * 100);
return extractZones(close(verticalParam.intValue(), binary.projectVertically()), close(horizontalParam.intValue(), binary.projectHorizontally()), binary);
}
private static boolean[] close(int k, List<Float> histo) {
boolean[] closed = new boolean[histo.size()];
Function<Integer, Boolean> isWhite = i -> histo.get(i) >= 255;
for (int i = 0; i < histo.size() - 1; i++)
if (!isWhite.apply(i) && isWhite.apply(i + 1)) {
for (int j = k + 1; j > 0; j--)
if (i + j < histo.size()) {
if (!isWhite.apply(i + j)) {
Arrays.fill(closed, i, i + j + 1, true);
i += j - 1;
break;
}
closed[i] = !isWhite.apply(i);
}
} else
closed[i] = !isWhite.apply(i);
if (!closed[histo.size() - 1])
closed[histo.size() - 1] = !isWhite.apply(histo.size() - 1);
return closed;
}
private List<Layout> extractZones(boolean[] resultV, boolean[] resultH, Img binary) {
List<double[]> shardsV = getShards(resultV, true);
List<double[]> shardsH = getShards(resultH, false);
// System.out.println("Binary size : " + binary.size());
// System.out.println("Size spit : " + shardsV.size() + " " + shardsH.size());
List<Layout> shards = new ArrayList<>();
for (double[] shardv : shardsV)
for (double[] shardh : shardsH) {
Layout target = new Layout(this, shardh[0], shardh[1], shardv[0], shardv[1]);
Img roi = target.getRoi(binary);
// System.out.println("roi : rows :" + roi.rows() + " , cols :" + roi.cols());
if (roi.rows() != 0 && roi.cols() != 0)
shards.add(target.tighten(roi));
}
return shards;
}
private List<double[]> getShards(boolean[] result, boolean vertical) {
List<double[]> shards = new ArrayList<>();
Integer start = result[0] ? 0 : null;
assert result.length >= 1;
for (int i = 0; i < result.length - 1; i++)
if (!result[i] && result[i + 1])
start = i + 1;
else if (result[i] && !result[i + 1]) {
shards.add(vertical ? new double[] { Integer.valueOf(start).doubleValue() / result.length, (Integer.valueOf(i).doubleValue() + 1) / result.length }
: new double[] { Integer.valueOf(start).doubleValue() / result.length, (Integer.valueOf(i).doubleValue() + 1) / result.length });
start = null;
}
if (result[result.length - 1]) {
shards.add(vertical ? new double[] { Integer.valueOf(start).doubleValue() / result.length, Integer.valueOf(result.length).doubleValue() / result.length }
: new double[] { Integer.valueOf(start).doubleValue() / result.length, Integer.valueOf(result.length).doubleValue() / result.length });
start = null;
}
return shards;
}
public Layout recursiveSplit(Size morph, int level, Img binary) {
// System.out.println("level : " + level);
// System.out.println("Layout : " + this);
assert binary.size().equals(binary.size());
if (binary.size().height == 0 || binary.size().width == 0)
return this;
if (level <= 0) {
// Imgproc.rectangle(img.getSrc(), new Point(0, 0), new Point(img.width(), img.height()), new Scalar(255, 0, 0), -1);
return this;
}
List<Layout> shards = split(morph, binary);
// shards.removeIf(shard -> ((shard.getY2() - shard.getY1()) * binary.size().height) < 2 || ((shard.getX2() - shard.getX1()) * binary.size().width) < 2);
if (shards.isEmpty()) {
// Imgproc.rectangle(img.getSrc(), new Point(0, 0), new Point(img.width(), img.height()), new Scalar(0, 0, 255), -1);
return this;
}
if (shards.size() == 1) {
return this;
}
for (Layout shard : shards) {
shard.recursiveSplit(morph, level - 1, shard.getRoi(binary));
this.addChild(shard);
}
return this;
}
}