Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/features2d/misc/java/test/FASTFeatureDetectorTest.java
16354 views
1
package org.opencv.test.features2d;
2
3
import java.util.Arrays;
4
5
import org.opencv.core.Core;
6
import org.opencv.core.CvType;
7
import org.opencv.core.Mat;
8
import org.opencv.core.MatOfKeyPoint;
9
import org.opencv.core.Point;
10
import org.opencv.core.Scalar;
11
import org.opencv.features2d.Feature2D;
12
import org.opencv.features2d.FastFeatureDetector;
13
import org.opencv.core.KeyPoint;
14
import org.opencv.test.OpenCVTestCase;
15
import org.opencv.test.OpenCVTestRunner;
16
import org.opencv.imgproc.Imgproc;
17
18
public class FASTFeatureDetectorTest extends OpenCVTestCase {
19
20
Feature2D detector;
21
KeyPoint[] truth;
22
23
private Mat getMaskImg() {
24
Mat mask = new Mat(100, 100, CvType.CV_8U, new Scalar(255));
25
Mat right = mask.submat(0, 100, 50, 100);
26
right.setTo(new Scalar(0));
27
return mask;
28
}
29
30
private Mat getTestImg() {
31
Mat img = new Mat(100, 100, CvType.CV_8U, new Scalar(255));
32
Imgproc.line(img, new Point(30, 30), new Point(70, 70), new Scalar(0), 8);
33
return img;
34
}
35
36
@Override
37
protected void setUp() throws Exception {
38
super.setUp();
39
detector = FastFeatureDetector.create();
40
truth = new KeyPoint[] { new KeyPoint(32, 27, 7, -1, 254, 0, -1), new KeyPoint(27, 32, 7, -1, 254, 0, -1), new KeyPoint(73, 68, 7, -1, 254, 0, -1),
41
new KeyPoint(68, 73, 7, -1, 254, 0, -1) };
42
}
43
44
public void testCreate() {
45
assertNotNull(detector);
46
}
47
48
public void testDetectListOfMatListOfListOfKeyPoint() {
49
fail("Not yet implemented");
50
}
51
52
public void testDetectListOfMatListOfListOfKeyPointListOfMat() {
53
fail("Not yet implemented");
54
}
55
56
public void testDetectMatListOfKeyPoint() {
57
Mat img = getTestImg();
58
MatOfKeyPoint keypoints = new MatOfKeyPoint();
59
60
detector.detect(img, keypoints);
61
62
assertListKeyPointEquals(Arrays.asList(truth), keypoints.toList(), EPS);
63
64
// OpenCVTestRunner.Log("points found: " + keypoints.size());
65
// for (KeyPoint kp : keypoints)
66
// OpenCVTestRunner.Log(kp.toString());
67
}
68
69
public void testDetectMatListOfKeyPointMat() {
70
Mat img = getTestImg();
71
Mat mask = getMaskImg();
72
MatOfKeyPoint keypoints = new MatOfKeyPoint();
73
74
detector.detect(img, keypoints, mask);
75
76
assertListKeyPointEquals(Arrays.asList(truth[0], truth[1]), keypoints.toList(), EPS);
77
}
78
79
public void testEmpty() {
80
// assertFalse(detector.empty());
81
fail("Not yet implemented"); //FAST does not override empty() method
82
}
83
84
public void testRead() {
85
String filename = OpenCVTestRunner.getTempFileName("yml");
86
87
writeFile(filename, "%YAML:1.0\n---\nthreshold: 130\nnonmaxSuppression: 1\n");
88
detector.read(filename);
89
90
MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
91
92
detector.detect(grayChess, keypoints1);
93
94
writeFile(filename, "%YAML:1.0\n---\nthreshold: 150\nnonmaxSuppression: 1\n");
95
detector.read(filename);
96
97
MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
98
99
detector.detect(grayChess, keypoints2);
100
101
assertTrue(keypoints2.total() <= keypoints1.total());
102
}
103
104
public void testReadYml() {
105
String filename = OpenCVTestRunner.getTempFileName("yml");
106
107
writeFile(filename,
108
"<?xml version=\"1.0\"?>\n<opencv_storage>\n<threshold>130</threshold>\n<nonmaxSuppression>1</nonmaxSuppression>\n</opencv_storage>\n");
109
detector.read(filename);
110
111
MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
112
113
detector.detect(grayChess, keypoints1);
114
115
writeFile(filename,
116
"<?xml version=\"1.0\"?>\n<opencv_storage>\n<threshold>150</threshold>\n<nonmaxSuppression>1</nonmaxSuppression>\n</opencv_storage>\n");
117
detector.read(filename);
118
119
MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
120
121
detector.detect(grayChess, keypoints2);
122
123
assertTrue(keypoints2.total() <= keypoints1.total());
124
}
125
126
public void testWrite() {
127
String filename = OpenCVTestRunner.getTempFileName("xml");
128
129
detector.write(filename);
130
131
// String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n<name>Feature2D.FAST</name>\n<nonmaxSuppression>1</nonmaxSuppression>\n<threshold>10</threshold>\n<type>2</type>\n</opencv_storage>\n";
132
String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n</opencv_storage>\n";
133
String data = readFile(filename);
134
//Log.d("qqq", "\"" + data + "\"");
135
assertEquals(truth, data);
136
}
137
138
public void testWriteYml() {
139
String filename = OpenCVTestRunner.getTempFileName("yml");
140
141
detector.write(filename);
142
143
// String truth = "%YAML:1.0\n---\nname: \"Feature2D.FAST\"\nnonmaxSuppression: 1\nthreshold: 10\ntype: 2\n";
144
String truth = "%YAML:1.0\n---\n";
145
String data = readFile(filename);
146
147
//Log.d("qqq", "\"" + data + "\"");
148
assertEquals(truth, data);
149
}
150
}
151
152