-
Notifications
You must be signed in to change notification settings - Fork 5
/
Zone.java
79 lines (61 loc) · 2.21 KB
/
Zone.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
package org.genericsystem.cv;
import java.util.Map.Entry;
import java.util.stream.Stream;
import org.genericsystem.cv.comparator.ZoneScorerMap;
import org.genericsystem.cv.comparator.ZoneScorerMap2;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;
public class Zone {
private Rect rect;
private int num;
public Zone() {
}
public Zone(int num, Rect rect) {
this.num = num;
this.rect = rect;
}
public Rect getRect() {
return rect;
}
public ZoneScorer newUnsupervisedScorer(Stream<Img> imgs) {
return new ZoneScorer(this, imgs);
}
public ZoneScorerMap2 newUnsupervisedScorerMap(String fileName, Stream<Entry<Img, String>> stream) {
ZoneScorerMap2 scorer = new ZoneScorerMap2(this, stream, fileName, false);
return scorer;
}
public ZoneScorerMap2 newSupervisedScorerMap(String fileName, String realText, Stream<Entry<Img, String>> stream) {
ZoneScorerMap2 scorer = new ZoneScorerMap2(this, stream, fileName, realText, true);
return scorer;
}
public Zone adjustRect(double dx, double dy, int maxWidht, int maxHeight) {
Point tl = new Point(rect.tl().x > dx ? rect.tl().x - dx : 0d, rect.tl().y > dy ? rect.tl().y - dy : 0d);
Point br = new Point((rect.br().x + dx > maxWidht) ? maxWidht : rect.br().x + dx, (rect.br().y + dy > maxHeight) ? maxHeight : rect.br().y + dy);
return new Zone(num, new Rect(tl, br));
}
public void draw(Img img, Scalar color, int thickness) {
Imgproc.rectangle(img.getSrc(), rect.tl(), rect.br(), color, thickness);
}
public void write(Img img, String text, double fontScale, Scalar color, int thickness) {
Imgproc.putText(img.getSrc(), text, new Point(rect.tl().x, rect.br().y), Core.FONT_HERSHEY_PLAIN, fontScale, color, thickness);
}
public String ocr(Img img) {
return Ocr.doWork(new Mat(img.getSrc(), getRect()));
}
public int getNum() {
return num;
}
public Img getRoi(Img img) {
return new Img(img, this);
}
public Img getImg(Img img) {
return new Img(new Mat(img.getSrc(), getRect()));
}
public void draw(Mat roi, Scalar color, int thickness) {
Imgproc.rectangle(roi, rect.tl(), rect.br(), color, thickness);
}
}