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