1
package org.opencv.test.features2d;
3
import java.util.ArrayList;
4
import java.util.Arrays;
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;
25
public class Features2dTest extends OpenCVTestCase {
27
public void testDrawKeypointsMatListOfKeyPointMat() {
28
fail("Not yet implemented");
31
public void testDrawKeypointsMatListOfKeyPointMatScalar() {
32
fail("Not yet implemented");
35
public void testDrawKeypointsMatListOfKeyPointMatScalarInt() {
36
fail("Not yet implemented");
39
public void testDrawMatches2MatListOfKeyPointMatListOfKeyPointListOfListOfDMatchMat() {
40
fail("Not yet implemented");
43
public void testDrawMatches2MatListOfKeyPointMatListOfKeyPointListOfListOfDMatchMatScalar() {
44
fail("Not yet implemented");
47
public void testDrawMatches2MatListOfKeyPointMatListOfKeyPointListOfListOfDMatchMatScalarScalar() {
48
fail("Not yet implemented");
51
public void testDrawMatches2MatListOfKeyPointMatListOfKeyPointListOfListOfDMatchMatScalarScalarListOfListOfByte() {
52
fail("Not yet implemented");
55
public void testDrawMatches2MatListOfKeyPointMatListOfKeyPointListOfListOfDMatchMatScalarScalarListOfListOfByteInt() {
56
fail("Not yet implemented");
59
public void testDrawMatchesMatListOfKeyPointMatListOfKeyPointListOfDMatchMat() {
60
fail("Not yet implemented");
63
public void testDrawMatchesMatListOfKeyPointMatListOfKeyPointListOfDMatchMatScalar() {
64
fail("Not yet implemented");
67
public void testDrawMatchesMatListOfKeyPointMatListOfKeyPointListOfDMatchMatScalarScalar() {
68
fail("Not yet implemented");
71
public void testDrawMatchesMatListOfKeyPointMatListOfKeyPointListOfDMatchMatScalarScalarListOfByte() {
72
fail("Not yet implemented");
75
public void testDrawMatchesMatListOfKeyPointMatListOfKeyPointListOfDMatchMatScalarScalarListOfByteInt() {
76
fail("Not yet implemented");
79
public void testPTOD()
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";
84
FeatureDetector detector = FeatureDetector.create(FeatureDetector.SURF);
85
DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
86
DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE);
88
String detectorCfgFile = OpenCVTestRunner.getTempFileName("yml");
89
writeFile(detectorCfgFile, detectorCfg);
90
detector.read(detectorCfgFile);
92
String extractorCfgFile = OpenCVTestRunner.getTempFileName("yml");
93
writeFile(extractorCfgFile, extractorCfg);
94
extractor.read(extractorCfgFile);
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());
99
MatOfKeyPoint trainKeypoints = new MatOfKeyPoint();
100
MatOfKeyPoint queryKeypoints = new MatOfKeyPoint();
102
detector.detect(imgTrain, trainKeypoints);
103
detector.detect(imgQuery, queryKeypoints);
105
// OpenCVTestRunner.Log("Keypoints found: " + trainKeypoints.size() +
106
// ":" + queryKeypoints.size());
108
Mat trainDescriptors = new Mat();
109
Mat queryDescriptors = new Mat();
111
extractor.compute(imgTrain, trainKeypoints, trainDescriptors);
112
extractor.compute(imgQuery, queryKeypoints, queryDescriptors);
114
MatOfDMatch matches = new MatOfDMatch();
116
matcher.add(Arrays.asList(trainDescriptors));
117
matcher.match(queryDescriptors, matches);
119
// OpenCVTestRunner.Log("Matches found: " + matches.size());
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++) {
128
lp1.add(tkp[dm.trainIdx].pt);
129
lp2.add(qkp[dm.queryIdx].pt);
132
MatOfPoint2f points1 = new MatOfPoint2f(lp1.toArray(new Point[0]));
133
MatOfPoint2f points2 = new MatOfPoint2f(lp2.toArray(new Point[0]));
135
Mat hmg = Calib3d.findHomography(points1, points2, Calib3d.RANSAC, 3);
137
assertMatEqual(Mat.eye(3, 3, CvType.CV_64F), hmg, EPS);
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);