1 package org.opencv.test.features2d;
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.features2d.DescriptorExtractor;
10 import org.opencv.core.KeyPoint;
11 import org.opencv.test.OpenCVTestCase;
12 import org.opencv.test.OpenCVTestRunner;
13 import org.opencv.imgproc.Imgproc;
15 public class ORBDescriptorExtractorTest extends OpenCVTestCase {
17 DescriptorExtractor extractor;
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);
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);
34 protected void setUp() throws Exception {
36 extractor = DescriptorExtractor.create(DescriptorExtractor.ORB);
40 public void testComputeListOfMatListOfListOfKeyPointListOfMat() {
41 fail("Not yet implemented");
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();
50 extractor.compute(img, keypoints, descriptors);
52 Mat truth = new Mat(1, 32, CvType.CV_8UC1) {
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);
58 assertDescriptorsClose(truth, descriptors, 1);
61 public void testCreate() {
62 assertNotNull(extractor);
65 public void testDescriptorSize() {
66 assertEquals(32, extractor.descriptorSize());
69 public void testDescriptorType() {
70 assertEquals(CvType.CV_8U, extractor.descriptorType());
73 public void testEmpty() {
74 assertFalse(extractor.empty());
77 public void testRead() {
78 KeyPoint point = new KeyPoint(55.775577545166016f, 44.224422454833984f, 16, 9.754629f, 8617.863f, 1, -1);
79 MatOfKeyPoint keypoints = new MatOfKeyPoint(point);
80 Mat img = getTestImg();
81 Mat descriptors = new Mat();
83 String filename = OpenCVTestRunner.getTempFileName("yml");
84 writeFile(filename, "%YAML:1.0\nscaleFactor: 1.1\nnLevels: 3\nfirstLevel: 0\nedgeThreshold: 31\npatchSize: 31\n");
85 extractor.read(filename);
87 extractor.compute(img, keypoints, descriptors);
89 Mat truth = new Mat(1, 32, CvType.CV_8UC1) {
92 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 assertDescriptorsClose(truth, descriptors, 1);
98 public void testWrite() {
99 String filename = OpenCVTestRunner.getTempFileName("xml");
101 extractor.write(filename);
103 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";
104 String actual = readFile(filename);
105 actual = actual.replaceAll("e\\+000", "e+00"); // NOTE: workaround for different platforms double representation
106 assertEquals(truth, actual);
109 public void testWriteYml() {
110 String filename = OpenCVTestRunner.getTempFileName("yml");
112 extractor.write(filename);
114 String truth = "%YAML:1.0\nname: \"Feature2D.ORB\"\nWTA_K: 2\nedgeThreshold: 31\nfirstLevel: 0\nnFeatures: 500\nnLevels: 8\npatchSize: 31\nscaleFactor: 1.2000000476837158e+00\nscoreType: 0\n";
115 String actual = readFile(filename);
116 actual = actual.replaceAll("e\\+000", "e+00"); // NOTE: workaround for different platforms double representation
117 assertEquals(truth, actual);