Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/features2d/misc/java/test/ORBDescriptorExtractorTest.java
16354 views
1
package org.opencv.test.features2d;
2
3
import org.opencv.core.Core;
4
import org.opencv.core.CvType;
5
import org.opencv.core.Mat;
6
import org.opencv.core.MatOfKeyPoint;
7
import org.opencv.core.Point;
8
import org.opencv.core.Scalar;
9
import org.opencv.core.KeyPoint;
10
import org.opencv.features2d.ORB;
11
import org.opencv.test.OpenCVTestCase;
12
import org.opencv.test.OpenCVTestRunner;
13
import org.opencv.imgproc.Imgproc;
14
15
public class ORBDescriptorExtractorTest extends OpenCVTestCase {
16
17
ORB extractor;
18
int matSize;
19
20
public static void assertDescriptorsClose(Mat expected, Mat actual, int allowedDistance) {
21
double distance = Core.norm(expected, actual, Core.NORM_HAMMING);
22
assertTrue("expected:<" + allowedDistance + "> but was:<" + distance + ">", distance <= allowedDistance);
23
}
24
25
private Mat getTestImg() {
26
Mat cross = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
27
Imgproc.line(cross, new Point(20, matSize / 2), new Point(matSize - 21, matSize / 2), new Scalar(100), 2);
28
Imgproc.line(cross, new Point(matSize / 2, 20), new Point(matSize / 2, matSize - 21), new Scalar(100), 2);
29
30
return cross;
31
}
32
33
@Override
34
protected void setUp() throws Exception {
35
super.setUp();
36
extractor = ORB.create();
37
matSize = 100;
38
}
39
40
public void testComputeListOfMatListOfListOfKeyPointListOfMat() {
41
fail("Not yet implemented");
42
}
43
44
public void testComputeMatListOfKeyPointMat() {
45
KeyPoint point = new KeyPoint(55.775577545166016f, 44.224422454833984f, 16, 9.754629f, 8617.863f, 1, -1);
46
MatOfKeyPoint keypoints = new MatOfKeyPoint(point);
47
Mat img = getTestImg();
48
Mat descriptors = new Mat();
49
50
extractor.compute(img, keypoints, descriptors);
51
52
Mat truth = new Mat(1, 32, CvType.CV_8UC1) {
53
{
54
put(0, 0,
55
6, 74, 6, 129, 2, 130, 56, 0, 36, 132, 66, 165, 172, 6, 3, 72, 102, 61, 163, 214, 0, 144, 65, 232, 4, 32, 138, 129, 4, 21, 37, 88);
56
}
57
};
58
assertDescriptorsClose(truth, descriptors, 1);
59
}
60
61
public void testCreate() {
62
assertNotNull(extractor);
63
}
64
65
public void testDescriptorSize() {
66
assertEquals(32, extractor.descriptorSize());
67
}
68
69
public void testDescriptorType() {
70
assertEquals(CvType.CV_8U, extractor.descriptorType());
71
}
72
73
public void testEmpty() {
74
// assertFalse(extractor.empty());
75
fail("Not yet implemented"); // ORB does not override empty() method
76
}
77
78
public void testRead() {
79
KeyPoint point = new KeyPoint(55.775577545166016f, 44.224422454833984f, 16, 9.754629f, 8617.863f, 1, -1);
80
MatOfKeyPoint keypoints = new MatOfKeyPoint(point);
81
Mat img = getTestImg();
82
Mat descriptors = new Mat();
83
84
// String filename = OpenCVTestRunner.getTempFileName("yml");
85
// writeFile(filename, "%YAML:1.0\n---\nscaleFactor: 1.1\nnLevels: 3\nfirstLevel: 0\nedgeThreshold: 31\npatchSize: 31\n");
86
// extractor.read(filename);
87
extractor = ORB.create(500, 1.1f, 3, 31, 0, 2, ORB.HARRIS_SCORE, 31, 20);
88
89
extractor.compute(img, keypoints, descriptors);
90
91
Mat truth = new Mat(1, 32, CvType.CV_8UC1) {
92
{
93
put(0, 0,
94
6, 10, 22, 5, 2, 130, 56, 0, 44, 164, 66, 165, 140, 6, 1, 72, 38, 61, 163, 210, 0, 208, 1, 104, 4, 32, 10, 131, 0, 37, 37, 67);
95
}
96
};
97
assertDescriptorsClose(truth, descriptors, 1);
98
}
99
100
public void testWrite() {
101
String filename = OpenCVTestRunner.getTempFileName("xml");
102
103
extractor.write(filename);
104
105
// String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n<name>Feature2D.ORB</name>\n<WTA_K>2</WTA_K>\n<edgeThreshold>31</edgeThreshold>\n<firstLevel>0</firstLevel>\n<nFeatures>500</nFeatures>\n<nLevels>8</nLevels>\n<patchSize>31</patchSize>\n<scaleFactor>1.2000000476837158e+00</scaleFactor>\n<scoreType>0</scoreType>\n</opencv_storage>\n";
106
String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n</opencv_storage>\n";
107
String actual = readFile(filename);
108
actual = actual.replaceAll("e\\+000", "e+00"); // NOTE: workaround for different platforms double representation
109
assertEquals(truth, actual);
110
}
111
112
public void testWriteYml() {
113
String filename = OpenCVTestRunner.getTempFileName("yml");
114
115
extractor.write(filename);
116
117
// String truth = "%YAML:1.0\n---\nname: \"Feature2D.ORB\"\nWTA_K: 2\nedgeThreshold: 31\nfirstLevel: 0\nnFeatures: 500\nnLevels: 8\npatchSize: 31\nscaleFactor: 1.2000000476837158e+00\nscoreType: 0\n";
118
String truth = "%YAML:1.0\n---\n";
119
String actual = readFile(filename);
120
actual = actual.replaceAll("e\\+000", "e+00"); // NOTE: workaround for different platforms double representation
121
assertEquals(truth, actual);
122
}
123
124
}
125
126