-
Notifications
You must be signed in to change notification settings - Fork 5
/
ClassifierParametersTest.java
132 lines (116 loc) · 4.15 KB
/
ClassifierParametersTest.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
package org.genericsystem.cv;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.Size;
import org.opencv.features2d.DescriptorExtractor;
import org.opencv.features2d.FeatureDetector;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
public class ClassifierParametersTest extends Application {
private final static String pngDirectoryPath = "png";
private final static String adjustedDirectoryPath = "adjusted";
private final double displayWidth = 200d;
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
GridPane gridPane = new GridPane();
List<File> pngImgs = Arrays.stream(new File(pngDirectoryPath).listFiles()).filter(img -> img.getName().endsWith(".png")).collect(Collectors.toList());
List<File> adjustedImages = Arrays.stream(new File(adjustedDirectoryPath).listFiles()).filter(img -> img.getName().endsWith(".png")).collect(Collectors.toList());
int row = 0;
// List<List<File>> classifiedFiles = partition(pngImgs, (file1, file2) -> Classifier.compareFeature(file1.getPath(), file2.getPath()) != null);
// for (List<File> typeList : classifiedFiles) {
// gridPane.add(new Separator(), 0, row);
// row++;
// int column = 0;
// for (File img : typeList) {
// Mat mat = Imgcodecs.imread(img.getPath());
// gridPane.add(getImageViewFromMat(mat), column++, row);
// }
// row++;
// }
for (File img1 : pngImgs) {
System.out.println("================== image : " + img1.getName());
int column = 0;
Mat mat1 = Imgcodecs.imread(img1.getPath());
gridPane.add(getImageViewFromMat(mat1), column++, row);
for (File img2 : adjustedImages) {
Mat mat2 = Imgcodecs.imread(img2.getPath());
gridPane.add(getImageViewFromMat(mat2), column++, row);
Mat result = Classifier.compareFeature(img1.getPath(), img2.getPath(), 50, FeatureDetector.GFTT, DescriptorExtractor.OPPONENT_ORB);
if (result != null)
gridPane.add(getImageViewFromMat(result), column++, row);
else
gridPane.add(new Label("Not matching"), column++, row);
}
row++;
}
Scene scene = new Scene(new Group());
stage.setTitle("Generic System Computer Vision");
ScrollPane scrollPane = new ScrollPane(gridPane);
scrollPane.setFitToHeight(true);
VBox root = new VBox(scrollPane);
scene.setRoot(root);
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
try {
stop();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
stage.setScene(scene);
stage.show();
}
private <T> List<List<T>> partition(List<T> elements, BiFunction<T, T, Boolean> equivalent) {
List<List<T>> results = new ArrayList<>();
elements.forEach(element -> {
for (List<T> testList : results) {
if (equivalent.apply(element, testList.get(0))) {
testList.add(element);
return;
}
}
List<T> newList = new ArrayList<>();
newList.add(element);
results.add(newList);
});
return results;
}
private ImageView getImageViewFromMat(Mat src) {
Mat target = new Mat();
Imgproc.resize(src, target, new Size(displayWidth, Math.floor((displayWidth / src.width()) * src.height())));
MatOfByte buffer = new MatOfByte();
Imgcodecs.imencode(".png", target, buffer);
ImageView imageView = new ImageView(new Image(new ByteArrayInputStream(buffer.toArray())));
imageView.setPreserveRatio(true);
imageView.setFitWidth(displayWidth);
return imageView;
}
}