SIMPLEBLOBFeatureDetectorTest.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.SimpleBlobDetector;
  13. import org.opencv.features2d.SimpleBlobDetector_Params;
  14. public class SIMPLEBLOBFeatureDetectorTest extends OpenCVTestCase {
  15. SimpleBlobDetector 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 testReadYml() {
  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.0\nminThreshold: 50\nmaxThreshold: 220\nminRepeatability: 2\nminDistBetweenBlobs: 10.\nfilterByColor: 1\nblobColor: 0\nfilterByArea: 1\nminArea: 800\nmaxArea: 6000\nfilterByCircularity: 0\nminCircularity: 0.7\nmaxCircularity: 10.\nfilterByInertia: 1\nminInertiaRatio: 0.2\nmaxInertiaRatio: 11.\nfilterByConvexity: true\nminConvexity: 0.9\nmaxConvexity: 12.\n");
  80. detector.read(filename);
  81. SimpleBlobDetector_Params params = detector.getParams();
  82. assertEquals(10.0f, params.get_thresholdStep());
  83. assertEquals(50f, params.get_minThreshold());
  84. assertEquals(220f, params.get_maxThreshold());
  85. assertEquals(2, params.get_minRepeatability());
  86. assertEquals(10.0f, params.get_minDistBetweenBlobs());
  87. assertEquals(true, params.get_filterByColor());
  88. assertEquals(0, params.get_blobColor());
  89. assertEquals(true, params.get_filterByArea());
  90. assertEquals(800f, params.get_minArea());
  91. assertEquals(6000f, params.get_maxArea());
  92. assertEquals(false, params.get_filterByCircularity());
  93. assertEquals(0.7f, params.get_minCircularity());
  94. assertEquals(10.0f, params.get_maxCircularity());
  95. assertEquals(true, params.get_filterByInertia());
  96. assertEquals(0.2f, params.get_minInertiaRatio());
  97. assertEquals(11.0f, params.get_maxInertiaRatio());
  98. assertEquals(true, params.get_filterByConvexity());
  99. assertEquals(0.9f, params.get_minConvexity());
  100. assertEquals(12.0f, params.get_maxConvexity());
  101. MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
  102. detector.detect(img, keypoints2);
  103. assertTrue(keypoints2.total() <= keypoints1.total());
  104. }
  105. public void testWrite() {
  106. String filename = OpenCVTestRunner.getTempFileName("xml");
  107. detector.write(filename);
  108. 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>0.80000001192092896</minCircularity>\n<maxCircularity>3.4028234663852886e+38</maxCircularity>\n<filterByInertia>1</filterByInertia>\n<minInertiaRatio>0.10000000149011612</minInertiaRatio>\n<maxInertiaRatio>3.4028234663852886e+38</maxInertiaRatio>\n<filterByConvexity>1</filterByConvexity>\n<minConvexity>0.94999998807907104</minConvexity>\n<maxConvexity>3.4028234663852886e+38</maxConvexity>\n<collectContours>0</collectContours>\n</opencv_storage>\n";
  109. assertEquals(truth, readFile(filename));
  110. }
  111. }