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.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; 14 15 public class ORBDescriptorExtractorTest extends OpenCVTestCase { 16 17 DescriptorExtractor extractor; 18 int matSize; 19 assertDescriptorsClose(Mat expected, Mat actual, int allowedDistance)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 getTestImg()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 setUp()34 protected void setUp() throws Exception { 35 super.setUp(); 36 extractor = DescriptorExtractor.create(DescriptorExtractor.ORB); 37 matSize = 100; 38 } 39 testComputeListOfMatListOfListOfKeyPointListOfMat()40 public void testComputeListOfMatListOfListOfKeyPointListOfMat() { 41 fail("Not yet implemented"); 42 } 43 testComputeMatListOfKeyPointMat()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 testCreate()61 public void testCreate() { 62 assertNotNull(extractor); 63 } 64 testDescriptorSize()65 public void testDescriptorSize() { 66 assertEquals(32, extractor.descriptorSize()); 67 } 68 testDescriptorType()69 public void testDescriptorType() { 70 assertEquals(CvType.CV_8U, extractor.descriptorType()); 71 } 72 testEmpty()73 public void testEmpty() { 74 assertFalse(extractor.empty()); 75 } 76 testRead()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(); 82 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); 86 87 extractor.compute(img, keypoints, descriptors); 88 89 Mat truth = new Mat(1, 32, CvType.CV_8UC1) { 90 { 91 put(0, 0, 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); 93 } 94 }; 95 assertDescriptorsClose(truth, descriptors, 1); 96 } 97 testWrite()98 public void testWrite() { 99 String filename = OpenCVTestRunner.getTempFileName("xml"); 100 101 extractor.write(filename); 102 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); 107 } 108 testWriteYml()109 public void testWriteYml() { 110 String filename = OpenCVTestRunner.getTempFileName("yml"); 111 112 extractor.write(filename); 113 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); 118 } 119 120 } 121