SURFDescriptorExtractorTest.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package org.opencv.test.features2d;
  2. import org.opencv.core.CvType;
  3. import org.opencv.core.Mat;
  4. import org.opencv.core.MatOfKeyPoint;
  5. import org.opencv.core.Point;
  6. import org.opencv.core.Scalar;
  7. import org.opencv.core.KeyPoint;
  8. import org.opencv.test.OpenCVTestCase;
  9. import org.opencv.test.OpenCVTestRunner;
  10. import org.opencv.imgproc.Imgproc;
  11. import org.opencv.features2d.Feature2D;
  12. public class SURFDescriptorExtractorTest extends OpenCVTestCase {
  13. Feature2D extractor;
  14. int matSize;
  15. private Mat getTestImg() {
  16. Mat cross = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
  17. Imgproc.line(cross, new Point(20, matSize / 2), new Point(matSize - 21, matSize / 2), new Scalar(100), 2);
  18. Imgproc.line(cross, new Point(matSize / 2, 20), new Point(matSize / 2, matSize - 21), new Scalar(100), 2);
  19. return cross;
  20. }
  21. @Override
  22. protected void setUp() throws Exception {
  23. super.setUp();
  24. Class[] cParams = {double.class, int.class, int.class, boolean.class, boolean.class};
  25. Object[] oValues = {100, 2, 4, true, false};
  26. extractor = createClassInstance(XFEATURES2D+"SURF", DEFAULT_FACTORY, cParams, oValues);
  27. matSize = 100;
  28. }
  29. public void testComputeListOfMatListOfListOfKeyPointListOfMat() {
  30. fail("Not yet implemented");
  31. }
  32. public void testComputeMatListOfKeyPointMat() {
  33. KeyPoint point = new KeyPoint(55.775577545166016f, 44.224422454833984f, 16, 9.754629f, 8617.863f, 1, -1);
  34. MatOfKeyPoint keypoints = new MatOfKeyPoint(point);
  35. Mat img = getTestImg();
  36. Mat descriptors = new Mat();
  37. extractor.compute(img, keypoints, descriptors);
  38. Mat truth = new Mat(1, 128, CvType.CV_32FC1) {
  39. {
  40. put(0, 0,
  41. 0, 0, 0, 0, 0, 0, 0, 0, 0.058821894, 0.058821894, -0.045962855, 0.046261817, 0.0085156476,
  42. 0.0085754395, -0.0064509804, 0.0064509804, 0.00044069235, 0.00044069235, 0, 0, 0.00025723741,
  43. 0.00025723741, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.00025723741, 0.00025723741, -0.00044069235,
  44. 0.00044069235, 0, 0, 0.36278215, 0.36278215, -0.24688604, 0.26173124, 0.052068226, 0.052662034,
  45. -0.032815345, 0.032815345, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.0064523756,
  46. 0.0064523756, 0.0082002236, 0.0088908644, -0.059001274, 0.059001274, 0.045789491, 0.04648013,
  47. 0.11961588, 0.22789426, -0.01322381, 0.18291828, -0.14042182, 0.23973691, 0.073782086, 0.23769434,
  48. -0.027880307, 0.027880307, 0.049587864, 0.049587864, -0.33991757, 0.33991757, 0.21437603, 0.21437603,
  49. -0.0020763327, 0.0020763327, 0.006245892, 0.006245892, -0.04067041, 0.04067041, 0.019361559,
  50. 0.019361559, 0, 0, -0.0035977389, 0.0035977389, 0, 0, -0.00099993451, 0.00099993451, 0.040670406,
  51. 0.040670406, -0.019361559, 0.019361559, 0.006245892, 0.006245892, -0.0020763327, 0.0020763327,
  52. -0.00034532088, 0.00034532088, 0, 0, 0, 0, 0.00034532088, 0.00034532088, -0.00099993451,
  53. 0.00099993451, 0, 0, 0, 0, 0.0035977389, 0.0035977389
  54. );
  55. }
  56. };
  57. assertMatEqual(truth, descriptors, EPS);
  58. }
  59. public void testCreate() {
  60. assertNotNull(extractor);
  61. }
  62. public void testDescriptorSize() {
  63. assertEquals(128, extractor.descriptorSize());
  64. }
  65. public void testDescriptorType() {
  66. assertEquals(CvType.CV_32F, extractor.descriptorType());
  67. }
  68. public void testEmpty() {
  69. // assertFalse(extractor.empty());
  70. fail("Not yet implemented");
  71. }
  72. public void testRead() {
  73. String filename = OpenCVTestRunner.getTempFileName("yml");
  74. writeFile(filename, "%YAML:1.0\n---\nnOctaves: 4\nnOctaveLayers: 2\nextended: 1\nupright: 0\n");
  75. extractor.read(filename);
  76. assertEquals(128, extractor.descriptorSize());
  77. }
  78. public void testWrite() {
  79. String filename = OpenCVTestRunner.getTempFileName("xml");
  80. extractor.write(filename);
  81. // String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n<name>Feature2D.SURF</name>\n<extended>1</extended>\n<hessianThreshold>100.</hessianThreshold>\n<nOctaveLayers>2</nOctaveLayers>\n<nOctaves>4</nOctaves>\n<upright>0</upright>\n</opencv_storage>\n";
  82. String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n</opencv_storage>\n";
  83. assertEquals(truth, readFile(filename));
  84. }
  85. public void testWriteYml() {
  86. String filename = OpenCVTestRunner.getTempFileName("yml");
  87. extractor.write(filename);
  88. // String truth = "%YAML:1.0\n---\nname: \"Feature2D.SURF\"\nextended: 1\nhessianThreshold: 100.\nnOctaveLayers: 2\nnOctaves: 4\nupright: 0\n";
  89. String truth = "%YAML:1.0\n---\n";
  90. assertEquals(truth, readFile(filename));
  91. }
  92. }