Path: blob/master/modules/features2d/misc/java/test/Features2dTest.java
16354 views
package org.opencv.test.features2d;12import java.util.ArrayList;3import java.util.Arrays;4import java.util.List;56import org.opencv.calib3d.Calib3d;7import org.opencv.core.CvType;8import org.opencv.core.Mat;9import org.opencv.core.MatOfDMatch;10import org.opencv.core.MatOfKeyPoint;11import org.opencv.core.MatOfPoint2f;12import org.opencv.core.Point;13import org.opencv.core.Range;14import org.opencv.core.DMatch;15import org.opencv.features2d.DescriptorMatcher;16import org.opencv.features2d.Features2d;17import org.opencv.core.KeyPoint;18import org.opencv.imgcodecs.Imgcodecs;19import org.opencv.test.OpenCVTestCase;20import org.opencv.test.OpenCVTestRunner;21import org.opencv.features2d.Feature2D;2223public class Features2dTest extends OpenCVTestCase {2425public void testDrawKeypointsMatListOfKeyPointMat() {26fail("Not yet implemented");27}2829public void testDrawKeypointsMatListOfKeyPointMatScalar() {30fail("Not yet implemented");31}3233public void testDrawKeypointsMatListOfKeyPointMatScalarInt() {34fail("Not yet implemented");35}3637public void testDrawMatches2MatListOfKeyPointMatListOfKeyPointListOfListOfDMatchMat() {38fail("Not yet implemented");39}4041public void testDrawMatches2MatListOfKeyPointMatListOfKeyPointListOfListOfDMatchMatScalar() {42fail("Not yet implemented");43}4445public void testDrawMatches2MatListOfKeyPointMatListOfKeyPointListOfListOfDMatchMatScalarScalar() {46fail("Not yet implemented");47}4849public void testDrawMatches2MatListOfKeyPointMatListOfKeyPointListOfListOfDMatchMatScalarScalarListOfListOfByte() {50fail("Not yet implemented");51}5253public void testDrawMatches2MatListOfKeyPointMatListOfKeyPointListOfListOfDMatchMatScalarScalarListOfListOfByteInt() {54fail("Not yet implemented");55}5657public void testDrawMatchesMatListOfKeyPointMatListOfKeyPointListOfDMatchMat() {58fail("Not yet implemented");59}6061public void testDrawMatchesMatListOfKeyPointMatListOfKeyPointListOfDMatchMatScalar() {62fail("Not yet implemented");63}6465public void testDrawMatchesMatListOfKeyPointMatListOfKeyPointListOfDMatchMatScalarScalar() {66fail("Not yet implemented");67}6869public void testDrawMatchesMatListOfKeyPointMatListOfKeyPointListOfDMatchMatScalarScalarListOfByte() {70fail("Not yet implemented");71}7273public void testDrawMatchesMatListOfKeyPointMatListOfKeyPointListOfDMatchMatScalarScalarListOfByteInt() {74fail("Not yet implemented");75}7677public void testPTOD()78{79String detectorCfg = "%YAML:1.0\n---\nhessianThreshold: 4000.\noctaves: 3\noctaveLayers: 4\nupright: 0\n";80String extractorCfg = "%YAML:1.0\n---\nnOctaves: 4\nnOctaveLayers: 2\nextended: 0\nupright: 0\n";8182Feature2D detector = createClassInstance(XFEATURES2D+"SURF", DEFAULT_FACTORY, null, null);83Feature2D extractor = createClassInstance(XFEATURES2D+"SURF", DEFAULT_FACTORY, null, null);84DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE);8586String detectorCfgFile = OpenCVTestRunner.getTempFileName("yml");87writeFile(detectorCfgFile, detectorCfg);88detector.read(detectorCfgFile);8990String extractorCfgFile = OpenCVTestRunner.getTempFileName("yml");91writeFile(extractorCfgFile, extractorCfg);92extractor.read(extractorCfgFile);9394Mat imgTrain = Imgcodecs.imread(OpenCVTestRunner.LENA_PATH, Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);95Mat imgQuery = imgTrain.submat(new Range(0, imgTrain.rows() - 100), Range.all());9697MatOfKeyPoint trainKeypoints = new MatOfKeyPoint();98MatOfKeyPoint queryKeypoints = new MatOfKeyPoint();99100detector.detect(imgTrain, trainKeypoints);101detector.detect(imgQuery, queryKeypoints);102103// OpenCVTestRunner.Log("Keypoints found: " + trainKeypoints.size() +104// ":" + queryKeypoints.size());105106Mat trainDescriptors = new Mat();107Mat queryDescriptors = new Mat();108109extractor.compute(imgTrain, trainKeypoints, trainDescriptors);110extractor.compute(imgQuery, queryKeypoints, queryDescriptors);111112MatOfDMatch matches = new MatOfDMatch();113114matcher.add(Arrays.asList(trainDescriptors));115matcher.match(queryDescriptors, matches);116117// OpenCVTestRunner.Log("Matches found: " + matches.size());118119DMatch adm[] = matches.toArray();120List<Point> lp1 = new ArrayList<Point>(adm.length);121List<Point> lp2 = new ArrayList<Point>(adm.length);122KeyPoint tkp[] = trainKeypoints.toArray();123KeyPoint qkp[] = queryKeypoints.toArray();124for (int i = 0; i < adm.length; i++) {125DMatch dm = adm[i];126lp1.add(tkp[dm.trainIdx].pt);127lp2.add(qkp[dm.queryIdx].pt);128}129130MatOfPoint2f points1 = new MatOfPoint2f(lp1.toArray(new Point[0]));131MatOfPoint2f points2 = new MatOfPoint2f(lp2.toArray(new Point[0]));132133Mat hmg = Calib3d.findHomography(points1, points2, Calib3d.RANSAC, 3);134135assertMatEqual(Mat.eye(3, 3, CvType.CV_64F), hmg, EPS);136137Mat outimg = new Mat();138Features2d.drawMatches(imgQuery, queryKeypoints, imgTrain, trainKeypoints, matches, outimg);139String outputPath = OpenCVTestRunner.getOutputFileName("PTODresult.png");140Imgcodecs.imwrite(outputPath, outimg);141// OpenCVTestRunner.Log("Output image is saved to: " + outputPath);142}143}144145146