FASTFeatureDetectorTest.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package org.opencv.test.features2d;
  2. import java.util.Arrays;
  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.FastFeatureDetector;
  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. public class FASTFeatureDetectorTest extends OpenCVTestCase {
  15. FastFeatureDetector detector;
  16. KeyPoint[] truth;
  17. private Mat getMaskImg() {
  18. Mat mask = new Mat(100, 100, CvType.CV_8U, new Scalar(255));
  19. Mat right = mask.submat(0, 100, 50, 100);
  20. right.setTo(new Scalar(0));
  21. return mask;
  22. }
  23. private Mat getTestImg() {
  24. Mat img = new Mat(100, 100, CvType.CV_8U, new Scalar(255));
  25. Imgproc.line(img, new Point(30, 30), new Point(70, 70), new Scalar(0), 8);
  26. return img;
  27. }
  28. @Override
  29. protected void setUp() throws Exception {
  30. super.setUp();
  31. detector = FastFeatureDetector.create();
  32. truth = new KeyPoint[] { new KeyPoint(32, 27, 7, -1, 254, 0, -1), new KeyPoint(27, 32, 7, -1, 254, 0, -1), new KeyPoint(73, 68, 7, -1, 254, 0, -1),
  33. new KeyPoint(68, 73, 7, -1, 254, 0, -1) };
  34. }
  35. public void testCreate() {
  36. assertNotNull(detector);
  37. }
  38. public void testDetectListOfMatListOfListOfKeyPoint() {
  39. fail("Not yet implemented");
  40. }
  41. public void testDetectListOfMatListOfListOfKeyPointListOfMat() {
  42. fail("Not yet implemented");
  43. }
  44. public void testDetectMatListOfKeyPoint() {
  45. Mat img = getTestImg();
  46. MatOfKeyPoint keypoints = new MatOfKeyPoint();
  47. detector.detect(img, keypoints);
  48. assertListKeyPointEquals(Arrays.asList(truth), keypoints.toList(), EPS);
  49. // OpenCVTestRunner.Log("points found: " + keypoints.size());
  50. // for (KeyPoint kp : keypoints)
  51. // OpenCVTestRunner.Log(kp.toString());
  52. }
  53. public void testDetectMatListOfKeyPointMat() {
  54. Mat img = getTestImg();
  55. Mat mask = getMaskImg();
  56. MatOfKeyPoint keypoints = new MatOfKeyPoint();
  57. detector.detect(img, keypoints, mask);
  58. assertListKeyPointEquals(Arrays.asList(truth[0], truth[1]), keypoints.toList(), EPS);
  59. }
  60. public void testEmpty() {
  61. // assertFalse(detector.empty());
  62. fail("Not yet implemented"); // FAST does not override empty() method
  63. }
  64. public void testRead() {
  65. String filename = OpenCVTestRunner.getTempFileName("xml");
  66. writeFile(filename, "<?xml version=\"1.0\"?>\n<opencv_storage>\n<name>Feature2D.FastFeatureDetector</name>\n<threshold>10</threshold>\n<nonmaxSuppression>1</nonmaxSuppression>\n<type>2</type>\n</opencv_storage>\n");
  67. detector.read(filename);
  68. assertEquals(10, detector.getThreshold());
  69. assertEquals(true, detector.getNonmaxSuppression());
  70. assertEquals(2, detector.getType());
  71. MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
  72. detector.detect(grayChess, keypoints1);
  73. writeFile(filename, "<?xml version=\"1.0\"?>\n<opencv_storage>\n<name>Feature2D.FastFeatureDetector</name>\n<threshold>150</threshold>\n<nonmaxSuppression>1</nonmaxSuppression>\n<type>2</type>\n</opencv_storage>\n");
  74. detector.read(filename);
  75. MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
  76. detector.detect(grayChess, keypoints2);
  77. assertTrue(keypoints2.total() <= keypoints1.total());
  78. }
  79. public void testReadYml() {
  80. String filename = OpenCVTestRunner.getTempFileName("yml");
  81. writeFile(filename, "%YAML:1.0\n---\nthreshold: 130\nnonmaxSuppression: 1\ntype: 2\n");
  82. detector.read(filename);
  83. assertEquals(130, detector.getThreshold());
  84. assertEquals(true, detector.getNonmaxSuppression());
  85. assertEquals(2, detector.getType());
  86. MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
  87. detector.detect(grayChess, keypoints1);
  88. writeFile(filename, "%YAML:1.0\n---\nthreshold: 150\nnonmaxSuppression: 1\ntype: 2\n");
  89. detector.read(filename);
  90. MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
  91. detector.detect(grayChess, keypoints2);
  92. assertTrue(keypoints2.total() <= keypoints1.total());
  93. }
  94. public void testWriteYml() {
  95. String filename = OpenCVTestRunner.getTempFileName("yml");
  96. detector.write(filename);
  97. String truth = "%YAML:1.0\n---\nname: \"Feature2D.FastFeatureDetector\"\nthreshold: 10\nnonmaxSuppression: 1\ntype: 2\n";
  98. String data = readFile(filename);
  99. assertEquals(truth, data);
  100. }
  101. }