-
Notifications
You must be signed in to change notification settings - Fork 5
/
ImgClass.java
197 lines (173 loc) · 6.1 KB
/
ImgClass.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package org.genericsystem.cv;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class ImgClass {
private Mat average;
private Mat variance;
private final String directory;
private final List<Mat> bgrImages = new ArrayList<>();
public static ImgClass fromDirectory(String bgrDirectory) {
return new ImgClass(bgrDirectory);
}
public ImgClass(String bgrDirectory) {
this.directory = bgrDirectory;
getClassMats(directory).forEach(bgrImages::add);
List<Mat> meanAndVariance = computeMeanAndVariance(Function.identity());
average = meanAndVariance.get(0);
variance = meanAndVariance.get(1);
}
public Mat computeMean(Function<Mat, Mat> filter) {
Mat mean = filter.apply(bgrImages.get(0));
for (int count = 1; count < bgrImages.size(); count++)
Core.addWeighted(mean, 1d - 1d / Integer.valueOf(count).doubleValue(), filter.apply(bgrImages.get(count)), 1d / Integer.valueOf(count).doubleValue(), 0, mean);
return mean;
}
public Mat computeBluredMean(Size blurSize) {
return computeMean(mat -> {
Mat result = new Mat();
Imgproc.GaussianBlur(mat, result, blurSize, 0);
return result;
});
}
public Mat computeBluredVariance(Size blurSize) {
return computeMeanAndVariance(mat -> {
Mat result = new Mat();
Imgproc.GaussianBlur(mat, result, blurSize, 0);
return result;
}).get(1);
}
public Mat computeVariance(Mat mean, Function<Mat, Mat> filter) {
Mat diff = new Mat();
Core.absdiff(mean, filter.apply(bgrImages.get(0)), diff);
Mat variance = new Mat(mean.size(), mean.type(), new Scalar(0, 0, 0));
for (int count = 1; count < bgrImages.size(); count++) {
diff = new Mat();
Core.absdiff(mean, filter.apply(bgrImages.get(count)), diff);
Core.addWeighted(variance, 1d - 1d / Integer.valueOf(count).doubleValue(), diff, 1d / Integer.valueOf(count).doubleValue(), 0, variance);
}
return variance;
}
public List<Mat> computeMeanAndVariance(Function<Mat, Mat> filter) {
int type = CvType.CV_32SC3;
Mat img0 = filter.apply(bgrImages.get(0));
Mat mean = new Mat(img0.size(), type, new Scalar(0, 0, 0));
Mat m2 = new Mat(img0.size(), type, new Scalar(0, 0, 0));
Mat mask = Mat.ones(img0.size(), CvType.CV_8U);
int count = 1;
for (; count <= bgrImages.size(); count++) {
Mat img = new Mat();
filter.apply(bgrImages.get(count - 1)).convertTo(img, type);;
Mat delta = new Mat(img.size(), type);
Core.subtract(img, mean, delta, mask, type);
Core.addWeighted(mean, 1, delta, 1d / count, 0, mean, type);
Mat delta2 = new Mat(m2.size(), type);
Core.subtract(img, mean, delta2, mask, type);
Mat product = delta.mul(delta2);
Core.add(m2, product, m2);
}
Mat variance = new Mat(m2.size(), type);
Core.multiply(m2, new Scalar(1d / count, 1d / count, 1d / count), variance);
variance.convertTo(variance, CvType.CV_8UC3);
mean.convertTo(mean, CvType.CV_8UC3);
List<Mat> result = new ArrayList<>();
result.add(mean);
result.add(variance);
return result;
}
public Mat computeRangedMean(Scalar bgr1, Scalar bgr2, boolean hsv, boolean reverse) {
return computeMean(mat -> {
Mat ranged = new Mat();
if (hsv)
Imgproc.cvtColor(mat, ranged, Imgproc.COLOR_BGR2HSV);
else
mat.copyTo(ranged);
Mat mask = new Mat();
Core.inRange(ranged, bgr1, bgr2, mask);
if (reverse)
Core.bitwise_not(mask, mask);
Mat result = new Mat(ranged.size(), ranged.type(), new Scalar(0, 0, 0));
ranged.copyTo(result, mask);
if (hsv)
Imgproc.cvtColor(result, result, Imgproc.COLOR_HSV2BGR);
return result;
});
}
public Mat computeRangedVariance(Scalar bgr1, Scalar bgr2, boolean hsv) {
return computeMeanAndVariance(mat -> {
Mat ranged = new Mat();
if (hsv)
Imgproc.cvtColor(mat, ranged, Imgproc.COLOR_BGR2HSV);
else
mat.copyTo(ranged);
Mat mask = new Mat();
Core.inRange(ranged, bgr1, bgr2, mask);
Mat result = new Mat();
ranged.copyTo(result, mask);
if (hsv)
Imgproc.cvtColor(result, result, Imgproc.COLOR_HSV2BGR);
return result;
}).get(1);
}
private List<Mat> getClassMats(String repository) {
return Arrays.stream(new File(repository).listFiles()).filter(img -> img.getName().endsWith(".png")).peek(img -> System.out.println("load : " + img.getPath())).map(img -> Imgcodecs.imread(img.getPath())).collect(Collectors.toList());
}
public static List<Rect> getRectZones(Mat highlightVariance) {
// To improve
List<Rect> result = new ArrayList<>();
List<MatOfPoint> contours = new ArrayList<>();
Imgproc.findContours(highlightVariance, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
double minArea = 500;
Collections.sort(contours, (c1, c2) -> Double.compare(Imgproc.contourArea(c2), Imgproc.contourArea(c1)));
for (int i = 0; i < contours.size(); i++) {
MatOfPoint contour = contours.get(i);
double contourarea = Imgproc.contourArea(contour);
if (contourarea > minArea)
result.add(Imgproc.boundingRect(contour));
}
return result;
}
public String getDirectory() {
return directory;
}
public List<Mat> getBgrImages() {
return bgrImages;
}
public Mat getAverage() {
return average;
}
public Mat getVariance() {
return variance;
}
// public Mat getGrayAverage() {
// Mat greyVariance = new Mat();
// if (hsv)
// Imgproc.cvtColor(average, greyVariance, Imgproc.COLOR_HSV2BGR);
// else
// greyVariance = average.clone();
// Imgproc.cvtColor(greyVariance, greyVariance, Imgproc.COLOR_BGR2GRAY);
// return greyVariance;
// }
// public Mat getGrayVariance() {
// Mat greyVariance = new Mat();
// if (hsv)
// Imgproc.cvtColor(variance, greyVariance, Imgproc.COLOR_HSV2BGR);
// else
// greyVariance = variance.clone();
// Imgproc.cvtColor(greyVariance, greyVariance, Imgproc.COLOR_BGR2GRAY);
// return greyVariance;
// }
}