Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/features2d/misc/java/test/SURFFeatureDetectorTest.java
16354 views
1
package org.opencv.test.features2d;
2
3
import java.util.ArrayList;
4
import java.util.Arrays;
5
import java.util.Collections;
6
import java.util.Comparator;
7
import java.util.List;
8
9
import org.opencv.core.CvType;
10
import org.opencv.core.Mat;
11
import org.opencv.core.MatOfKeyPoint;
12
import org.opencv.core.Point;
13
import org.opencv.core.Scalar;
14
import org.opencv.core.KeyPoint;
15
import org.opencv.test.OpenCVTestCase;
16
import org.opencv.test.OpenCVTestRunner;
17
import org.opencv.imgproc.Imgproc;
18
import org.opencv.features2d.Feature2D;
19
20
public class SURFFeatureDetectorTest extends OpenCVTestCase {
21
22
Feature2D detector;
23
int matSize;
24
KeyPoint[] truth;
25
26
private Mat getMaskImg() {
27
Mat mask = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
28
Mat right = mask.submat(0, matSize, matSize / 2, matSize);
29
right.setTo(new Scalar(0));
30
return mask;
31
}
32
33
private Mat getTestImg() {
34
Mat cross = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
35
Imgproc.line(cross, new Point(20, matSize / 2), new Point(matSize - 21, matSize / 2), new Scalar(100), 2);
36
Imgproc.line(cross, new Point(matSize / 2, 20), new Point(matSize / 2, matSize - 21), new Scalar(100), 2);
37
38
return cross;
39
}
40
41
private void order(List<KeyPoint> points) {
42
Collections.sort(points, new Comparator<KeyPoint>() {
43
public int compare(KeyPoint p1, KeyPoint p2) {
44
if (p1.angle < p2.angle)
45
return -1;
46
if (p1.angle > p2.angle)
47
return 1;
48
return 0;
49
}
50
});
51
}
52
53
@Override
54
protected void setUp() throws Exception {
55
super.setUp();
56
detector = createClassInstance(XFEATURES2D+"SURF", DEFAULT_FACTORY, null, null);
57
matSize = 100;
58
truth = new KeyPoint[] {
59
new KeyPoint(55.775578f, 55.775578f, 16, 80.245735f, 8617.8633f, 0, -1),
60
new KeyPoint(44.224422f, 55.775578f, 16, 170.24574f, 8617.8633f, 0, -1),
61
new KeyPoint(44.224422f, 44.224422f, 16, 260.24573f, 8617.8633f, 0, -1),
62
new KeyPoint(55.775578f, 44.224422f, 16, 350.24573f, 8617.8633f, 0, -1)
63
};
64
}
65
66
public void testCreate() {
67
assertNotNull(detector);
68
}
69
70
public void testDetectListOfMatListOfListOfKeyPoint() {
71
72
setProperty(detector, "hessianThreshold", "double", 8000);
73
setProperty(detector, "nOctaves", "int", 3);
74
setProperty(detector, "nOctaveLayers", "int", 4);
75
setProperty(detector, "upright", "boolean", false);
76
77
List<MatOfKeyPoint> keypoints = new ArrayList<MatOfKeyPoint>();
78
Mat cross = getTestImg();
79
List<Mat> crosses = new ArrayList<Mat>(3);
80
crosses.add(cross);
81
crosses.add(cross);
82
crosses.add(cross);
83
84
detector.detect(crosses, keypoints);
85
86
assertEquals(3, keypoints.size());
87
88
for (MatOfKeyPoint mkp : keypoints) {
89
List<KeyPoint> lkp = mkp.toList();
90
order(lkp);
91
assertListKeyPointEquals(Arrays.asList(truth), lkp, EPS);
92
}
93
}
94
95
public void testDetectListOfMatListOfListOfKeyPointListOfMat() {
96
fail("Not yet implemented");
97
}
98
99
public void testDetectMatListOfKeyPoint() {
100
101
setProperty(detector, "hessianThreshold", "double", 8000);
102
setProperty(detector, "nOctaves", "int", 3);
103
setProperty(detector, "nOctaveLayers", "int", 4);
104
setProperty(detector, "upright", "boolean", false);
105
106
MatOfKeyPoint keypoints = new MatOfKeyPoint();
107
Mat cross = getTestImg();
108
109
detector.detect(cross, keypoints);
110
111
List<KeyPoint> lkp = keypoints.toList();
112
order(lkp);
113
assertListKeyPointEquals(Arrays.asList(truth), lkp, EPS);
114
}
115
116
public void testDetectMatListOfKeyPointMat() {
117
118
setProperty(detector, "hessianThreshold", "double", 8000);
119
setProperty(detector, "nOctaves", "int", 3);
120
setProperty(detector, "nOctaveLayers", "int", 4);
121
setProperty(detector, "upright", "boolean", false);
122
123
Mat img = getTestImg();
124
Mat mask = getMaskImg();
125
MatOfKeyPoint keypoints = new MatOfKeyPoint();
126
127
detector.detect(img, keypoints, mask);
128
129
List<KeyPoint> lkp = keypoints.toList();
130
order(lkp);
131
assertListKeyPointEquals(Arrays.asList(truth[1], truth[2]), lkp, EPS);
132
}
133
134
public void testEmpty() {
135
// assertFalse(detector.empty());
136
fail("Not yet implemented");
137
}
138
139
public void testRead() {
140
Mat cross = getTestImg();
141
142
MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
143
detector.detect(cross, keypoints1);
144
145
String filename = OpenCVTestRunner.getTempFileName("yml");
146
writeFile(filename, "%YAML:1.0\n---\nhessianThreshold: 8000.\noctaves: 3\noctaveLayers: 4\nupright: 0\n");
147
detector.read(filename);
148
149
MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
150
detector.detect(cross, keypoints2);
151
152
assertTrue(keypoints2.total() <= keypoints1.total());
153
}
154
155
public void testWrite() {
156
String filename = OpenCVTestRunner.getTempFileName("xml");
157
158
detector.write(filename);
159
160
// String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n<name>Feature2D.SURF</name>\n<extended>0</extended>\n<hessianThreshold>100.</hessianThreshold>\n<nOctaveLayers>3</nOctaveLayers>\n<nOctaves>4</nOctaves>\n<upright>0</upright>\n</opencv_storage>\n";
161
String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n</opencv_storage>\n";
162
assertEquals(truth, readFile(filename));
163
}
164
165
public void testWriteYml() {
166
String filename = OpenCVTestRunner.getTempFileName("yml");
167
168
detector.write(filename);
169
170
// String truth = "%YAML:1.0\n---\nname: \"Feature2D.SURF\"\nextended: 0\nhessianThreshold: 100.\nnOctaveLayers: 3\nnOctaves: 4\nupright: 0\n";
171
String truth = "%YAML:1.0\n---\n";
172
assertEquals(truth, readFile(filename));
173
}
174
175
}
176
177