1 package org.opencv.test.features2d;
3 import java.util.Arrays;
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;
22 public class FlannBasedDescriptorMatcherTest extends OpenCVTestCase {
24 static final String xmlParamsDefault = "<?xml version=\"1.0\"?>\n"
25 + "<opencv_storage>\n"
28 + " <name>algorithm</name>\n"
29 + " <type>23</type>\n"
30 + " <value>1</value></_>\n"
32 + " <name>trees</name>\n"
34 + " <value>4</value></_></indexParams>\n"
37 + " <name>checks</name>\n"
39 + " <value>32</value></_>\n"
41 + " <name>eps</name>\n"
43 + " <value>0.</value></_>\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"
52 + " name: algorithm\n"
72 static final String ymlParamsModified = "%YAML:1.0\n"
75 + " name: algorithm\n"
77 + " value: 6\n"// this line is changed!
90 + " value: 4.\n"// this line is changed!
96 DescriptorMatcher matcher;
102 private Mat getMaskImg() {
103 return new Mat(5, 2, CvType.CV_8U, new Scalar(0)) {
105 put(0, 0, 1, 1, 1, 1);
110 private Mat getQueryDescriptors() {
111 Mat img = getQueryImg();
112 MatOfKeyPoint keypoints = new MatOfKeyPoint();
113 Mat descriptors = new Mat();
115 FeatureDetector detector = FeatureDetector.create(FeatureDetector.SURF);
116 DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
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);
122 detector.detect(img, keypoints);
123 extractor.compute(img, keypoints, descriptors);
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);
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();
141 DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
143 extractor.compute(img, keypoints, descriptors);
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);
156 protected void setUp() throws Exception {
158 matcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);
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)
169 public void testAdd() {
170 matcher.add(Arrays.asList(new Mat()));
171 assertFalse(matcher.empty());
174 public void testClear() {
175 matcher.add(Arrays.asList(new Mat()));
179 assertTrue(matcher.empty());
182 public void testClone() {
183 Mat train = new Mat(1, 1, CvType.CV_8U, new Scalar(123));
184 matcher.add(Arrays.asList(train));
188 fail("Expected CvException (CV_StsNotImplemented)");
189 } catch (CvException cverr) {
194 public void testCloneBoolean() {
195 matcher.add(Arrays.asList(new Mat()));
197 DescriptorMatcher cloned = matcher.clone(true);
199 assertNotNull(cloned);
200 assertTrue(cloned.empty());
203 public void testCreate() {
204 assertNotNull(matcher);
207 public void testEmpty() {
208 assertTrue(matcher.empty());
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));
216 List<Mat> descriptors = matcher.getTrainDescriptors();
218 assertEquals(1, descriptors.size());
219 assertMatEqual(truth, descriptors.get(0));
222 public void testIsMaskSupported() {
223 assertFalse(matcher.isMaskSupported());
226 public void testKnnMatchMatListOfListOfDMatchInt() {
227 fail("Not yet implemented");
230 public void testKnnMatchMatListOfListOfDMatchIntListOfMat() {
231 fail("Not yet implemented");
234 public void testKnnMatchMatListOfListOfDMatchIntListOfMatBoolean() {
235 fail("Not yet implemented");
238 public void testKnnMatchMatMatListOfListOfDMatchInt() {
239 fail("Not yet implemented");
242 public void testKnnMatchMatMatListOfListOfDMatchIntMat() {
243 fail("Not yet implemented");
246 public void testKnnMatchMatMatListOfListOfDMatchIntMatBoolean() {
247 fail("Not yet implemented");
250 public void testMatchMatListOfDMatch() {
251 Mat train = getTrainDescriptors();
252 Mat query = getQueryDescriptors();
253 MatOfDMatch matches = new MatOfDMatch();
254 matcher.add(Arrays.asList(train));
257 matcher.match(query, matches);
259 assertArrayDMatchEquals(truth, matches.toArray(), EPS);
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));
270 matcher.match(query, matches, Arrays.asList(mask));
272 assertArrayDMatchEquals(truth, matches.toArray(), EPS);
275 public void testMatchMatMatListOfDMatch() {
276 Mat train = getTrainDescriptors();
277 Mat query = getQueryDescriptors();
278 MatOfDMatch matches = new MatOfDMatch();
280 matcher.match(query, train, matches);
282 assertArrayDMatchEquals(truth, matches.toArray(), EPS);
284 // OpenCVTestRunner.Log(matches.toString());
285 // OpenCVTestRunner.Log(matches);
288 public void testMatchMatMatListOfDMatchMat() {
289 Mat train = getTrainDescriptors();
290 Mat query = getQueryDescriptors();
291 Mat mask = getMaskImg();
292 MatOfDMatch matches = new MatOfDMatch();
294 matcher.match(query, train, matches, mask);
296 assertListDMatchEquals(Arrays.asList(truth), matches.toList(), EPS);
299 public void testRadiusMatchMatListOfListOfDMatchFloat() {
300 fail("Not yet implemented");
303 public void testRadiusMatchMatListOfListOfDMatchFloatListOfMat() {
304 fail("Not yet implemented");
307 public void testRadiusMatchMatListOfListOfDMatchFloatListOfMatBoolean() {
308 fail("Not yet implemented");
311 public void testRadiusMatchMatMatListOfListOfDMatchFloat() {
312 fail("Not yet implemented");
315 public void testRadiusMatchMatMatListOfListOfDMatchFloatMat() {
316 fail("Not yet implemented");
319 public void testRadiusMatchMatMatListOfListOfDMatchFloatMatBoolean() {
320 fail("Not yet implemented");
323 public void testRead() {
324 String filenameR = OpenCVTestRunner.getTempFileName("yml");
325 String filenameW = OpenCVTestRunner.getTempFileName("yml");
326 writeFile(filenameR, ymlParamsModified);
328 matcher.read(filenameR);
329 matcher.write(filenameW);
331 assertEquals(ymlParamsModified, readFile(filenameW));
334 public void testTrain() {
335 Mat train = getTrainDescriptors();
336 matcher.add(Arrays.asList(train));
340 public void testTrainNoData() {
343 fail("Expected CvException - FlannBasedMatcher::train should fail on empty train set");
344 } catch (CvException cverr) {
349 public void testWrite() {
350 String filename = OpenCVTestRunner.getTempFileName("xml");
352 matcher.write(filename);
354 assertEquals(xmlParamsDefault, readFile(filename));
357 public void testWriteYml() {
358 String filename = OpenCVTestRunner.getTempFileName("yml");
360 matcher.write(filename);
362 assertEquals(ymlParamsDefault, readFile(filename));