STARFeatureDetectorTest.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package org.opencv.test.features2d;
  2. import java.util.Arrays;
  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.test.OpenCVTestCase;
  10. import org.opencv.test.OpenCVTestRunner;
  11. import org.opencv.imgproc.Imgproc;
  12. import org.opencv.features2d.Feature2D;
  13. public class STARFeatureDetectorTest extends OpenCVTestCase {
  14. Feature2D detector;
  15. int matSize;
  16. KeyPoint[] truth;
  17. private Mat getMaskImg() {
  18. Mat mask = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
  19. Mat right = mask.submat(0, matSize, matSize / 2, matSize);
  20. right.setTo(new Scalar(0));
  21. return mask;
  22. }
  23. private Mat getTestImg() {
  24. Scalar color = new Scalar(0);
  25. int center = matSize / 2;
  26. int radius = 6;
  27. int offset = 40;
  28. Mat img = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
  29. Imgproc.circle(img, new Point(center - offset, center), radius, color, -1);
  30. Imgproc.circle(img, new Point(center + offset, center), radius, color, -1);
  31. Imgproc.circle(img, new Point(center, center - offset), radius, color, -1);
  32. Imgproc.circle(img, new Point(center, center + offset), radius, color, -1);
  33. Imgproc.circle(img, new Point(center, center), radius, color, -1);
  34. return img;
  35. }
  36. protected void setUp() throws Exception {
  37. super.setUp();
  38. detector = createClassInstance(XFEATURES2D+"StarDetector", DEFAULT_FACTORY, null, null);
  39. matSize = 200;
  40. truth = new KeyPoint[] {
  41. new KeyPoint( 95, 80, 22, -1, 31.5957f, 0, -1),
  42. new KeyPoint(105, 80, 22, -1, 31.5957f, 0, -1),
  43. new KeyPoint( 80, 95, 22, -1, 31.5957f, 0, -1),
  44. new KeyPoint(120, 95, 22, -1, 31.5957f, 0, -1),
  45. new KeyPoint(100, 100, 8, -1, 30.f, 0, -1),
  46. new KeyPoint( 80, 105, 22, -1, 31.5957f, 0, -1),
  47. new KeyPoint(120, 105, 22, -1, 31.5957f, 0, -1),
  48. new KeyPoint( 95, 120, 22, -1, 31.5957f, 0, -1),
  49. new KeyPoint(105, 120, 22, -1, 31.5957f, 0, -1)
  50. };
  51. }
  52. public void testCreate() {
  53. assertNotNull(detector);
  54. }
  55. public void testDetectListOfMatListOfListOfKeyPoint() {
  56. fail("Not yet implemented");
  57. }
  58. public void testDetectListOfMatListOfListOfKeyPointListOfMat() {
  59. fail("Not yet implemented");
  60. }
  61. public void testDetectMatListOfKeyPoint() {
  62. Mat img = getTestImg();
  63. MatOfKeyPoint keypoints = new MatOfKeyPoint();
  64. detector.detect(img, keypoints);
  65. assertListKeyPointEquals(Arrays.asList(truth), keypoints.toList(), EPS);
  66. }
  67. public void testDetectMatListOfKeyPointMat() {
  68. Mat img = getTestImg();
  69. Mat mask = getMaskImg();
  70. MatOfKeyPoint keypoints = new MatOfKeyPoint();
  71. detector.detect(img, keypoints, mask);
  72. assertListKeyPointEquals(Arrays.asList(truth[0], truth[2], truth[5], truth[7]), keypoints.toList(), EPS);
  73. }
  74. public void testEmpty() {
  75. // assertFalse(detector.empty());
  76. fail("Not yet implemented");
  77. }
  78. public void testRead() {
  79. Mat img = getTestImg();
  80. MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
  81. detector.detect(img, keypoints1);
  82. String filename = OpenCVTestRunner.getTempFileName("yml");
  83. writeFile(filename, "%YAML:1.0\n---\nmaxSize: 45\nresponseThreshold: 150\nlineThresholdProjected: 10\nlineThresholdBinarized: 8\nsuppressNonmaxSize: 5\n");
  84. detector.read(filename);
  85. MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
  86. detector.detect(img, keypoints2);
  87. assertTrue(keypoints2.total() <= keypoints1.total());
  88. }
  89. public void testWrite() {
  90. String filename = OpenCVTestRunner.getTempFileName("xml");
  91. detector.write(filename);
  92. // String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n<name>Feature2D.STAR</name>\n<lineThresholdBinarized>8</lineThresholdBinarized>\n<lineThresholdProjected>10</lineThresholdProjected>\n<maxSize>45</maxSize>\n<responseThreshold>30</responseThreshold>\n<suppressNonmaxSize>5</suppressNonmaxSize>\n</opencv_storage>\n";
  93. String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n</opencv_storage>\n";
  94. assertEquals(truth, readFile(filename));
  95. }
  96. public void testWriteYml() {
  97. String filename = OpenCVTestRunner.getTempFileName("yml");
  98. detector.write(filename);
  99. // String truth = "%YAML:1.0\n---\nname: \"Feature2D.STAR\"\nlineThresholdBinarized: 8\nlineThresholdProjected: 10\nmaxSize: 45\nresponseThreshold: 30\nsuppressNonmaxSize: 5\n";
  100. String truth = "%YAML:1.0\n---\n";
  101. assertEquals(truth, readFile(filename));
  102. }
  103. }