FASTFeatureDetectorTest.java 4.9 KB

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