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