Merge remote-tracking branch 'origin/2.4' into merge-2.4
[profile/ivi/opencv.git] / modules / java / android_test / src / org / opencv / test / features2d / FlannBasedDescriptorMatcherTest.java
1 package org.opencv.test.features2d;
2
3 import java.util.Arrays;
4 import java.util.List;
5
6 import org.opencv.core.Core;
7 import org.opencv.core.CvException;
8 import org.opencv.core.CvType;
9 import org.opencv.core.Mat;
10 import org.opencv.core.MatOfDMatch;
11 import org.opencv.core.MatOfKeyPoint;
12 import org.opencv.core.Point;
13 import org.opencv.core.Scalar;
14 import org.opencv.core.DMatch;
15 import org.opencv.features2d.DescriptorExtractor;
16 import org.opencv.features2d.DescriptorMatcher;
17 import org.opencv.features2d.FeatureDetector;
18 import org.opencv.core.KeyPoint;
19 import org.opencv.test.OpenCVTestCase;
20 import org.opencv.test.OpenCVTestRunner;
21
22 public class FlannBasedDescriptorMatcherTest extends OpenCVTestCase {
23
24     static final String xmlParamsDefault = "<?xml version=\"1.0\"?>\n"
25             + "<opencv_storage>\n"
26             + "<indexParams>\n"
27             + "  <_>\n"
28             + "    <name>algorithm</name>\n"
29             + "    <type>23</type>\n"
30             + "    <value>1</value></_>\n"
31             + "  <_>\n"
32             + "    <name>trees</name>\n"
33             + "    <type>4</type>\n"
34             + "    <value>4</value></_></indexParams>\n"
35             + "<searchParams>\n"
36             + "  <_>\n"
37             + "    <name>checks</name>\n"
38             + "    <type>4</type>\n"
39             + "    <value>32</value></_>\n"
40             + "  <_>\n"
41             + "    <name>eps</name>\n"
42             + "    <type>5</type>\n"
43             + "    <value>0.</value></_>\n"
44             + "  <_>\n"
45             + "    <name>sorted</name>\n"
46             + "    <type>15</type>\n"
47             + "    <value>1</value></_></searchParams>\n"
48             + "</opencv_storage>\n";
49     static final String ymlParamsDefault = "%YAML:1.0\n"
50             + "indexParams:\n"
51             + "   -\n"
52             + "      name: algorithm\n"
53             + "      type: 23\n"
54             + "      value: 1\n"
55             + "   -\n"
56             + "      name: trees\n"
57             + "      type: 4\n"
58             + "      value: 4\n"
59             + "searchParams:\n"
60             + "   -\n"
61             + "      name: checks\n"
62             + "      type: 4\n"
63             + "      value: 32\n"
64             + "   -\n"
65             + "      name: eps\n"
66             + "      type: 5\n"
67             + "      value: 0.\n"
68             + "   -\n"
69             + "      name: sorted\n"
70             + "      type: 15\n"
71             + "      value: 1\n";
72     static final String ymlParamsModified = "%YAML:1.0\n"
73             + "indexParams:\n"
74             + "   -\n"
75             + "      name: algorithm\n"
76             + "      type: 23\n"
77             + "      value: 6\n"// this line is changed!
78             + "   -\n"
79             + "      name: trees\n"
80             + "      type: 4\n"
81             + "      value: 4\n"
82             + "searchParams:\n"
83             + "   -\n"
84             + "      name: checks\n"
85             + "      type: 4\n"
86             + "      value: 32\n"
87             + "   -\n"
88             + "      name: eps\n"
89             + "      type: 5\n"
90             + "      value: 4.\n"// this line is changed!
91             + "   -\n"
92             + "      name: sorted\n"
93             + "      type: 15\n"
94             + "      value: 1\n";
95
96     DescriptorMatcher matcher;
97
98     int matSize;
99
100     DMatch[] truth;
101
102     private Mat getMaskImg() {
103         return new Mat(5, 2, CvType.CV_8U, new Scalar(0)) {
104             {
105                 put(0, 0, 1, 1, 1, 1);
106             }
107         };
108     }
109
110     private Mat getQueryDescriptors() {
111         Mat img = getQueryImg();
112         MatOfKeyPoint keypoints = new MatOfKeyPoint();
113         Mat descriptors = new Mat();
114
115         FeatureDetector detector = FeatureDetector.create(FeatureDetector.SURF);
116         DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
117
118         String filename = OpenCVTestRunner.getTempFileName("yml");
119         writeFile(filename, "%YAML:1.0\nhessianThreshold: 8000.\noctaves: 3\noctaveLayers: 4\nupright: 0\n");
120         detector.read(filename);
121
122         detector.detect(img, keypoints);
123         extractor.compute(img, keypoints, descriptors);
124
125         return descriptors;
126     }
127
128     private Mat getQueryImg() {
129         Mat cross = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
130         Core.line(cross, new Point(30, matSize / 2), new Point(matSize - 31, matSize / 2), new Scalar(100), 3);
131         Core.line(cross, new Point(matSize / 2, 30), new Point(matSize / 2, matSize - 31), new Scalar(100), 3);
132
133         return cross;
134     }
135
136     private Mat getTrainDescriptors() {
137         Mat img = getTrainImg();
138         MatOfKeyPoint keypoints = new MatOfKeyPoint(new KeyPoint(50, 50, 16, 0, 20000, 1, -1), new KeyPoint(42, 42, 16, 160, 10000, 1, -1));
139         Mat descriptors = new Mat();
140
141         DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
142
143         extractor.compute(img, keypoints, descriptors);
144
145         return descriptors;
146     }
147
148     private Mat getTrainImg() {
149         Mat cross = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
150         Core.line(cross, new Point(20, matSize / 2), new Point(matSize - 21, matSize / 2), new Scalar(100), 2);
151         Core.line(cross, new Point(matSize / 2, 20), new Point(matSize / 2, matSize - 21), new Scalar(100), 2);
152
153         return cross;
154     }
155
156     protected void setUp() throws Exception {
157         super.setUp();
158         matcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);
159         matSize = 100;
160         truth = new DMatch[] {
161                 new DMatch(0, 0, 0, 0.6211397f),
162                 new DMatch(1, 1, 0, 0.9177120f),
163                 new DMatch(2, 1, 0, 0.3112163f),
164                 new DMatch(3, 1, 0, 0.2925075f),
165                 new DMatch(4, 1, 0, 0.9309179f)
166                 };
167     }
168
169     public void testAdd() {
170         matcher.add(Arrays.asList(new Mat()));
171         assertFalse(matcher.empty());
172     }
173
174     public void testClear() {
175         matcher.add(Arrays.asList(new Mat()));
176
177         matcher.clear();
178
179         assertTrue(matcher.empty());
180     }
181
182     public void testClone() {
183         Mat train = new Mat(1, 1, CvType.CV_8U, new Scalar(123));
184         matcher.add(Arrays.asList(train));
185
186         try {
187             matcher.clone();
188             fail("Expected CvException (CV_StsNotImplemented)");
189         } catch (CvException cverr) {
190             // expected
191         }
192     }
193
194     public void testCloneBoolean() {
195         matcher.add(Arrays.asList(new Mat()));
196
197         DescriptorMatcher cloned = matcher.clone(true);
198
199         assertNotNull(cloned);
200         assertTrue(cloned.empty());
201     }
202
203     public void testCreate() {
204         assertNotNull(matcher);
205     }
206
207     public void testEmpty() {
208         assertTrue(matcher.empty());
209     }
210
211     public void testGetTrainDescriptors() {
212         Mat train = new Mat(1, 1, CvType.CV_8U, new Scalar(123));
213         Mat truth = train.clone();
214         matcher.add(Arrays.asList(train));
215
216         List<Mat> descriptors = matcher.getTrainDescriptors();
217
218         assertEquals(1, descriptors.size());
219         assertMatEqual(truth, descriptors.get(0));
220     }
221
222     public void testIsMaskSupported() {
223         assertFalse(matcher.isMaskSupported());
224     }
225
226     public void testKnnMatchMatListOfListOfDMatchInt() {
227         fail("Not yet implemented");
228     }
229
230     public void testKnnMatchMatListOfListOfDMatchIntListOfMat() {
231         fail("Not yet implemented");
232     }
233
234     public void testKnnMatchMatListOfListOfDMatchIntListOfMatBoolean() {
235         fail("Not yet implemented");
236     }
237
238     public void testKnnMatchMatMatListOfListOfDMatchInt() {
239         fail("Not yet implemented");
240     }
241
242     public void testKnnMatchMatMatListOfListOfDMatchIntMat() {
243         fail("Not yet implemented");
244     }
245
246     public void testKnnMatchMatMatListOfListOfDMatchIntMatBoolean() {
247         fail("Not yet implemented");
248     }
249
250     public void testMatchMatListOfDMatch() {
251         Mat train = getTrainDescriptors();
252         Mat query = getQueryDescriptors();
253         MatOfDMatch matches = new MatOfDMatch();
254         matcher.add(Arrays.asList(train));
255         matcher.train();
256
257         matcher.match(query, matches);
258
259         assertArrayDMatchEquals(truth, matches.toArray(), EPS);
260     }
261
262     public void testMatchMatListOfDMatchListOfMat() {
263         Mat train = getTrainDescriptors();
264         Mat query = getQueryDescriptors();
265         Mat mask = getMaskImg();
266         MatOfDMatch matches = new MatOfDMatch();
267         matcher.add(Arrays.asList(train));
268         matcher.train();
269
270         matcher.match(query, matches, Arrays.asList(mask));
271
272         assertArrayDMatchEquals(truth, matches.toArray(), EPS);
273     }
274
275     public void testMatchMatMatListOfDMatch() {
276         Mat train = getTrainDescriptors();
277         Mat query = getQueryDescriptors();
278         MatOfDMatch matches = new MatOfDMatch();
279
280         matcher.match(query, train, matches);
281
282         assertArrayDMatchEquals(truth, matches.toArray(), EPS);
283
284         // OpenCVTestRunner.Log(matches.toString());
285         // OpenCVTestRunner.Log(matches);
286     }
287
288     public void testMatchMatMatListOfDMatchMat() {
289         Mat train = getTrainDescriptors();
290         Mat query = getQueryDescriptors();
291         Mat mask = getMaskImg();
292         MatOfDMatch matches = new MatOfDMatch();
293
294         matcher.match(query, train, matches, mask);
295
296         assertListDMatchEquals(Arrays.asList(truth), matches.toList(), EPS);
297     }
298
299     public void testRadiusMatchMatListOfListOfDMatchFloat() {
300         fail("Not yet implemented");
301     }
302
303     public void testRadiusMatchMatListOfListOfDMatchFloatListOfMat() {
304         fail("Not yet implemented");
305     }
306
307     public void testRadiusMatchMatListOfListOfDMatchFloatListOfMatBoolean() {
308         fail("Not yet implemented");
309     }
310
311     public void testRadiusMatchMatMatListOfListOfDMatchFloat() {
312         fail("Not yet implemented");
313     }
314
315     public void testRadiusMatchMatMatListOfListOfDMatchFloatMat() {
316         fail("Not yet implemented");
317     }
318
319     public void testRadiusMatchMatMatListOfListOfDMatchFloatMatBoolean() {
320         fail("Not yet implemented");
321     }
322
323     public void testRead() {
324         String filenameR = OpenCVTestRunner.getTempFileName("yml");
325         String filenameW = OpenCVTestRunner.getTempFileName("yml");
326         writeFile(filenameR, ymlParamsModified);
327
328         matcher.read(filenameR);
329         matcher.write(filenameW);
330
331         assertEquals(ymlParamsModified, readFile(filenameW));
332     }
333
334     public void testTrain() {
335         Mat train = getTrainDescriptors();
336         matcher.add(Arrays.asList(train));
337         matcher.train();
338     }
339
340     public void testTrainNoData() {
341         try {
342             matcher.train();
343             fail("Expected CvException - FlannBasedMatcher::train should fail on empty train set");
344         } catch (CvException cverr) {
345             // expected
346         }
347     }
348
349     public void testWrite() {
350         String filename = OpenCVTestRunner.getTempFileName("xml");
351
352         matcher.write(filename);
353
354         assertEquals(xmlParamsDefault, readFile(filename));
355     }
356
357     public void testWriteYml() {
358         String filename = OpenCVTestRunner.getTempFileName("yml");
359
360         matcher.write(filename);
361
362         assertEquals(ymlParamsDefault, readFile(filename));
363     }
364
365 }