-
Notifications
You must be signed in to change notification settings - Fork 5
/
RectToolsMapper.java
138 lines (114 loc) · 5.03 KB
/
RectToolsMapper.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
package org.genericsystem.cv.utils;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.genericsystem.reinforcer.tools.GSPoint;
import org.genericsystem.reinforcer.tools.GSRect;
import org.genericsystem.reinforcer.tools.RectangleTools;
import org.genericsystem.reinforcer.tools.RectangleTools.MERGE_METHOD;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Size;
/**
* This is an adaptater class for gs-reinforcer {@link RectangleTools}, that accept of OpenCV's {@link Rect}, {@link Point} and {@link Size}.
*
* @author Pierrik Lassalas
*/
public final class RectToolsMapper {
private static final Function<? super Rect, ? extends GSRect> rectToGSRect = rect -> convert(rect);
private static final Function<? super GSRect, ? extends Rect> gsRectToRect = gsRect -> convert(gsRect);
private static final Function<? super Point, ? extends GSPoint> pointToGSPoint = point -> convert(point);
private static final Function<? super GSPoint, ? extends Point> gsPointToPoint = gsPoint -> convert(gsPoint);
private static GSRect convert(Rect rect) {
return new GSRect(rect.x, rect.y, rect.width, rect.height);
}
private static Rect convert(GSRect gsRect) {
return new Rect((int) gsRect.getX(), (int) gsRect.getY(), (int) gsRect.getWidth(), (int) gsRect.getHeight());
}
private static Point convert(GSPoint gsPoint) {
return new Point(gsPoint.getX(), gsPoint.getY());
}
private static GSPoint convert(Point point) {
return new GSPoint(point.x, point.y);
}
public static List<Rect> groupRectangles(List<Rect> input, MERGE_METHOD method) {
List<GSRect> rects = input.stream().map(rectToGSRect).collect(Collectors.toList());
List<GSRect> result = RectangleTools.groupRectangles(rects, method);
return result.stream().map(gsRectToRect).collect(Collectors.toList());
}
public static List<Rect> groupRectangles(List<Rect> input, double eps, double groupThreshold, MERGE_METHOD method) {
List<GSRect> rects = input.stream().map(rectToGSRect).collect(Collectors.toList());
List<GSRect> result = RectangleTools.groupRectangles(rects, eps, groupThreshold, method);
return result.stream().map(gsRectToRect).collect(Collectors.toList());
}
public static List<List<Rect>> cluster(List<Rect> input, double eps) {
List<GSRect> rects = input.stream().map(rectToGSRect).collect(Collectors.toList());
List<List<GSRect>> result = RectangleTools.cluster(rects, eps);
return result.stream().map(list -> (List<Rect>) list.stream().map(gsRectToRect).collect(Collectors.toList())).collect(Collectors.toList());
}
public static boolean isInCluster(Rect rect1, Rect rect2, double eps) {
GSRect r1 = convert(rect1);
GSRect r2 = convert(rect2);
return RectangleTools.isInCluster(r1, r2, eps);
}
public static boolean isInCluster(Rect rect1, Rect rect2, double eps, int sides) {
GSRect r1 = convert(rect1);
GSRect r2 = convert(rect2);
return RectangleTools.isInCluster(r1, r2, eps, sides);
}
public static double[] commonArea(Rect rect1, Rect rect2) {
GSRect r1 = convert(rect1);
GSRect r2 = convert(rect2);
return RectangleTools.commonArea(r1, r2);
}
public static double inclusiveArea(Rect rect1, Rect rect2) {
GSRect r1 = convert(rect1);
GSRect r2 = convert(rect2);
return r1.inclusiveArea(r2);
}
public static Optional<Rect> getIntersection(Rect rect1, Rect rect2) {
GSRect r1 = convert(rect1);
GSRect r2 = convert(rect2);
Optional<GSRect> optional = r1.getIntersection(r2);
return optional.map(gsRectToRect);
}
public static Rect getUnion(Rect rect1, Rect rect2) {
GSRect r1 = convert(rect1);
GSRect r2 = convert(rect2);
GSRect result = r1.getUnion(r2);
return convert(result);
}
public static Rect getMean(List<Rect> rects) {
List<GSRect> rectangles = rects.stream().map(rectToGSRect).collect(Collectors.toList());
GSRect result = RectangleTools.getMean(rectangles);
return convert(result);
}
public static boolean isOverlapping(Rect rect1, Rect rect2) throws IllegalArgumentException {
GSRect r1 = convert(rect1);
GSRect r2 = convert(rect2);
return r1.isOverlapping(r2);
}
public static Optional<Rect> getInsider(Rect rect1, Rect rect2) {
GSRect r1 = convert(rect1);
GSRect r2 = convert(rect2);
Optional<GSRect> optional = r1.getInsider(r2);
return optional.map(gsRectToRect);
}
public static boolean contains(Rect rect, Point point) {
GSRect r = convert(rect);
GSPoint p = convert(point);
return r.contains(p);
}
public static Point[] decomposeClockwise(Rect rect) {
GSRect r = convert(rect);
List<GSPoint> points = Arrays.asList(r.decomposeClockwise());
return points.stream().map(gsPointToPoint).toArray(Point[]::new);
}
public static List<Rect> nonMaximumSuppression(List<Rect> boxes, double overlapThreshold) {
List<GSRect> rectangles = boxes.stream().map(rectToGSRect).collect(Collectors.toList());
List<GSRect> results = RectangleTools.nonMaximumSuppression(rectangles, overlapThreshold);
return results.stream().map(gsRectToRect).collect(Collectors.toList());
}
}