-
Notifications
You must be signed in to change notification settings - Fork 5
/
Layout.java
346 lines (288 loc) · 11 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
package org.genericsystem.layout;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Function;
import org.genericsystem.cv.Img;
import org.genericsystem.cv.Ocr;
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 org.opencv.utils.Converters;
public class Layout {
private double x1;
private double x2;
private double y1;
private double y2;
private List<String> labels = new ArrayList<>();
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 Rect getLargeRect(Img imgRoot, int delta) {
Rect rect = getRect(imgRoot);
return new Rect(new Point(rect.tl().x - delta, rect.tl().y - delta), new Point(rect.br().x + delta, rect.br().y + delta));
}
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);
});
}
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)));
layout.getLabels().add(ocr);
System.out.println(layout.getLabels());
}
});
}
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 List<String> getLabels() {
return labels;
}
public Layout getParent() {
return parent;
}
public void setParent(Layout parent) {
this.parent = parent;
}
public boolean hasChildren() {
if (children != null && children.size() >= 1)
return true;
return false;
}
public String recursivToString() {
StringBuilder sb = new StringBuilder();
recursivToString(this, sb, 0);
return sb.toString();
}
private void recursivToString(Layout shard, StringBuilder sb, int depth) {
sb.append("depth : " + depth + " : ");
sb.append("((" + shard.x1 + "-" + shard.y1 + "),(" + shard.x2 + "-" + shard.y2 + "))".toString());
if (shard.getChildren().isEmpty())
sb.append(" : Label : ");
for (String label : shard.labels)
sb.append("/" + label);
if (shard.hasChildren()) {
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, float concentration) {
List<Float> Vhist = new ArrayList<>();
List<Float> Hhist = new ArrayList<>();
Converters.Mat_to_vector_float(binary.projectVertically().getSrc(), Vhist);
Converters.Mat_to_vector_float(binary.projectHorizontally().transpose().getSrc(), Hhist);
for (int i = 0; i < binary.getSrc().rows(); i++) {
for (int j = 0; j < binary.getSrc().cols(); j++) {
double value = binary.getSrc().get(i, j)[0];
if (value > 255.0)
System.out.println(value);
}
}
// System.out.println("Vhist before smooth: " + Vhist.toString());
Vhist = smoothHisto(concentration, Vhist, true, binary);
// System.out.println("Vhist after smooth: " + Vhist.toString());
// System.out.println("Hhist before smooth: " + Hhist.toString());
Hhist = smoothHisto(concentration, Hhist, false, binary);
// System.out.println("Hhist after smooth: " + Hhist.toString());
double[] x = getHistoLimits(Hhist);
double[] y = getHistoLimits(Vhist);
// System.out.println("x " + Arrays.toString(x) + " y " + Arrays.toString(y));
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, float concentration, Img binary) {
List<Float> histoVertical = new ArrayList<>();
List<Float> histoHorizontal = new ArrayList<>();
Converters.Mat_to_vector_float(binary.projectVertically().getSrc(), histoVertical);
Converters.Mat_to_vector_float((binary.projectHorizontally().transpose()).getSrc(), histoHorizontal);
histoVertical = smoothHisto(concentration, histoVertical, true, binary);
histoHorizontal = smoothHisto(concentration, histoHorizontal, false, binary);
int kV = new Double(Math.floor(morph.height * histoVertical.size())).intValue();
int kH = new Double(Math.floor(morph.width * histoHorizontal.size())).intValue();
boolean[] closedV = getClosedFromHisto(kV, histoVertical);
boolean[] closedH = getClosedFromHisto(kH, histoHorizontal);
return extractZones(closedV, closedH, binary, concentration);
}
private static List<Float> smoothHisto(float concentration, List<Float> histo, boolean vertical, Img binary) {
float min = concentration * 255;
float max = (1 - concentration) * 255;
for (int i = 0; i < histo.size(); i++) {
float value = histo.get(i);
if (histo.size() > 32)
if (value <= min || value >= max) {
histo.set(i, 255f);
// System.out.println("mask black " + (vertical ? "line" : "column") + " " + i);
if (vertical)
Imgproc.line(binary.getSrc(), new Point(0, i), new Point(binary.getSrc().width() - 1, i), new Scalar(255));
else
Imgproc.line(binary.getSrc(), new Point(i, 0), new Point(i, binary.getSrc().rows() - 1), new Scalar(255));
}
}
return histo;
}
private static boolean[] getClosedFromHisto(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, float concentration) {
List<Layout> shardsV = getShards(resultV, true);
List<Layout> 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 (Layout shardv : shardsV)
for (Layout shardh : shardsH) {
Layout target = new Layout(this, shardh.x1, shardh.x2, shardv.y1, shardv.y2);
// (0.5223880597014925-0.3358302122347066),(1.0-0.34519350811485644)
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, concentration));
}
return shards;
}
private List<Layout> getShards(boolean[] result, boolean vertical) {
List<Layout> 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 Layout(this, 0, 1, Integer.valueOf(start).doubleValue() / result.length, (Integer.valueOf(i).doubleValue() + 1) / result.length)
: new Layout(this, Integer.valueOf(start).doubleValue() / result.length, (Integer.valueOf(i).doubleValue() + 1) / result.length, 0, 1));
start = null;
}
if (result[result.length - 1]) {
shards.add(vertical ? new Layout(this, 0, 1, Integer.valueOf(start).doubleValue() / result.length, Integer.valueOf(result.length).doubleValue() / result.length)
: new Layout(this, Integer.valueOf(start).doubleValue() / result.length, Integer.valueOf(result.length).doubleValue() / result.length, 0, 1));
start = null;
}
return shards;
}
public Layout recursivSplit(Size morph, int level, float concentration, Img img, Img binary) {
// System.out.println("level : " + level);
// System.out.println("Layout : " + this);
assert img.size().equals(binary.size());
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, concentration, binary);
shards.removeIf(shard -> ((shard.getY2() - shard.getY1()) * img.size().height) < 4 || ((shard.getX2() - shard.getX1()) * img.size().width) < 4);
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.recursivSplit(morph, level - 1, concentration, shard.getRoi(img), shard.getRoi(binary));
this.addChild(shard);
}
return this;
}
}