1
package org.opencv.test.features2d;
3
import java.util.ArrayList;
4
import java.util.Arrays;
5
import java.util.Collections;
6
import java.util.Comparator;
9
import org.opencv.core.Core;
10
import org.opencv.core.CvType;
11
import org.opencv.core.Mat;
12
import org.opencv.core.MatOfKeyPoint;
13
import org.opencv.core.Point;
14
import org.opencv.core.Scalar;
15
import org.opencv.features2d.FeatureDetector;
16
import org.opencv.core.KeyPoint;
17
import org.opencv.test.OpenCVTestCase;
18
import org.opencv.test.OpenCVTestRunner;
19
import org.opencv.imgproc.Imgproc;
21
public class SURFFeatureDetectorTest extends OpenCVTestCase {
23
FeatureDetector detector;
27
private Mat getMaskImg() {
28
Mat mask = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
29
Mat right = mask.submat(0, matSize, matSize / 2, matSize);
30
right.setTo(new Scalar(0));
34
private Mat getTestImg() {
35
Mat cross = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
36
Imgproc.line(cross, new Point(20, matSize / 2), new Point(matSize - 21, matSize / 2), new Scalar(100), 2);
37
Imgproc.line(cross, new Point(matSize / 2, 20), new Point(matSize / 2, matSize - 21), new Scalar(100), 2);
42
private void order(List<KeyPoint> points) {
43
Collections.sort(points, new Comparator<KeyPoint>() {
44
public int compare(KeyPoint p1, KeyPoint p2) {
45
if (p1.angle < p2.angle)
47
if (p1.angle > p2.angle)
55
protected void setUp() throws Exception {
57
detector = FeatureDetector.create(FeatureDetector.SURF);
59
truth = new KeyPoint[] {
60
new KeyPoint(55.775578f, 55.775578f, 16, 80.245735f, 8617.8633f, 0, -1),
61
new KeyPoint(44.224422f, 55.775578f, 16, 170.24574f, 8617.8633f, 0, -1),
62
new KeyPoint(44.224422f, 44.224422f, 16, 260.24573f, 8617.8633f, 0, -1),
63
new KeyPoint(55.775578f, 44.224422f, 16, 350.24573f, 8617.8633f, 0, -1)
67
public void testCreate() {
68
assertNotNull(detector);
71
public void testDetectListOfMatListOfListOfKeyPoint() {
72
String filename = OpenCVTestRunner.getTempFileName("yml");
73
writeFile(filename, "%YAML:1.0\nhessianThreshold: 8000.\noctaves: 3\noctaveLayers: 4\nupright: 0\n");
74
detector.read(filename);
76
List<MatOfKeyPoint> keypoints = new ArrayList<MatOfKeyPoint>();
77
Mat cross = getTestImg();
78
List<Mat> crosses = new ArrayList<Mat>(3);
83
detector.detect(crosses, keypoints);
85
assertEquals(3, keypoints.size());
87
for (MatOfKeyPoint mkp : keypoints) {
88
List<KeyPoint> lkp = mkp.toList();
90
assertListKeyPointEquals(Arrays.asList(truth), lkp, EPS);
94
public void testDetectListOfMatListOfListOfKeyPointListOfMat() {
95
fail("Not yet implemented");
98
public void testDetectMatListOfKeyPoint() {
99
String filename = OpenCVTestRunner.getTempFileName("yml");
100
writeFile(filename, "%YAML:1.0\nhessianThreshold: 8000.\noctaves: 3\noctaveLayers: 4\nupright: 0\n");
101
detector.read(filename);
103
MatOfKeyPoint keypoints = new MatOfKeyPoint();
104
Mat cross = getTestImg();
106
detector.detect(cross, keypoints);
108
List<KeyPoint> lkp = keypoints.toList();
110
assertListKeyPointEquals(Arrays.asList(truth), lkp, EPS);
113
public void testDetectMatListOfKeyPointMat() {
114
String filename = OpenCVTestRunner.getTempFileName("yml");
115
writeFile(filename, "%YAML:1.0\nhessianThreshold: 8000.\noctaves: 3\noctaveLayers: 4\nupright: 0\n");
116
detector.read(filename);
118
Mat img = getTestImg();
119
Mat mask = getMaskImg();
120
MatOfKeyPoint keypoints = new MatOfKeyPoint();
122
detector.detect(img, keypoints, mask);
124
List<KeyPoint> lkp = keypoints.toList();
126
assertListKeyPointEquals(Arrays.asList(truth[1], truth[2]), lkp, EPS);
129
public void testEmpty() {
130
assertFalse(detector.empty());
133
public void testRead() {
134
Mat cross = getTestImg();
136
MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
137
detector.detect(cross, keypoints1);
139
String filename = OpenCVTestRunner.getTempFileName("yml");
140
writeFile(filename, "%YAML:1.0\nhessianThreshold: 8000.\noctaves: 3\noctaveLayers: 4\nupright: 0\n");
141
detector.read(filename);
143
MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
144
detector.detect(cross, keypoints2);
146
assertTrue(keypoints2.total() <= keypoints1.total());
149
public void testWrite() {
150
String filename = OpenCVTestRunner.getTempFileName("xml");
152
detector.write(filename);
154
String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n<name>Feature2D.SURF</name>\n<extended>0</extended>\n<hessianThreshold>100.</hessianThreshold>\n<nOctaveLayers>3</nOctaveLayers>\n<nOctaves>4</nOctaves>\n<upright>0</upright>\n</opencv_storage>\n";
155
assertEquals(truth, readFile(filename));
158
public void testWriteYml() {
159
String filename = OpenCVTestRunner.getTempFileName("yml");
161
detector.write(filename);
163
String truth = "%YAML:1.0\nname: \"Feature2D.SURF\"\nextended: 0\nhessianThreshold: 100.\nnOctaveLayers: 3\nnOctaves: 4\nupright: 0\n";
164
assertEquals(truth, readFile(filename));