-
Notifications
You must be signed in to change notification settings - Fork 5
/
ClassImgFieldsDetector.java
213 lines (189 loc) · 9.72 KB
/
ClassImgFieldsDetector.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package org.genericsystem.cv;
import java.util.List;
import javafx.scene.layout.GridPane;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
public class ClassImgFieldsDetector extends AbstractApp {
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
private final static String classImgRepertory = "aligned-image-3.png";
// private final static String adjustedDirectoryPath2 = "aligned-image-3.png/mask/image-3";
public static void main(String[] args) {
launch(args);
}
@Override
protected void fillGrid(GridPane mainGrid) {
int columnIndex = 0;
int rowIndex = 0;
ImgClass imgClass = ImgClass.fromDirectory(classImgRepertory);
double dx = 5;
double dy = 5;
Mat imageToZone = highlight(imgClass.computeRangedMean(new Scalar(0, 0, 0), new Scalar(255, 255, 90), true, false), new Scalar(20, 20, 20));
// ImgZoner.drawZones(imageToZone, 1, new Scalar(255, 255, 255), -1);
// ImgZoner.drawAdjustedZones(imageToZone, dx, dy, new Scalar(0, 255, 0), 3);
mainGrid.add(buildImageViewFromMat(imageToZone), columnIndex, rowIndex++);
Mat imageToZone2 = imgClass.computeRangedVariance(new Scalar(0, 0, 0), new Scalar(255, 255, 82), true);
// ImgZoner.drawZones(imageToZone2, 1, new Scalar(255, 255, 255), -1);
mainGrid.add(buildImageViewFromMat(imageToZone2), columnIndex, rowIndex++);
// mainGrid.add(buildImageViewFromMat(imgClass.getAverage()), columnIndex, rowIndex++);
// mainGrid.add(buildImageViewFromMat(imgClass.getVariance()), columnIndex, rowIndex++);
Mat bluredVariance = imgClass.computeBluredVariance(new Size(15, 15));
Imgproc.cvtColor(bluredVariance, bluredVariance, Imgproc.COLOR_BGR2HSV);
Mat mask = new Mat();
Core.inRange(bluredVariance, new Scalar(0, 0, 0), new Scalar(255, 1, 255), mask);
Mat result = new Mat(bluredVariance.size(), bluredVariance.type(), new Scalar(0, 0, 0));
bluredVariance.copyTo(result, mask);
Imgproc.cvtColor(result, result, Imgproc.COLOR_HSV2BGR);
// Imgproc.dilate(result, result, Imgproc.getStructuringElement(Imgproc.MORPH_CROSS, new Size(17, 3)));
// Imgproc.GaussianBlur(result, result, new Size(17, 3), 0);
ImgZoner.drawAdjustedZones(result, 500, dx, dy, new Scalar(0, 255, 0), 3);
mainGrid.add(buildImageViewFromMat(result), columnIndex, rowIndex++);
Mat bluredVariance2 = imgClass.computeBluredVariance(new Size(31, 31));
Imgproc.cvtColor(bluredVariance2, bluredVariance2, Imgproc.COLOR_BGR2HSV);
Mat mask2 = new Mat();
Core.inRange(bluredVariance2, new Scalar(0, 0, 0), new Scalar(255, 1, 255), mask2);
Mat result2 = new Mat(bluredVariance2.size(), bluredVariance2.type(), new Scalar(0, 0, 0));
bluredVariance2.copyTo(result2, mask2);
Imgproc.cvtColor(result2, result2, Imgproc.COLOR_HSV2BGR);
// Imgproc.dilate(result2, result2, Imgproc.getStructuringElement(Imgproc.MORPH_CROSS, new Size(17, 3)));
// Imgproc.GaussianBlur(result2, result2, new Size(17, 3), 0);
ImgZoner.drawAdjustedZones(result2, 500, dx, dy, new Scalar(0, 255, 0), 3);
mainGrid.add(buildImageViewFromMat(result2), columnIndex, rowIndex++);
// mainGrid.add(buildImageViewFromMat(imgClass.computeBluredVariance(new Size(31, 31))), columnIndex, rowIndex++);
//
// // mainGrid.add(buildImageViewFromMat(imgClass.computeRangedMean(new Scalar(0, 0, 0), new Scalar(220, 180, 230), true, true)), columnIndex, rowIndex++);
// // mainGrid.add(buildImageViewFromMat(imgClass.computeRangedVariance(new Scalar(0, 0, 0), new Scalar(255, 255, 60), true)), columnIndex, rowIndex++);
// // mainGrid.add(buildImageViewFromMat(highlight(imgClass.computeRangedVariance(new Scalar(0, 0, 0), new Scalar(255, 255, 60), true), 40)), columnIndex, rowIndex++);
//
// mainGrid.add(buildImageViewFromMat(Kmeans.colorMapKMeans(imgClass.getAverage(), 4)), columnIndex, rowIndex++);
// mainGrid.add(buildImageViewFromMat(Kmeans.colorMapKMeans(imgClass.getVariance(), 4)), columnIndex, rowIndex++);
//
// List<Mat> clusters = Kmeans.cluster(imgClass.getAverage(), 4);
// mainGrid.add(buildImageViewFromMat(clusters.get(0)), columnIndex, rowIndex++);
// mainGrid.add(buildImageViewFromMat(clusters.get(1)), columnIndex, rowIndex++);
// mainGrid.add(buildImageViewFromMat(clusters.get(2)), columnIndex, rowIndex++);
// mainGrid.add(buildImageViewFromMat(clusters.get(3)), columnIndex, rowIndex++);
// mainGrid.add(buildImageViewFromMat(clusters.get(4)), columnIndex, rowIndex++);
// mainGrid.add(buildImageViewFromMat(clusters.get(5)), columnIndex, rowIndex++);
// Mat img = Imgcodecs.imread("aligned-image-3.png/image-3.png");
// Mat canny = new Mat();
// Imgproc.cvtColor(img, img, Imgproc.COLOR_BGR2GRAY);
// Imgproc.Canny(img, canny, canyThreshold1, canyThreshold2);
// mainGrid.add(buildImageViewFromMat(canny), columnIndex, rowIndex++);
// // highlight(imgClass.computeRangedMean(new Scalar(220, 0, 0), new Scalar(240, 180, 230), true, true), 1)
//
// List<Mat> sameMats = Tools.getClassMats(/* "aligned-image-3.png/mask/image-3" */ "aligned-image-3.png/foreground/image-3");
// sameMats.addAll(Tools.getImages("aligned-image-3.png", "image-3.png"));
// List<String> bestOcrs = new ArrayList<>();
//
// Map<Zone, Double> scores = new HashMap<>();
// for (Zone zone : zones) {
// Zone adjusted = zone.adjustRect(dx, dy, sameMats.get(0).width(), sameMats.get(0).height());
// String scoredText = adjusted.computeUnsupervisedScoredText(sameMats);
// scores.put(zone, adjusted.computeUnsupervisedScore(sameMats));
// bestOcrs.add(scoredText);
// System.out.println(scoredText);
// }
//
// Mat scoredMat = sameMats.get(sameMats.size() - 1).clone();
//
// for (Zone zone : zones) {
// Zone adjusted = zone.adjustRect(dx, dy, sameMats.get(0).width(), sameMats.get(0).height());
// adjusted.draw(scoredMat, new Scalar(0, 255, 0), 1);
// adjusted.write(scoredMat, "" + Math.floor((scores.get(zone) * 10000)) / 100 + "%", 1.8, new Scalar(0, 0, 255), 2);
// }
//
// mainGrid.add(buildImageViewFromMat(scoredMat), columnIndex, rowIndex++);
//
// VBox vbox = new VBox();
// bestOcrs.forEach(ocr -> vbox.getChildren().add(new Label(ocr)));
// mainGrid.add(vbox, columnIndex, rowIndex++);
}
private Mat getVariance(List<Mat> mats) {
Mat average = adjust(mats.get(0));
Mat nVariance = new Mat(average.size(), CvType.CV_32S, new Scalar(0, 0, 0));
for (int n = 1; n < 10 * mats.size(); n++)
computeImage(average, nVariance, adjust(mats.get(n % mats.size())), n + 1);
return normalize(nVariance, mats.size());
}
private Mat normalize(Mat nVariance, int n) {
Mat variance = new Mat();
Core.multiply(nVariance, new Scalar(1 / Integer.valueOf(n).doubleValue()), variance);
Core.convertScaleAbs(variance, variance);
return variance;
}
private Mat highlight(Mat variance, Scalar scalar) {
Mat superVariance = new Mat();
Core.multiply(variance, scalar, superVariance);
// Imgproc.dilate(superVariance, superVariance, Imgproc.getStructuringElement(Imgproc.MORPH_CROSS, new Size(17, 3)));
// Imgproc.GaussianBlur(superVariance, superVariance, new Size(17, 3), 0);
return superVariance;
}
private static void computeImage(Mat average, Mat nVariance, Mat adjusted, int n) {
Mat mask = Mat.ones(nVariance.size(), CvType.CV_8U);
Mat delta = new Mat(nVariance.size(), CvType.CV_32S);
Core.subtract(adjusted, average, delta, mask, CvType.CV_32S);
Core.addWeighted(average, 1, delta, 1 / Integer.valueOf(n).doubleValue(), 0, average, average.type());
Mat delta2 = new Mat(nVariance.size(), CvType.CV_32S);
Core.subtract(adjusted, average, delta2, mask, CvType.CV_32S);
Mat product = delta.mul(delta2);
Core.add(nVariance, product, nVariance);
}
public static Mat adjust(Mat frame) {
Mat mask = new Mat();
Core.inRange(frame, new Scalar(0, 0, 0), new Scalar(80, 255, 255), mask);
Mat masked = new Mat();
frame.copyTo(masked, mask);
Mat grey = new Mat();
Imgproc.cvtColor(masked, grey, Imgproc.COLOR_BGR2GRAY);
return grey;
}
// 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;
// }
// private Mat equalizeHisto(Mat mat) {
// Mat result = new Mat();
// Imgproc.cvtColor(mat, result, Imgproc.COLOR_BGR2YCrCb);
// List<Mat> channels = new ArrayList<Mat>();
// Core.split(result, channels);
// Imgproc.equalizeHist(channels.get(0), channels.get(0));
// // Imgproc.equalizeHist(channels.get(1), channels.get(1));
// // Imgproc.equalizeHist(channels.get(2), channels.get(2));
// Core.merge(channels, result);
// Imgproc.cvtColor(result, result, Imgproc.COLOR_YCrCb2BGR);
// return result;
// }
//
// private Mat prepareOcr(Mat mat) {
// // Mat tmp = new Mat();
// // Imgproc.blur(mat, tmp, new Size(3, 3));
// // Mat tmp = equalizeHisto(mat);
// // Mat tmp = Kmeans.colorMapKMeans(mat, 7);
// // Mat tmp = new Mat();
// // Imgproc.blur(tmp, tmp, new Size(3, 3));
// // Imgproc.cvtColor(mat, tmp, Imgproc.COLOR_BGR2GRAY);
// // / Imgproc.adaptiveThreshold(tmp, tmp, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY, 11, 2);
// // Imgproc.threshold(tmp, tmp, 0, 255, Imgproc.THRESH_BINARY + Imgproc.THRESH_OTSU);
// // Mat result = new Mat();
// // Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_GRAY2BGR);
// return mat;
// }
}