-
Notifications
You must be signed in to change notification settings - Fork 5
/
Zones.java
52 lines (42 loc) · 1.44 KB
/
Zones.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
package org.genericsystem.cv;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;
public class Zones {
private final List<Zone> zones;
public static Zones get(Img img, double minArea) {
return new Zones(img, minArea);
}
public static Zones get(Img img, double minArea, double dx, double dy) {
return new Zones(img, minArea).adjust(dx, dy, img.width(), img.height());
}
private Zones adjust(double dx, double dy, int width, int height) {
return new Zones(zones.stream().map(zone -> zone.adjustRect(dx, dy, width, height)).collect(Collectors.toList()));
}
public Zones(List<Zone> zonesList) {
this.zones = zonesList;
}
public Zones(Img gray, double minArea) {
this.zones = new ArrayList<>();
List<MatOfPoint> contours = gray.findContours(new Img[1], Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
for (int i = 0; i < contours.size(); i++) {
MatOfPoint contour = contours.get(i);
double contourarea = Imgproc.contourArea(contour);
if (contourarea > minArea) {
Rect rect = Imgproc.boundingRect(contour);
if (rect.width >= rect.height)
zones.add(new Zone(rect));
}
}
}
public List<Zone> get() {
return zones;
}
public void draw(Img img, Scalar scalar, int thickness) {
zones.forEach(adjusted -> adjusted.draw(img, scalar, thickness));
}
}