1 package org.opencv.test.features2d;
3 import java.util.Arrays;
5 import org.opencv.core.Core;
6 import org.opencv.core.CvType;
7 import org.opencv.core.Mat;
8 import org.opencv.core.MatOfKeyPoint;
9 import org.opencv.core.Point;
10 import org.opencv.core.Scalar;
11 import org.opencv.features2d.FeatureDetector;
12 import org.opencv.core.KeyPoint;
13 import org.opencv.test.OpenCVTestCase;
14 import org.opencv.test.OpenCVTestRunner;
15 import org.opencv.imgproc.Imgproc;
17 public class FASTFeatureDetectorTest extends OpenCVTestCase {
19 FeatureDetector detector;
22 private Mat getMaskImg() {
23 Mat mask = new Mat(100, 100, CvType.CV_8U, new Scalar(255));
24 Mat right = mask.submat(0, 100, 50, 100);
25 right.setTo(new Scalar(0));
29 private Mat getTestImg() {
30 Mat img = new Mat(100, 100, CvType.CV_8U, new Scalar(255));
31 Imgproc.line(img, new Point(30, 30), new Point(70, 70), new Scalar(0), 8);
36 protected void setUp() throws Exception {
38 detector = FeatureDetector.create(FeatureDetector.FAST);
39 truth = new KeyPoint[] { new KeyPoint(32, 27, 7, -1, 254, 0, -1), new KeyPoint(27, 32, 7, -1, 254, 0, -1), new KeyPoint(73, 68, 7, -1, 254, 0, -1),
40 new KeyPoint(68, 73, 7, -1, 254, 0, -1) };
43 public void testCreate() {
44 assertNotNull(detector);
47 public void testDetectListOfMatListOfListOfKeyPoint() {
48 fail("Not yet implemented");
51 public void testDetectListOfMatListOfListOfKeyPointListOfMat() {
52 fail("Not yet implemented");
55 public void testDetectMatListOfKeyPoint() {
56 Mat img = getTestImg();
57 MatOfKeyPoint keypoints = new MatOfKeyPoint();
59 detector.detect(img, keypoints);
61 assertListKeyPointEquals(Arrays.asList(truth), keypoints.toList(), EPS);
63 // OpenCVTestRunner.Log("points found: " + keypoints.size());
64 // for (KeyPoint kp : keypoints)
65 // OpenCVTestRunner.Log(kp.toString());
68 public void testDetectMatListOfKeyPointMat() {
69 Mat img = getTestImg();
70 Mat mask = getMaskImg();
71 MatOfKeyPoint keypoints = new MatOfKeyPoint();
73 detector.detect(img, keypoints, mask);
75 assertListKeyPointEquals(Arrays.asList(truth[0], truth[1]), keypoints.toList(), EPS);
78 public void testEmpty() {
79 assertFalse(detector.empty());
82 public void testRead() {
83 String filename = OpenCVTestRunner.getTempFileName("yml");
85 writeFile(filename, "%YAML:1.0\nthreshold: 130\nnonmaxSuppression: 1\n");
86 detector.read(filename);
88 MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
90 detector.detect(grayChess, keypoints1);
92 writeFile(filename, "%YAML:1.0\nthreshold: 150\nnonmaxSuppression: 1\n");
93 detector.read(filename);
95 MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
97 detector.detect(grayChess, keypoints2);
99 assertTrue(keypoints2.total() <= keypoints1.total());
102 public void testReadYml() {
103 String filename = OpenCVTestRunner.getTempFileName("yml");
106 "<?xml version=\"1.0\"?>\n<opencv_storage>\n<threshold>130</threshold>\n<nonmaxSuppression>1</nonmaxSuppression>\n</opencv_storage>\n");
107 detector.read(filename);
109 MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
111 detector.detect(grayChess, keypoints1);
114 "<?xml version=\"1.0\"?>\n<opencv_storage>\n<threshold>150</threshold>\n<nonmaxSuppression>1</nonmaxSuppression>\n</opencv_storage>\n");
115 detector.read(filename);
117 MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
119 detector.detect(grayChess, keypoints2);
121 assertTrue(keypoints2.total() <= keypoints1.total());
124 public void testWrite() {
125 String filename = OpenCVTestRunner.getTempFileName("xml");
127 detector.write(filename);
129 String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n<name>Feature2D.FAST</name>\n<nonmaxSuppression>1</nonmaxSuppression>\n<threshold>10</threshold>\n<type>2</type>\n</opencv_storage>\n";
130 String data = readFile(filename);
131 //Log.d("qqq", "\"" + data + "\"");
132 assertEquals(truth, data);
135 public void testWriteYml() {
136 String filename = OpenCVTestRunner.getTempFileName("yml");
138 detector.write(filename);
140 String truth = "%YAML:1.0\nname: \"Feature2D.FAST\"\nnonmaxSuppression: 1\nthreshold: 10\ntype: 2\n";
141 String data = readFile(filename);
143 //Log.d("qqq", "\"" + data + "\"");
144 assertEquals(truth, data);