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 STARFeatureDetectorTest extends OpenCVTestCase {
19 FeatureDetector detector;
23 private Mat getMaskImg() {
24 Mat mask = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
25 Mat right = mask.submat(0, matSize, matSize / 2, matSize);
26 right.setTo(new Scalar(0));
30 private Mat getTestImg() {
31 Scalar color = new Scalar(0);
32 int center = matSize / 2;
36 Mat img = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
37 Imgproc.circle(img, new Point(center - offset, center), radius, color, -1);
38 Imgproc.circle(img, new Point(center + offset, center), radius, color, -1);
39 Imgproc.circle(img, new Point(center, center - offset), radius, color, -1);
40 Imgproc.circle(img, new Point(center, center + offset), radius, color, -1);
41 Imgproc.circle(img, new Point(center, center), radius, color, -1);
45 protected void setUp() throws Exception {
47 detector = FeatureDetector.create(FeatureDetector.STAR);
49 truth = new KeyPoint[] {
50 new KeyPoint( 95, 80, 22, -1, 31.5957f, 0, -1),
51 new KeyPoint(105, 80, 22, -1, 31.5957f, 0, -1),
52 new KeyPoint( 80, 95, 22, -1, 31.5957f, 0, -1),
53 new KeyPoint(120, 95, 22, -1, 31.5957f, 0, -1),
54 new KeyPoint(100, 100, 8, -1, 30.f, 0, -1),
55 new KeyPoint( 80, 105, 22, -1, 31.5957f, 0, -1),
56 new KeyPoint(120, 105, 22, -1, 31.5957f, 0, -1),
57 new KeyPoint( 95, 120, 22, -1, 31.5957f, 0, -1),
58 new KeyPoint(105, 120, 22, -1, 31.5957f, 0, -1)
62 public void testCreate() {
63 assertNotNull(detector);
66 public void testDetectListOfMatListOfListOfKeyPoint() {
67 fail("Not yet implemented");
70 public void testDetectListOfMatListOfListOfKeyPointListOfMat() {
71 fail("Not yet implemented");
74 public void testDetectMatListOfKeyPoint() {
75 Mat img = getTestImg();
76 MatOfKeyPoint keypoints = new MatOfKeyPoint();
78 detector.detect(img, keypoints);
80 assertListKeyPointEquals(Arrays.asList(truth), keypoints.toList(), EPS);
83 public void testDetectMatListOfKeyPointMat() {
84 Mat img = getTestImg();
85 Mat mask = getMaskImg();
86 MatOfKeyPoint keypoints = new MatOfKeyPoint();
88 detector.detect(img, keypoints, mask);
90 assertListKeyPointEquals(Arrays.asList(truth[0], truth[2], truth[5], truth[7]), keypoints.toList(), EPS);
93 public void testEmpty() {
94 assertFalse(detector.empty());
97 public void testRead() {
98 Mat img = getTestImg();
100 MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
101 detector.detect(img, keypoints1);
103 String filename = OpenCVTestRunner.getTempFileName("yml");
104 writeFile(filename, "%YAML:1.0\nmaxSize: 45\nresponseThreshold: 150\nlineThresholdProjected: 10\nlineThresholdBinarized: 8\nsuppressNonmaxSize: 5\n");
105 detector.read(filename);
107 MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
108 detector.detect(img, keypoints2);
110 assertTrue(keypoints2.total() <= keypoints1.total());
113 public void testWrite() {
114 String filename = OpenCVTestRunner.getTempFileName("xml");
116 detector.write(filename);
118 String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n<name>Feature2D.STAR</name>\n<lineThresholdBinarized>8</lineThresholdBinarized>\n<lineThresholdProjected>10</lineThresholdProjected>\n<maxSize>45</maxSize>\n<responseThreshold>30</responseThreshold>\n<suppressNonmaxSize>5</suppressNonmaxSize>\n</opencv_storage>\n";
119 assertEquals(truth, readFile(filename));
122 public void testWriteYml() {
123 String filename = OpenCVTestRunner.getTempFileName("yml");
125 detector.write(filename);
127 String truth = "%YAML:1.0\nname: \"Feature2D.STAR\"\nlineThresholdBinarized: 8\nlineThresholdProjected: 10\nmaxSize: 45\nresponseThreshold: 30\nsuppressNonmaxSize: 5\n";
128 assertEquals(truth, readFile(filename));