SURFFeatureDetectorTest.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package org.opencv.test.features2d;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.Collections;
  5. import java.util.Comparator;
  6. import java.util.List;
  7. import org.opencv.core.CvType;
  8. import org.opencv.core.Mat;
  9. import org.opencv.core.MatOfKeyPoint;
  10. import org.opencv.core.Point;
  11. import org.opencv.core.Scalar;
  12. import org.opencv.core.KeyPoint;
  13. import org.opencv.test.OpenCVTestCase;
  14. import org.opencv.test.OpenCVTestRunner;
  15. import org.opencv.imgproc.Imgproc;
  16. import org.opencv.features2d.Feature2D;
  17. public class SURFFeatureDetectorTest extends OpenCVTestCase {
  18. Feature2D detector;
  19. int matSize;
  20. KeyPoint[] truth;
  21. private Mat getMaskImg() {
  22. Mat mask = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
  23. Mat right = mask.submat(0, matSize, matSize / 2, matSize);
  24. right.setTo(new Scalar(0));
  25. return mask;
  26. }
  27. private Mat getTestImg() {
  28. Mat cross = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
  29. Imgproc.line(cross, new Point(20, matSize / 2), new Point(matSize - 21, matSize / 2), new Scalar(100), 2);
  30. Imgproc.line(cross, new Point(matSize / 2, 20), new Point(matSize / 2, matSize - 21), new Scalar(100), 2);
  31. return cross;
  32. }
  33. private void order(List<KeyPoint> points) {
  34. Collections.sort(points, new Comparator<KeyPoint>() {
  35. public int compare(KeyPoint p1, KeyPoint p2) {
  36. if (p1.angle < p2.angle)
  37. return -1;
  38. if (p1.angle > p2.angle)
  39. return 1;
  40. return 0;
  41. }
  42. });
  43. }
  44. @Override
  45. protected void setUp() throws Exception {
  46. super.setUp();
  47. detector = createClassInstance(XFEATURES2D+"SURF", DEFAULT_FACTORY, null, null);
  48. matSize = 100;
  49. truth = new KeyPoint[] {
  50. new KeyPoint(55.775578f, 55.775578f, 16, 80.245735f, 8617.8633f, 0, -1),
  51. new KeyPoint(44.224422f, 55.775578f, 16, 170.24574f, 8617.8633f, 0, -1),
  52. new KeyPoint(44.224422f, 44.224422f, 16, 260.24573f, 8617.8633f, 0, -1),
  53. new KeyPoint(55.775578f, 44.224422f, 16, 350.24573f, 8617.8633f, 0, -1)
  54. };
  55. }
  56. public void testCreate() {
  57. assertNotNull(detector);
  58. }
  59. public void testDetectListOfMatListOfListOfKeyPoint() {
  60. setProperty(detector, "hessianThreshold", "double", 8000);
  61. setProperty(detector, "nOctaves", "int", 3);
  62. setProperty(detector, "nOctaveLayers", "int", 4);
  63. setProperty(detector, "upright", "boolean", false);
  64. List<MatOfKeyPoint> keypoints = new ArrayList<MatOfKeyPoint>();
  65. Mat cross = getTestImg();
  66. List<Mat> crosses = new ArrayList<Mat>(3);
  67. crosses.add(cross);
  68. crosses.add(cross);
  69. crosses.add(cross);
  70. detector.detect(crosses, keypoints);
  71. assertEquals(3, keypoints.size());
  72. for (MatOfKeyPoint mkp : keypoints) {
  73. List<KeyPoint> lkp = mkp.toList();
  74. order(lkp);
  75. assertListKeyPointEquals(Arrays.asList(truth), lkp, EPS);
  76. }
  77. }
  78. public void testDetectListOfMatListOfListOfKeyPointListOfMat() {
  79. fail("Not yet implemented");
  80. }
  81. public void testDetectMatListOfKeyPoint() {
  82. setProperty(detector, "hessianThreshold", "double", 8000);
  83. setProperty(detector, "nOctaves", "int", 3);
  84. setProperty(detector, "nOctaveLayers", "int", 4);
  85. setProperty(detector, "upright", "boolean", false);
  86. MatOfKeyPoint keypoints = new MatOfKeyPoint();
  87. Mat cross = getTestImg();
  88. detector.detect(cross, keypoints);
  89. List<KeyPoint> lkp = keypoints.toList();
  90. order(lkp);
  91. assertListKeyPointEquals(Arrays.asList(truth), lkp, EPS);
  92. }
  93. public void testDetectMatListOfKeyPointMat() {
  94. setProperty(detector, "hessianThreshold", "double", 8000);
  95. setProperty(detector, "nOctaves", "int", 3);
  96. setProperty(detector, "nOctaveLayers", "int", 4);
  97. setProperty(detector, "upright", "boolean", false);
  98. Mat img = getTestImg();
  99. Mat mask = getMaskImg();
  100. MatOfKeyPoint keypoints = new MatOfKeyPoint();
  101. detector.detect(img, keypoints, mask);
  102. List<KeyPoint> lkp = keypoints.toList();
  103. order(lkp);
  104. assertListKeyPointEquals(Arrays.asList(truth[1], truth[2]), lkp, EPS);
  105. }
  106. public void testEmpty() {
  107. // assertFalse(detector.empty());
  108. fail("Not yet implemented");
  109. }
  110. public void testRead() {
  111. Mat cross = getTestImg();
  112. MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
  113. detector.detect(cross, keypoints1);
  114. String filename = OpenCVTestRunner.getTempFileName("yml");
  115. writeFile(filename, "%YAML:1.0\n---\nhessianThreshold: 8000.\noctaves: 3\noctaveLayers: 4\nupright: 0\n");
  116. detector.read(filename);
  117. MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
  118. detector.detect(cross, keypoints2);
  119. assertTrue(keypoints2.total() <= keypoints1.total());
  120. }
  121. public void testWrite() {
  122. String filename = OpenCVTestRunner.getTempFileName("xml");
  123. detector.write(filename);
  124. // String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n<name>Feature2D.SURF</name>\n<extended>0</extended>\n<hessianThreshold>100.</hessianThreshold>\n<nOctaveLayers>3</nOctaveLayers>\n<nOctaves>4</nOctaves>\n<upright>0</upright>\n</opencv_storage>\n";
  125. String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n</opencv_storage>\n";
  126. assertEquals(truth, readFile(filename));
  127. }
  128. public void testWriteYml() {
  129. String filename = OpenCVTestRunner.getTempFileName("yml");
  130. detector.write(filename);
  131. // String truth = "%YAML:1.0\n---\nname: \"Feature2D.SURF\"\nextended: 0\nhessianThreshold: 100.\nnOctaveLayers: 3\nnOctaves: 4\nupright: 0\n";
  132. String truth = "%YAML:1.0\n---\n";
  133. assertEquals(truth, readFile(filename));
  134. }
  135. }