327084211bd24f0fa82a94bc6ad56835eb487647
[platform/upstream/opencv.git] / modules / features2d / misc / java / test / STARFeatureDetectorTest.java
1 package org.opencv.test.features2d;
2
3 import java.util.Arrays;
4
5 import org.opencv.core.CvType;
6 import org.opencv.core.Mat;
7 import org.opencv.core.MatOfKeyPoint;
8 import org.opencv.core.Point;
9 import org.opencv.core.Scalar;
10 import org.opencv.core.KeyPoint;
11 import org.opencv.test.OpenCVTestCase;
12 import org.opencv.test.OpenCVTestRunner;
13 import org.opencv.imgproc.Imgproc;
14 import org.opencv.features2d.Feature2D;
15
16 public class STARFeatureDetectorTest extends OpenCVTestCase {
17
18     Feature2D detector;
19     int matSize;
20     KeyPoint[] truth;
21
22     private Mat getMaskImg() {
23         Mat mask = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
24         Mat right = mask.submat(0, matSize, matSize / 2, matSize);
25         right.setTo(new Scalar(0));
26         return mask;
27     }
28
29     private Mat getTestImg() {
30         Scalar color = new Scalar(0);
31         int center = matSize / 2;
32         int radius = 6;
33         int offset = 40;
34
35         Mat img = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
36         Imgproc.circle(img, new Point(center - offset, center), radius, color, -1);
37         Imgproc.circle(img, new Point(center + offset, center), radius, color, -1);
38         Imgproc.circle(img, new Point(center, center - offset), radius, color, -1);
39         Imgproc.circle(img, new Point(center, center + offset), radius, color, -1);
40         Imgproc.circle(img, new Point(center, center), radius, color, -1);
41         return img;
42     }
43
44     protected void setUp() throws Exception {
45         super.setUp();
46         detector = createClassInstance(XFEATURES2D+"StarDetector", DEFAULT_FACTORY, null, null);
47         matSize = 200;
48         truth = new KeyPoint[] {
49                 new KeyPoint( 95,  80, 22, -1, 31.5957f, 0, -1),
50                 new KeyPoint(105,  80, 22, -1, 31.5957f, 0, -1),
51                 new KeyPoint( 80,  95, 22, -1, 31.5957f, 0, -1),
52                 new KeyPoint(120,  95, 22, -1, 31.5957f, 0, -1),
53                 new KeyPoint(100, 100,  8, -1, 30.f,     0, -1),
54                 new KeyPoint( 80, 105, 22, -1, 31.5957f, 0, -1),
55                 new KeyPoint(120, 105, 22, -1, 31.5957f, 0, -1),
56                 new KeyPoint( 95, 120, 22, -1, 31.5957f, 0, -1),
57                 new KeyPoint(105, 120, 22, -1, 31.5957f, 0, -1)
58             };
59     }
60
61     public void testCreate() {
62         assertNotNull(detector);
63     }
64
65     public void testDetectListOfMatListOfListOfKeyPoint() {
66         fail("Not yet implemented");
67     }
68
69     public void testDetectListOfMatListOfListOfKeyPointListOfMat() {
70         fail("Not yet implemented");
71     }
72
73     public void testDetectMatListOfKeyPoint() {
74         Mat img = getTestImg();
75         MatOfKeyPoint keypoints = new MatOfKeyPoint();
76
77         detector.detect(img, keypoints);
78
79         assertListKeyPointEquals(Arrays.asList(truth), keypoints.toList(), EPS);
80     }
81
82     public void testDetectMatListOfKeyPointMat() {
83         Mat img = getTestImg();
84         Mat mask = getMaskImg();
85         MatOfKeyPoint keypoints = new MatOfKeyPoint();
86
87         detector.detect(img, keypoints, mask);
88
89         assertListKeyPointEquals(Arrays.asList(truth[0], truth[2], truth[5], truth[7]), keypoints.toList(), EPS);
90     }
91
92     public void testEmpty() {
93 //        assertFalse(detector.empty());
94         fail("Not yet implemented");
95     }
96
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\n---\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
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         String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n</opencv_storage>\n";
120         assertEquals(truth, readFile(filename));
121     }
122
123     public void testWriteYml() {
124         String filename = OpenCVTestRunner.getTempFileName("yml");
125
126         detector.write(filename);
127
128 //        String truth = "%YAML:1.0\n---\nname: \"Feature2D.STAR\"\nlineThresholdBinarized: 8\nlineThresholdProjected: 10\nmaxSize: 45\nresponseThreshold: 30\nsuppressNonmaxSize: 5\n";
129         String truth = "%YAML:1.0\n---\n";
130         assertEquals(truth, readFile(filename));
131     }
132
133 }