SIMPLEBLOBFeatureDetectorTest.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. import org.opencv.features2d.SimpleBlobDetector;
  14. public class SIMPLEBLOBFeatureDetectorTest extends OpenCVTestCase {
  15. Feature2D detector;
  16. int matSize;
  17. KeyPoint[] truth;
  18. private Mat getMaskImg() {
  19. Mat mask = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
  20. Mat right = mask.submat(0, matSize, matSize / 2, matSize);
  21. right.setTo(new Scalar(0));
  22. return mask;
  23. }
  24. private Mat getTestImg() {
  25. int center = matSize / 2;
  26. int offset = 40;
  27. Mat img = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
  28. Imgproc.circle(img, new Point(center - offset, center), 24, new Scalar(0), -1);
  29. Imgproc.circle(img, new Point(center + offset, center), 20, new Scalar(50), -1);
  30. Imgproc.circle(img, new Point(center, center - offset), 18, new Scalar(100), -1);
  31. Imgproc.circle(img, new Point(center, center + offset), 14, new Scalar(150), -1);
  32. Imgproc.circle(img, new Point(center, center), 10, new Scalar(200), -1);
  33. return img;
  34. }
  35. @Override
  36. protected void setUp() throws Exception {
  37. super.setUp();
  38. detector = SimpleBlobDetector.create();
  39. matSize = 200;
  40. truth = new KeyPoint[] {
  41. new KeyPoint( 140, 100, 41.036568f, -1, 0, 0, -1),
  42. new KeyPoint( 60, 100, 48.538486f, -1, 0, 0, -1),
  43. new KeyPoint(100, 60, 36.769554f, -1, 0, 0, -1),
  44. new KeyPoint(100, 140, 28.635643f, -1, 0, 0, -1),
  45. new KeyPoint(100, 100, 20.880613f, -1, 0, 0, -1)
  46. };
  47. }
  48. public void testCreate() {
  49. assertNotNull(detector);
  50. }
  51. public void testDetectListOfMatListOfListOfKeyPoint() {
  52. fail("Not yet implemented");
  53. }
  54. public void testDetectListOfMatListOfListOfKeyPointListOfMat() {
  55. fail("Not yet implemented");
  56. }
  57. public void testDetectMatListOfKeyPoint() {
  58. Mat img = getTestImg();
  59. MatOfKeyPoint keypoints = new MatOfKeyPoint();
  60. detector.detect(img, keypoints);
  61. assertListKeyPointEquals(Arrays.asList(truth), keypoints.toList(), EPS);
  62. }
  63. public void testDetectMatListOfKeyPointMat() {
  64. Mat img = getTestImg();
  65. Mat mask = getMaskImg();
  66. MatOfKeyPoint keypoints = new MatOfKeyPoint();
  67. detector.detect(img, keypoints, mask);
  68. assertListKeyPointEquals(Arrays.asList(truth[1]), keypoints.toList(), EPS);
  69. }
  70. public void testEmpty() {
  71. // assertFalse(detector.empty());
  72. fail("Not yet implemented");
  73. }
  74. public void testRead() {
  75. Mat img = getTestImg();
  76. MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
  77. detector.detect(img, keypoints1);
  78. String filename = OpenCVTestRunner.getTempFileName("yml");
  79. writeFile(filename, "%YAML:1.0\nthresholdStep: 10\nminThreshold: 50\nmaxThreshold: 220\nminRepeatability: 2\nfilterByArea: true\nminArea: 800\nmaxArea: 5000\n");
  80. detector.read(filename);
  81. MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
  82. detector.detect(img, keypoints2);
  83. assertTrue(keypoints2.total() <= keypoints1.total());
  84. }
  85. public void testWrite() {
  86. String filename = OpenCVTestRunner.getTempFileName("xml");
  87. detector.write(filename);
  88. String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n<format>3</format>\n<thresholdStep>10.</thresholdStep>\n<minThreshold>50.</minThreshold>\n<maxThreshold>220.</maxThreshold>\n<minRepeatability>2</minRepeatability>\n<minDistBetweenBlobs>10.</minDistBetweenBlobs>\n<filterByColor>1</filterByColor>\n<blobColor>0</blobColor>\n<filterByArea>1</filterByArea>\n<minArea>25.</minArea>\n<maxArea>5000.</maxArea>\n<filterByCircularity>0</filterByCircularity>\n<minCircularity>8.0000001192092896e-01</minCircularity>\n<maxCircularity>3.4028234663852886e+38</maxCircularity>\n<filterByInertia>1</filterByInertia>\n<minInertiaRatio>1.0000000149011612e-01</minInertiaRatio>\n<maxInertiaRatio>3.4028234663852886e+38</maxInertiaRatio>\n<filterByConvexity>1</filterByConvexity>\n<minConvexity>9.4999998807907104e-01</minConvexity>\n<maxConvexity>3.4028234663852886e+38</maxConvexity>\n</opencv_storage>\n";
  89. assertEquals(truth, readFile(filename));
  90. }
  91. }