ORBDescriptorExtractorTest.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package org.opencv.test.features2d;
  2. import org.opencv.core.Core;
  3. import org.opencv.core.CvType;
  4. import org.opencv.core.Mat;
  5. import org.opencv.core.MatOfKeyPoint;
  6. import org.opencv.core.Point;
  7. import org.opencv.core.Scalar;
  8. import org.opencv.core.KeyPoint;
  9. import org.opencv.features2d.ORB;
  10. import org.opencv.test.OpenCVTestCase;
  11. import org.opencv.test.OpenCVTestRunner;
  12. import org.opencv.imgproc.Imgproc;
  13. public class ORBDescriptorExtractorTest extends OpenCVTestCase {
  14. ORB extractor;
  15. int matSize;
  16. public static void assertDescriptorsClose(Mat expected, Mat actual, int allowedDistance) {
  17. double distance = Core.norm(expected, actual, Core.NORM_HAMMING);
  18. assertTrue("expected:<" + allowedDistance + "> but was:<" + distance + ">", distance <= allowedDistance);
  19. }
  20. private Mat getTestImg() {
  21. Mat cross = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
  22. Imgproc.line(cross, new Point(20, matSize / 2), new Point(matSize - 21, matSize / 2), new Scalar(100), 2);
  23. Imgproc.line(cross, new Point(matSize / 2, 20), new Point(matSize / 2, matSize - 21), new Scalar(100), 2);
  24. return cross;
  25. }
  26. @Override
  27. protected void setUp() throws Exception {
  28. super.setUp();
  29. extractor = ORB.create();
  30. matSize = 100;
  31. }
  32. public void testComputeListOfMatListOfListOfKeyPointListOfMat() {
  33. fail("Not yet implemented");
  34. }
  35. public void testComputeMatListOfKeyPointMat() {
  36. KeyPoint point = new KeyPoint(55.775577545166016f, 44.224422454833984f, 16, 9.754629f, 8617.863f, 1, -1);
  37. MatOfKeyPoint keypoints = new MatOfKeyPoint(point);
  38. Mat img = getTestImg();
  39. Mat descriptors = new Mat();
  40. extractor.compute(img, keypoints, descriptors);
  41. Mat truth = new Mat(1, 32, CvType.CV_8UC1) {
  42. {
  43. put(0, 0,
  44. 6, 74, 6, 129, 2, 130, 56, 0, 44, 132, 66, 165, 172, 6, 3, 72, 102, 61, 171, 214, 0, 144, 65, 232, 4, 32, 138, 131, 4, 21, 37, 217);
  45. }
  46. };
  47. assertDescriptorsClose(truth, descriptors, 1);
  48. }
  49. public void testCreate() {
  50. assertNotNull(extractor);
  51. }
  52. public void testDescriptorSize() {
  53. assertEquals(32, extractor.descriptorSize());
  54. }
  55. public void testDescriptorType() {
  56. assertEquals(CvType.CV_8U, extractor.descriptorType());
  57. }
  58. public void testEmpty() {
  59. // assertFalse(extractor.empty());
  60. fail("Not yet implemented"); // ORB does not override empty() method
  61. }
  62. public void testRead() {
  63. KeyPoint point = new KeyPoint(55.775577545166016f, 44.224422454833984f, 16, 9.754629f, 8617.863f, 1, -1);
  64. MatOfKeyPoint keypoints = new MatOfKeyPoint(point);
  65. Mat img = getTestImg();
  66. Mat descriptors = new Mat();
  67. // String filename = OpenCVTestRunner.getTempFileName("yml");
  68. // writeFile(filename, "%YAML:1.0\n---\nscaleFactor: 1.1\nnLevels: 3\nfirstLevel: 0\nedgeThreshold: 31\npatchSize: 31\n");
  69. // extractor.read(filename);
  70. extractor = ORB.create(500, 1.1f, 3, 31, 0, 2, ORB.HARRIS_SCORE, 31, 20);
  71. extractor.compute(img, keypoints, descriptors);
  72. Mat truth = new Mat(1, 32, CvType.CV_8UC1) {
  73. {
  74. put(0, 0,
  75. 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, 74, 131, 0, 37, 37, 67);
  76. }
  77. };
  78. assertDescriptorsClose(truth, descriptors, 1);
  79. }
  80. public void testWrite() {
  81. String filename = OpenCVTestRunner.getTempFileName("xml");
  82. extractor.write(filename);
  83. // 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";
  84. String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n</opencv_storage>\n";
  85. String actual = readFile(filename);
  86. actual = actual.replaceAll("e\\+000", "e+00"); // NOTE: workaround for different platforms double representation
  87. assertEquals(truth, actual);
  88. }
  89. public void testWriteYml() {
  90. String filename = OpenCVTestRunner.getTempFileName("yml");
  91. extractor.write(filename);
  92. // 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";
  93. String truth = "%YAML:1.0\n---\n";
  94. String actual = readFile(filename);
  95. actual = actual.replaceAll("e\\+000", "e+00"); // NOTE: workaround for different platforms double representation
  96. assertEquals(truth, actual);
  97. }
  98. }