-
Notifications
You must be signed in to change notification settings - Fork 5
/
Zone.java
108 lines (89 loc) · 2.58 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
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
package org.genericsystem.cv;
import java.io.Serializable;
import org.genericsystem.cv.utils.ModelTools;
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 implements Serializable {
private Rect rect;
private int num;
private String uid;
public Zone() {
}
/**
* Create a new Zone with a given number and UID.
*
* @param num - a number for the zone
* @param rect - a {@link Rect} object which define the zone
*/
public Zone(int num, Rect rect) {
this.num = num;
this.rect = rect;
this.uid = ModelTools.generateZoneUID(rect);
}
public Rect getRect() {
return rect;
}
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()), false);
}
public void draw(Mat roi, Scalar color, int thickness) {
Imgproc.rectangle(roi, rect.tl(), rect.br(), color, thickness);
}
@Override
public String toString() {
return "Zone [rect=" + rect + ", num=" + num + ", uid=" + uid + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + num;
result = prime * result + ((rect == null) ? 0 : rect.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Zone other = (Zone) obj;
if (num != other.num)
return false;
if (rect == null) {
if (other.rect != null)
return false;
} else if (!rect.equals(other.rect))
return false;
return true;
}
public String getUid() {
return uid;
}
}