Features2dTest.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package org.opencv.test.features2d;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import org.opencv.calib3d.Calib3d;
  6. import org.opencv.core.CvType;
  7. import org.opencv.core.Mat;
  8. import org.opencv.core.MatOfInt;
  9. import org.opencv.core.MatOfDMatch;
  10. import org.opencv.core.MatOfKeyPoint;
  11. import org.opencv.core.MatOfPoint2f;
  12. import org.opencv.core.Point;
  13. import org.opencv.core.Range;
  14. import org.opencv.core.Scalar;
  15. import org.opencv.core.DMatch;
  16. import org.opencv.features2d.DescriptorMatcher;
  17. import org.opencv.features2d.Features2d;
  18. import org.opencv.core.KeyPoint;
  19. import org.opencv.imgcodecs.Imgcodecs;
  20. import org.opencv.test.OpenCVTestCase;
  21. import org.opencv.test.OpenCVTestRunner;
  22. import org.opencv.features2d.Feature2D;
  23. public class Features2dTest extends OpenCVTestCase {
  24. public void testDrawKeypointsMatListOfKeyPointMat() {
  25. fail("Not yet implemented");
  26. }
  27. public void testDrawKeypointsMatListOfKeyPointMatScalar() {
  28. fail("Not yet implemented");
  29. }
  30. public void testDrawKeypointsMatListOfKeyPointMatScalarInt() {
  31. fail("Not yet implemented");
  32. }
  33. public void testDrawMatches2MatListOfKeyPointMatListOfKeyPointListOfListOfDMatchMat() {
  34. fail("Not yet implemented");
  35. }
  36. public void testDrawMatches2MatListOfKeyPointMatListOfKeyPointListOfListOfDMatchMatScalar() {
  37. fail("Not yet implemented");
  38. }
  39. public void testDrawMatches2MatListOfKeyPointMatListOfKeyPointListOfListOfDMatchMatScalarScalar() {
  40. fail("Not yet implemented");
  41. }
  42. public void testDrawMatches2MatListOfKeyPointMatListOfKeyPointListOfListOfDMatchMatScalarScalarListOfListOfByte() {
  43. fail("Not yet implemented");
  44. }
  45. public void testDrawMatches2MatListOfKeyPointMatListOfKeyPointListOfListOfDMatchMatScalarScalarListOfListOfByteInt() {
  46. fail("Not yet implemented");
  47. }
  48. public void testDrawMatchesMatListOfKeyPointMatListOfKeyPointListOfDMatchMat() {
  49. fail("Not yet implemented");
  50. }
  51. public void testDrawMatchesMatListOfKeyPointMatListOfKeyPointListOfDMatchMatScalar() {
  52. fail("Not yet implemented");
  53. }
  54. public void testDrawMatchesMatListOfKeyPointMatListOfKeyPointListOfDMatchMatScalarScalar() {
  55. fail("Not yet implemented");
  56. }
  57. public void testDrawMatchesMatListOfKeyPointMatListOfKeyPointListOfDMatchMatScalarScalarListOfByte() {
  58. fail("Not yet implemented");
  59. }
  60. public void testDrawMatchesMatListOfKeyPointMatListOfKeyPointListOfDMatchMatScalarScalarListOfByteInt() {
  61. fail("Not yet implemented");
  62. }
  63. public void testPTOD()
  64. {
  65. String detectorCfg = "%YAML:1.0\n---\nhessianThreshold: 4000.\noctaves: 3\noctaveLayers: 4\nupright: 0\n";
  66. String extractorCfg = "%YAML:1.0\n---\nnOctaves: 4\nnOctaveLayers: 2\nextended: 0\nupright: 0\n";
  67. Feature2D detector = createClassInstance(XFEATURES2D+"SURF", DEFAULT_FACTORY, null, null);
  68. Feature2D extractor = createClassInstance(XFEATURES2D+"SURF", DEFAULT_FACTORY, null, null);
  69. DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE);
  70. String detectorCfgFile = OpenCVTestRunner.getTempFileName("yml");
  71. writeFile(detectorCfgFile, detectorCfg);
  72. detector.read(detectorCfgFile);
  73. String extractorCfgFile = OpenCVTestRunner.getTempFileName("yml");
  74. writeFile(extractorCfgFile, extractorCfg);
  75. extractor.read(extractorCfgFile);
  76. Mat imgTrain = Imgcodecs.imread(OpenCVTestRunner.LENA_PATH, Imgcodecs.IMREAD_GRAYSCALE);
  77. Mat imgQuery = imgTrain.submat(new Range(0, imgTrain.rows() - 100), Range.all());
  78. MatOfKeyPoint trainKeypoints = new MatOfKeyPoint();
  79. MatOfKeyPoint queryKeypoints = new MatOfKeyPoint();
  80. detector.detect(imgTrain, trainKeypoints);
  81. detector.detect(imgQuery, queryKeypoints);
  82. // OpenCVTestRunner.Log("Keypoints found: " + trainKeypoints.size() +
  83. // ":" + queryKeypoints.size());
  84. Mat trainDescriptors = new Mat();
  85. Mat queryDescriptors = new Mat();
  86. extractor.compute(imgTrain, trainKeypoints, trainDescriptors);
  87. extractor.compute(imgQuery, queryKeypoints, queryDescriptors);
  88. MatOfDMatch matches = new MatOfDMatch();
  89. matcher.add(Arrays.asList(trainDescriptors));
  90. matcher.match(queryDescriptors, matches);
  91. // OpenCVTestRunner.Log("Matches found: " + matches.size());
  92. DMatch adm[] = matches.toArray();
  93. List<Point> lp1 = new ArrayList<Point>(adm.length);
  94. List<Point> lp2 = new ArrayList<Point>(adm.length);
  95. KeyPoint tkp[] = trainKeypoints.toArray();
  96. KeyPoint qkp[] = queryKeypoints.toArray();
  97. for (int i = 0; i < adm.length; i++) {
  98. DMatch dm = adm[i];
  99. lp1.add(tkp[dm.trainIdx].pt);
  100. lp2.add(qkp[dm.queryIdx].pt);
  101. }
  102. MatOfPoint2f points1 = new MatOfPoint2f(lp1.toArray(new Point[0]));
  103. MatOfPoint2f points2 = new MatOfPoint2f(lp2.toArray(new Point[0]));
  104. Mat hmg = Calib3d.findHomography(points1, points2, Calib3d.RANSAC, 3);
  105. assertMatEqual(Mat.eye(3, 3, CvType.CV_64F), hmg, EPS);
  106. Mat outimg = new Mat();
  107. Features2d.drawMatches(imgQuery, queryKeypoints, imgTrain, trainKeypoints, matches, outimg);
  108. String outputPath = OpenCVTestRunner.getOutputFileName("PTODresult.png");
  109. Imgcodecs.imwrite(outputPath, outimg);
  110. // OpenCVTestRunner.Log("Output image is saved to: " + outputPath);
  111. }
  112. public void testDrawKeypoints()
  113. {
  114. Mat outImg = Mat.ones(11, 11, CvType.CV_8U);
  115. MatOfKeyPoint kps = new MatOfKeyPoint(new KeyPoint(5, 5, 1)); // x, y, size
  116. Features2d.drawKeypoints(new Mat(), kps, outImg, new Scalar(255),
  117. Features2d.DrawMatchesFlags_DRAW_OVER_OUTIMG);
  118. Mat ref = new MatOfInt(new int[] {
  119. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  120. 1, 1, 1, 1, 15, 54, 15, 1, 1, 1, 1,
  121. 1, 1, 1, 76, 217, 217, 221, 81, 1, 1, 1,
  122. 1, 1, 100, 224, 111, 57, 115, 225, 101, 1, 1,
  123. 1, 44, 215, 100, 1, 1, 1, 101, 214, 44, 1,
  124. 1, 54, 212, 57, 1, 1, 1, 55, 212, 55, 1,
  125. 1, 40, 215, 104, 1, 1, 1, 105, 215, 40, 1,
  126. 1, 1, 102, 221, 111, 55, 115, 222, 103, 1, 1,
  127. 1, 1, 1, 76, 218, 217, 220, 81, 1, 1, 1,
  128. 1, 1, 1, 1, 15, 55, 15, 1, 1, 1, 1,
  129. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
  130. }).reshape(1, 11);
  131. ref.convertTo(ref, CvType.CV_8U);
  132. assertMatEqual(ref, outImg);
  133. }
  134. }