1 package org.opencv.test.features2d;
2 
3 import java.util.Arrays;
4 
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;
16 
17 public class STARFeatureDetectorTest extends OpenCVTestCase {
18 
19     FeatureDetector detector;
20     int matSize;
21     KeyPoint[] truth;
22 
getMaskImg()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));
27         return mask;
28     }
29 
getTestImg()30     private Mat getTestImg() {
31         Scalar color = new Scalar(0);
32         int center = matSize / 2;
33         int radius = 6;
34         int offset = 40;
35 
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);
42         return img;
43     }
44 
setUp()45     protected void setUp() throws Exception {
46         super.setUp();
47         detector = FeatureDetector.create(FeatureDetector.STAR);
48         matSize = 200;
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)
59             };
60     }
61 
testCreate()62     public void testCreate() {
63         assertNotNull(detector);
64     }
65 
testDetectListOfMatListOfListOfKeyPoint()66     public void testDetectListOfMatListOfListOfKeyPoint() {
67         fail("Not yet implemented");
68     }
69 
testDetectListOfMatListOfListOfKeyPointListOfMat()70     public void testDetectListOfMatListOfListOfKeyPointListOfMat() {
71         fail("Not yet implemented");
72     }
73 
testDetectMatListOfKeyPoint()74     public void testDetectMatListOfKeyPoint() {
75         Mat img = getTestImg();
76         MatOfKeyPoint keypoints = new MatOfKeyPoint();
77 
78         detector.detect(img, keypoints);
79 
80         assertListKeyPointEquals(Arrays.asList(truth), keypoints.toList(), EPS);
81     }
82 
testDetectMatListOfKeyPointMat()83     public void testDetectMatListOfKeyPointMat() {
84         Mat img = getTestImg();
85         Mat mask = getMaskImg();
86         MatOfKeyPoint keypoints = new MatOfKeyPoint();
87 
88         detector.detect(img, keypoints, mask);
89 
90         assertListKeyPointEquals(Arrays.asList(truth[0], truth[2], truth[5], truth[7]), keypoints.toList(), EPS);
91     }
92 
testEmpty()93     public void testEmpty() {
94         assertFalse(detector.empty());
95     }
96 
testRead()97     public void testRead() {
98         Mat img = getTestImg();
99 
100         MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
101         detector.detect(img, keypoints1);
102 
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);
106 
107         MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
108         detector.detect(img, keypoints2);
109 
110         assertTrue(keypoints2.total() <= keypoints1.total());
111     }
112 
testWrite()113     public void testWrite() {
114         String filename = OpenCVTestRunner.getTempFileName("xml");
115 
116         detector.write(filename);
117 
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));
120     }
121 
testWriteYml()122     public void testWriteYml() {
123         String filename = OpenCVTestRunner.getTempFileName("yml");
124 
125         detector.write(filename);
126 
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));
129     }
130 
131 }
132