-
Notifications
You must be signed in to change notification settings - Fork 5
/
Zones.java
137 lines (109 loc) · 4.37 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
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
package org.genericsystem.cv;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
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.core.Size;
import org.opencv.imgproc.Imgproc;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class Zones implements Iterable<Zone> {
private final List<Zone> zones;
private static final ObjectMapper mapper = new ObjectMapper();
public static Zones get(Img img, double minArea) {
return new Zones(img.channels() == 1 ? img : img.bgr2Gray(), minArea);
}
public static Zones get(Img img, double minArea, int RETR) {
return new Zones(img.channels() == 1 ? img : img.bgr2Gray(), minArea, RETR);
}
public static Zones get(Img img, double minArea, double dx, double dy) {
return new Zones(img.channels() == 1 ? img : img.bgr2Gray(), minArea).adjust(dx, dy, img.width(), img.height());
}
public static Zones splitVertically(Img img, double morph, double minarea) {
return get(img.otsuInv().projectVertically().toVerticalHistogram(img.cols()).morphologyEx(Imgproc.MORPH_CLOSE, Imgproc.MORPH_RECT, new Size(1, morph)), minarea);
}
public static Zones splitHorizontally(Img img, double morph, double minarea) {
return get(img.otsuInv().projectHorizontally().toHorizontalHistogram(img.rows()).morphologyEx(Imgproc.MORPH_CLOSE, Imgproc.MORPH_RECT, new Size(morph, 1)), minarea);
}
public static Zones split(Img img, double morph, double minarea, double dx, double dy, boolean vertical) {
return vertical ? splitVertically(img, morph, minarea, dx, dy) : splitHorizontally(img, morph, minarea, dx, dy);
}
public static Zones splitVertically(Img img, double morph, double minarea, double dx, double dy) {
return get(img.otsuInv().projectVertically().toVerticalHistogram(img.cols()).morphologyEx(Imgproc.MORPH_CLOSE, Imgproc.MORPH_RECT, new Size(1, morph)), minarea, dx, dy);
}
public static Zones splitHorizontally(Img img, double morph, double minarea, double dx, double dy) {
return get(img.otsuInv().projectHorizontally().toHorizontalHistogram(img.rows()).morphologyEx(Imgproc.MORPH_CLOSE, Imgproc.MORPH_RECT, new Size(morph, 1)), minarea, dx, dy);
}
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() {
zones = new ArrayList<>();
}
public Zones(List<Zone> zonesList) {
this.zones = zonesList;
}
public Zones(Img gray, double minArea) {
this(gray, minArea, Imgproc.RETR_EXTERNAL);
}
public Zones(Img gray, double minArea, int RETR) {
this.zones = new ArrayList<>();
List<MatOfPoint> contours = gray.findContours(new Img[1], RETR, Imgproc.CHAIN_APPROX_SIMPLE);
int num = 0;
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(num, rect));
num++;
// }
}
}
}
public void draw(Img img, Scalar scalar, int thickness) {
zones.forEach(adjusted -> adjusted.draw(img, scalar, thickness));
}
public void writeNum(Img img, Scalar scalar, int thickness) {
zones.forEach(adjusted -> adjusted.write(img, String.valueOf(adjusted.getNum()), 3, scalar, thickness));
}
public List<Zone> getZones() {
return zones;
}
public void save(File file) {
file.getParentFile().mkdirs();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
try {
mapper.writeValue(file, this);
System.out.println("Zones saved in " + file.getAbsolutePath());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void save(String imgClassDirectory) {
this.save(new File(imgClassDirectory + "/zones/zones.json"));
}
public static Zones load(File file) {
try {
return mapper.readValue(file, Zones.class);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static Zones load(String imgClassDirectory) {
return load(new File(imgClassDirectory + "/zones/zones.json"));
}
@Override
public Iterator<Zone> iterator() {
return zones.iterator();
}
public int size() {
return zones.size();
}
}