SIFTDescriptorExtractorTest.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.features2d.SIFT;
  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. public class SIFTDescriptorExtractorTest extends OpenCVTestCase {
  14. Feature2D extractor;
  15. KeyPoint keypoint;
  16. int matSize;
  17. Mat truth;
  18. private Mat getTestImg() {
  19. Mat cross = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
  20. Imgproc.line(cross, new Point(20, matSize / 2), new Point(matSize - 21, matSize / 2), new Scalar(100), 2);
  21. Imgproc.line(cross, new Point(matSize / 2, 20), new Point(matSize / 2, matSize - 21), new Scalar(100), 2);
  22. return cross;
  23. }
  24. @Override
  25. protected void setUp() throws Exception {
  26. super.setUp();
  27. extractor = SIFT.create();
  28. keypoint = new KeyPoint(55.775577545166016f, 44.224422454833984f, 16, 9.754629f, 8617.863f, 1, -1);
  29. matSize = 100;
  30. truth = new Mat(1, 128, CvType.CV_32FC1) {
  31. {
  32. put(0, 0,
  33. 0, 0, 0, 1, 3, 0, 0, 0, 15, 23, 22, 20, 24, 2, 0, 0, 7, 8, 2, 0,
  34. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 27, 16, 13, 2, 0, 0, 117,
  35. 86, 79, 68, 117, 42, 5, 5, 79, 60, 117, 25, 9, 2, 28, 19, 11, 13,
  36. 20, 2, 0, 0, 5, 8, 0, 0, 76, 58, 34, 31, 97, 16, 95, 49, 117, 92,
  37. 117, 112, 117, 76, 117, 54, 117, 25, 29, 22, 117, 117, 16, 11, 14,
  38. 1, 0, 0, 22, 26, 0, 0, 0, 0, 1, 4, 15, 2, 47, 8, 0, 0, 82, 56, 31,
  39. 17, 81, 12, 0, 0, 26, 23, 18, 23, 0, 0, 0, 0, 0, 0, 0, 0
  40. );
  41. }
  42. };
  43. }
  44. public void testComputeListOfMatListOfListOfKeyPointListOfMat() {
  45. fail("Not yet implemented");
  46. }
  47. public void testComputeMatListOfKeyPointMat() {
  48. MatOfKeyPoint keypoints = new MatOfKeyPoint(keypoint);
  49. Mat img = getTestImg();
  50. Mat descriptors = new Mat();
  51. extractor.compute(img, keypoints, descriptors);
  52. assertMatEqual(truth, descriptors, EPS);
  53. }
  54. public void testCreate() {
  55. assertNotNull(extractor);
  56. }
  57. public void testDescriptorSize() {
  58. assertEquals(128, extractor.descriptorSize());
  59. }
  60. public void testDescriptorType() {
  61. assertEquals(CvType.CV_32F, extractor.descriptorType());
  62. }
  63. public void testEmpty() {
  64. // assertFalse(extractor.empty());
  65. fail("Not yet implemented"); //SIFT does not override empty() method
  66. }
  67. public void testRead() {
  68. fail("Not yet implemented");
  69. }
  70. public void testWrite() {
  71. String filename = OpenCVTestRunner.getTempFileName("xml");
  72. extractor.write(filename);
  73. // String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n<name>Feature2D.SIFT</name>\n<contrastThreshold>4.0000000000000001e-02</contrastThreshold>\n<edgeThreshold>10.</edgeThreshold>\n<nFeatures>0</nFeatures>\n<nOctaveLayers>3</nOctaveLayers>\n<sigma>1.6000000000000001e+00</sigma>\n</opencv_storage>\n";
  74. String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n</opencv_storage>\n";
  75. String actual = readFile(filename);
  76. actual = actual.replaceAll("e([+-])0(\\d\\d)", "e$1$2"); // NOTE: workaround for different platforms double representation
  77. assertEquals(truth, actual);
  78. }
  79. public void testWriteYml() {
  80. String filename = OpenCVTestRunner.getTempFileName("yml");
  81. extractor.write(filename);
  82. // String truth = "%YAML:1.0\n---\nname: \"Feature2D.SIFT\"\ncontrastThreshold: 4.0000000000000001e-02\nedgeThreshold: 10.\nnFeatures: 0\nnOctaveLayers: 3\nsigma: 1.6000000000000001e+00\n";
  83. String truth = "%YAML:1.0\n---\n";
  84. String actual = readFile(filename);
  85. actual = actual.replaceAll("e([+-])0(\\d\\d)", "e$1$2"); // NOTE: workaround for different platforms double representation
  86. assertEquals(truth, actual);
  87. }
  88. }