2011-08-03 21:20:01 +08:00
|
|
|
package org.opencv.test.features2d;
|
|
|
|
|
2012-04-09 16:20:23 +08:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
|
2011-08-03 21:20:01 +08:00
|
|
|
import org.opencv.core.CvException;
|
|
|
|
import org.opencv.core.CvType;
|
|
|
|
import org.opencv.core.Mat;
|
2012-04-09 16:20:23 +08:00
|
|
|
import org.opencv.core.MatOfDMatch;
|
|
|
|
import org.opencv.core.MatOfKeyPoint;
|
2011-08-03 21:20:01 +08:00
|
|
|
import org.opencv.core.Point;
|
|
|
|
import org.opencv.core.Scalar;
|
2013-03-26 18:58:09 +08:00
|
|
|
import org.opencv.core.DMatch;
|
2011-08-03 21:20:01 +08:00
|
|
|
import org.opencv.features2d.DescriptorMatcher;
|
2020-05-29 21:20:28 +08:00
|
|
|
import org.opencv.features2d.FlannBasedMatcher;
|
2013-03-26 18:58:09 +08:00
|
|
|
import org.opencv.core.KeyPoint;
|
2011-08-03 21:20:01 +08:00
|
|
|
import org.opencv.test.OpenCVTestCase;
|
|
|
|
import org.opencv.test.OpenCVTestRunner;
|
2014-09-08 21:35:55 +08:00
|
|
|
import org.opencv.imgproc.Imgproc;
|
2016-10-18 01:53:59 +08:00
|
|
|
import org.opencv.features2d.Feature2D;
|
2011-08-03 21:20:01 +08:00
|
|
|
|
|
|
|
public class FlannBasedDescriptorMatcherTest extends OpenCVTestCase {
|
|
|
|
|
2011-08-05 16:39:28 +08:00
|
|
|
static final String xmlParamsDefault = "<?xml version=\"1.0\"?>\n"
|
|
|
|
+ "<opencv_storage>\n"
|
2016-10-18 01:53:59 +08:00
|
|
|
+ "<format>3</format>\n"
|
2011-08-05 16:39:28 +08:00
|
|
|
+ "<indexParams>\n"
|
|
|
|
+ " <_>\n"
|
|
|
|
+ " <name>algorithm</name>\n"
|
2018-09-19 18:55:26 +08:00
|
|
|
+ " <type>9</type>\n" // FLANN_INDEX_TYPE_ALGORITHM
|
2011-08-05 16:39:28 +08:00
|
|
|
+ " <value>1</value></_>\n"
|
|
|
|
+ " <_>\n"
|
|
|
|
+ " <name>trees</name>\n"
|
|
|
|
+ " <type>4</type>\n"
|
|
|
|
+ " <value>4</value></_></indexParams>\n"
|
|
|
|
+ "<searchParams>\n"
|
|
|
|
+ " <_>\n"
|
|
|
|
+ " <name>checks</name>\n"
|
|
|
|
+ " <type>4</type>\n"
|
|
|
|
+ " <value>32</value></_>\n"
|
|
|
|
+ " <_>\n"
|
|
|
|
+ " <name>eps</name>\n"
|
|
|
|
+ " <type>5</type>\n"
|
|
|
|
+ " <value>0.</value></_>\n"
|
|
|
|
+ " <_>\n"
|
|
|
|
+ " <name>sorted</name>\n"
|
2018-09-19 18:55:26 +08:00
|
|
|
+ " <type>8</type>\n" // FLANN_INDEX_TYPE_BOOL
|
2011-08-05 16:39:28 +08:00
|
|
|
+ " <value>1</value></_></searchParams>\n"
|
|
|
|
+ "</opencv_storage>\n";
|
2016-10-19 02:22:18 +08:00
|
|
|
static final String ymlParamsDefault = "%YAML:1.0\n---\n"
|
2016-10-18 01:53:59 +08:00
|
|
|
+ "format: 3\n"
|
2011-08-05 16:39:28 +08:00
|
|
|
+ "indexParams:\n"
|
|
|
|
+ " -\n"
|
|
|
|
+ " name: algorithm\n"
|
2018-09-19 18:55:26 +08:00
|
|
|
+ " type: 9\n" // FLANN_INDEX_TYPE_ALGORITHM
|
2011-08-05 16:39:28 +08:00
|
|
|
+ " value: 1\n"
|
|
|
|
+ " -\n"
|
|
|
|
+ " name: trees\n"
|
|
|
|
+ " type: 4\n"
|
|
|
|
+ " value: 4\n"
|
|
|
|
+ "searchParams:\n"
|
|
|
|
+ " -\n"
|
|
|
|
+ " name: checks\n"
|
|
|
|
+ " type: 4\n"
|
|
|
|
+ " value: 32\n"
|
|
|
|
+ " -\n"
|
|
|
|
+ " name: eps\n"
|
|
|
|
+ " type: 5\n"
|
|
|
|
+ " value: 0.\n"
|
|
|
|
+ " -\n"
|
|
|
|
+ " name: sorted\n"
|
2018-09-19 18:55:26 +08:00
|
|
|
+ " type: 8\n" // FLANN_INDEX_TYPE_BOOL
|
2011-08-05 16:39:28 +08:00
|
|
|
+ " value: 1\n";
|
2016-10-19 02:22:18 +08:00
|
|
|
static final String ymlParamsModified = "%YAML:1.0\n---\n"
|
2016-10-18 01:53:59 +08:00
|
|
|
+ "format: 3\n"
|
2011-08-05 16:39:28 +08:00
|
|
|
+ "indexParams:\n"
|
|
|
|
+ " -\n"
|
|
|
|
+ " name: algorithm\n"
|
2018-09-19 18:55:26 +08:00
|
|
|
+ " type: 9\n" // FLANN_INDEX_TYPE_ALGORITHM
|
2011-08-05 16:39:28 +08:00
|
|
|
+ " value: 6\n"// this line is changed!
|
|
|
|
+ " -\n"
|
|
|
|
+ " name: trees\n"
|
|
|
|
+ " type: 4\n"
|
|
|
|
+ " value: 4\n"
|
|
|
|
+ "searchParams:\n"
|
|
|
|
+ " -\n"
|
|
|
|
+ " name: checks\n"
|
|
|
|
+ " type: 4\n"
|
|
|
|
+ " value: 32\n"
|
|
|
|
+ " -\n"
|
|
|
|
+ " name: eps\n"
|
|
|
|
+ " type: 5\n"
|
2012-04-11 20:37:24 +08:00
|
|
|
+ " value: 4.\n"// this line is changed!
|
2011-08-05 16:39:28 +08:00
|
|
|
+ " -\n"
|
|
|
|
+ " name: sorted\n"
|
2018-09-19 18:55:26 +08:00
|
|
|
+ " type: 8\n" // FLANN_INDEX_TYPE_BOOL
|
2011-08-05 16:39:28 +08:00
|
|
|
+ " value: 1\n";
|
|
|
|
|
2011-08-03 21:20:01 +08:00
|
|
|
DescriptorMatcher matcher;
|
2011-08-05 16:39:28 +08:00
|
|
|
|
2011-08-03 21:20:01 +08:00
|
|
|
int matSize;
|
2011-08-04 22:13:07 +08:00
|
|
|
|
2011-08-05 16:39:28 +08:00
|
|
|
DMatch[] truth;
|
2011-08-03 21:20:01 +08:00
|
|
|
|
2011-08-05 16:39:28 +08:00
|
|
|
private Mat getMaskImg() {
|
|
|
|
return new Mat(5, 2, CvType.CV_8U, new Scalar(0)) {
|
|
|
|
{
|
|
|
|
put(0, 0, 1, 1, 1, 1);
|
|
|
|
}
|
|
|
|
};
|
2011-08-03 21:20:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private Mat getQueryDescriptors() {
|
|
|
|
Mat img = getQueryImg();
|
2012-04-09 16:20:23 +08:00
|
|
|
MatOfKeyPoint keypoints = new MatOfKeyPoint();
|
2011-08-03 21:20:01 +08:00
|
|
|
Mat descriptors = new Mat();
|
|
|
|
|
2016-10-19 02:22:18 +08:00
|
|
|
Feature2D detector = createClassInstance(XFEATURES2D+"SURF", DEFAULT_FACTORY, null, null);
|
|
|
|
Feature2D extractor = createClassInstance(XFEATURES2D+"SURF", DEFAULT_FACTORY, null, null);
|
2011-08-03 21:20:01 +08:00
|
|
|
|
2016-10-18 01:53:59 +08:00
|
|
|
setProperty(detector, "hessianThreshold", "double", 8000);
|
|
|
|
setProperty(detector, "nOctaves", "int", 3);
|
|
|
|
setProperty(detector, "upright", "boolean", false);
|
2011-08-03 21:20:01 +08:00
|
|
|
|
|
|
|
detector.detect(img, keypoints);
|
|
|
|
extractor.compute(img, keypoints, descriptors);
|
|
|
|
|
|
|
|
return descriptors;
|
|
|
|
}
|
|
|
|
|
2011-08-05 16:39:28 +08:00
|
|
|
private Mat getQueryImg() {
|
|
|
|
Mat cross = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
|
2014-08-14 17:48:52 +08:00
|
|
|
Imgproc.line(cross, new Point(30, matSize / 2), new Point(matSize - 31, matSize / 2), new Scalar(100), 3);
|
|
|
|
Imgproc.line(cross, new Point(matSize / 2, 30), new Point(matSize / 2, matSize - 31), new Scalar(100), 3);
|
2011-08-05 16:39:28 +08:00
|
|
|
|
|
|
|
return cross;
|
|
|
|
}
|
|
|
|
|
2011-08-03 21:20:01 +08:00
|
|
|
private Mat getTrainDescriptors() {
|
|
|
|
Mat img = getTrainImg();
|
2012-04-09 16:20:23 +08:00
|
|
|
MatOfKeyPoint keypoints = new MatOfKeyPoint(new KeyPoint(50, 50, 16, 0, 20000, 1, -1), new KeyPoint(42, 42, 16, 160, 10000, 1, -1));
|
2011-08-03 21:20:01 +08:00
|
|
|
Mat descriptors = new Mat();
|
|
|
|
|
2016-10-19 02:22:18 +08:00
|
|
|
Feature2D extractor = createClassInstance(XFEATURES2D+"SURF", DEFAULT_FACTORY, null, null);
|
2011-08-03 21:20:01 +08:00
|
|
|
|
|
|
|
extractor.compute(img, keypoints, descriptors);
|
|
|
|
|
|
|
|
return descriptors;
|
|
|
|
}
|
|
|
|
|
2011-08-05 16:39:28 +08:00
|
|
|
private Mat getTrainImg() {
|
|
|
|
Mat cross = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
|
2014-08-14 17:48:52 +08:00
|
|
|
Imgproc.line(cross, new Point(20, matSize / 2), new Point(matSize - 21, matSize / 2), new Scalar(100), 2);
|
|
|
|
Imgproc.line(cross, new Point(matSize / 2, 20), new Point(matSize / 2, matSize - 21), new Scalar(100), 2);
|
2011-08-05 16:39:28 +08:00
|
|
|
|
|
|
|
return cross;
|
2011-08-04 22:13:07 +08:00
|
|
|
}
|
|
|
|
|
2011-08-05 16:39:28 +08:00
|
|
|
protected void setUp() throws Exception {
|
2012-12-28 21:03:35 +08:00
|
|
|
super.setUp();
|
2011-08-05 16:39:28 +08:00
|
|
|
matcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);
|
|
|
|
matSize = 100;
|
|
|
|
truth = new DMatch[] {
|
2019-11-18 06:39:27 +08:00
|
|
|
new DMatch(0, 0, 0, 0.6159003f),
|
2013-02-01 18:37:24 +08:00
|
|
|
new DMatch(1, 1, 0, 0.9177120f),
|
|
|
|
new DMatch(2, 1, 0, 0.3112163f),
|
|
|
|
new DMatch(3, 1, 0, 0.2925075f),
|
2019-11-18 06:39:27 +08:00
|
|
|
new DMatch(4, 1, 0, 0.26520672f)
|
2012-04-11 03:17:44 +08:00
|
|
|
};
|
2011-08-04 22:13:07 +08:00
|
|
|
}
|
2011-08-03 21:20:01 +08:00
|
|
|
|
2020-05-29 21:20:28 +08:00
|
|
|
// https://github.com/opencv/opencv/issues/11268
|
|
|
|
public void testConstructor()
|
|
|
|
{
|
|
|
|
FlannBasedMatcher self_created_matcher = new FlannBasedMatcher();
|
|
|
|
Mat train = new Mat(1, 1, CvType.CV_8U, new Scalar(123));
|
|
|
|
self_created_matcher.add(Arrays.asList(train));
|
|
|
|
assertTrue(!self_created_matcher.empty());
|
|
|
|
}
|
|
|
|
|
2011-08-03 21:20:01 +08:00
|
|
|
public void testAdd() {
|
|
|
|
matcher.add(Arrays.asList(new Mat()));
|
|
|
|
assertFalse(matcher.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testClear() {
|
|
|
|
matcher.add(Arrays.asList(new Mat()));
|
|
|
|
|
|
|
|
matcher.clear();
|
|
|
|
|
|
|
|
assertTrue(matcher.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testClone() {
|
|
|
|
Mat train = new Mat(1, 1, CvType.CV_8U, new Scalar(123));
|
|
|
|
matcher.add(Arrays.asList(train));
|
|
|
|
|
|
|
|
try {
|
|
|
|
matcher.clone();
|
|
|
|
fail("Expected CvException (CV_StsNotImplemented)");
|
|
|
|
} catch (CvException cverr) {
|
|
|
|
// expected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-05 16:39:28 +08:00
|
|
|
public void testCloneBoolean() {
|
|
|
|
matcher.add(Arrays.asList(new Mat()));
|
|
|
|
|
|
|
|
DescriptorMatcher cloned = matcher.clone(true);
|
|
|
|
|
|
|
|
assertNotNull(cloned);
|
|
|
|
assertTrue(cloned.empty());
|
|
|
|
}
|
|
|
|
|
2011-08-03 21:20:01 +08:00
|
|
|
public void testCreate() {
|
|
|
|
assertNotNull(matcher);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testEmpty() {
|
|
|
|
assertTrue(matcher.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testGetTrainDescriptors() {
|
|
|
|
Mat train = new Mat(1, 1, CvType.CV_8U, new Scalar(123));
|
|
|
|
Mat truth = train.clone();
|
|
|
|
matcher.add(Arrays.asList(train));
|
|
|
|
|
|
|
|
List<Mat> descriptors = matcher.getTrainDescriptors();
|
|
|
|
|
|
|
|
assertEquals(1, descriptors.size());
|
|
|
|
assertMatEqual(truth, descriptors.get(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testIsMaskSupported() {
|
|
|
|
assertFalse(matcher.isMaskSupported());
|
|
|
|
}
|
|
|
|
|
2011-08-05 16:39:28 +08:00
|
|
|
public void testKnnMatchMatListOfListOfDMatchInt() {
|
|
|
|
fail("Not yet implemented");
|
|
|
|
}
|
2011-08-03 21:20:01 +08:00
|
|
|
|
2011-08-05 16:39:28 +08:00
|
|
|
public void testKnnMatchMatListOfListOfDMatchIntListOfMat() {
|
|
|
|
fail("Not yet implemented");
|
2011-08-03 21:20:01 +08:00
|
|
|
}
|
|
|
|
|
2011-08-05 16:39:28 +08:00
|
|
|
public void testKnnMatchMatListOfListOfDMatchIntListOfMatBoolean() {
|
|
|
|
fail("Not yet implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testKnnMatchMatMatListOfListOfDMatchInt() {
|
|
|
|
fail("Not yet implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testKnnMatchMatMatListOfListOfDMatchIntMat() {
|
|
|
|
fail("Not yet implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testKnnMatchMatMatListOfListOfDMatchIntMatBoolean() {
|
|
|
|
fail("Not yet implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testMatchMatListOfDMatch() {
|
2011-08-03 21:20:01 +08:00
|
|
|
Mat train = getTrainDescriptors();
|
|
|
|
Mat query = getQueryDescriptors();
|
2012-04-09 16:20:23 +08:00
|
|
|
MatOfDMatch matches = new MatOfDMatch();
|
2011-08-05 16:39:28 +08:00
|
|
|
matcher.add(Arrays.asList(train));
|
|
|
|
matcher.train();
|
|
|
|
|
|
|
|
matcher.match(query, matches);
|
2011-08-03 21:20:01 +08:00
|
|
|
|
2012-04-09 16:20:23 +08:00
|
|
|
assertArrayDMatchEquals(truth, matches.toArray(), EPS);
|
2011-08-03 21:20:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testMatchMatListOfDMatchListOfMat() {
|
|
|
|
Mat train = getTrainDescriptors();
|
|
|
|
Mat query = getQueryDescriptors();
|
|
|
|
Mat mask = getMaskImg();
|
2012-04-09 16:20:23 +08:00
|
|
|
MatOfDMatch matches = new MatOfDMatch();
|
2011-08-03 21:20:01 +08:00
|
|
|
matcher.add(Arrays.asList(train));
|
|
|
|
matcher.train();
|
|
|
|
|
|
|
|
matcher.match(query, matches, Arrays.asList(mask));
|
|
|
|
|
2012-04-09 16:20:23 +08:00
|
|
|
assertArrayDMatchEquals(truth, matches.toArray(), EPS);
|
2011-08-03 21:20:01 +08:00
|
|
|
}
|
|
|
|
|
2011-08-05 16:39:28 +08:00
|
|
|
public void testMatchMatMatListOfDMatch() {
|
2011-08-03 21:20:01 +08:00
|
|
|
Mat train = getTrainDescriptors();
|
|
|
|
Mat query = getQueryDescriptors();
|
2012-04-09 16:20:23 +08:00
|
|
|
MatOfDMatch matches = new MatOfDMatch();
|
2011-08-05 16:39:28 +08:00
|
|
|
|
|
|
|
matcher.match(query, train, matches);
|
|
|
|
|
2012-04-09 16:20:23 +08:00
|
|
|
assertArrayDMatchEquals(truth, matches.toArray(), EPS);
|
2011-08-05 16:39:28 +08:00
|
|
|
|
2012-07-03 20:44:32 +08:00
|
|
|
// OpenCVTestRunner.Log(matches.toString());
|
|
|
|
// OpenCVTestRunner.Log(matches);
|
2011-08-05 16:39:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testMatchMatMatListOfDMatchMat() {
|
|
|
|
Mat train = getTrainDescriptors();
|
|
|
|
Mat query = getQueryDescriptors();
|
|
|
|
Mat mask = getMaskImg();
|
2012-04-09 16:20:23 +08:00
|
|
|
MatOfDMatch matches = new MatOfDMatch();
|
2011-08-05 16:39:28 +08:00
|
|
|
|
|
|
|
matcher.match(query, train, matches, mask);
|
|
|
|
|
2012-04-09 16:20:23 +08:00
|
|
|
assertListDMatchEquals(Arrays.asList(truth), matches.toList(), EPS);
|
2011-08-03 21:20:01 +08:00
|
|
|
}
|
|
|
|
|
2011-08-05 16:39:28 +08:00
|
|
|
public void testRadiusMatchMatListOfListOfDMatchFloat() {
|
|
|
|
fail("Not yet implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testRadiusMatchMatListOfListOfDMatchFloatListOfMat() {
|
|
|
|
fail("Not yet implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testRadiusMatchMatListOfListOfDMatchFloatListOfMatBoolean() {
|
|
|
|
fail("Not yet implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testRadiusMatchMatMatListOfListOfDMatchFloat() {
|
|
|
|
fail("Not yet implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testRadiusMatchMatMatListOfListOfDMatchFloatMat() {
|
|
|
|
fail("Not yet implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testRadiusMatchMatMatListOfListOfDMatchFloatMatBoolean() {
|
|
|
|
fail("Not yet implemented");
|
|
|
|
}
|
|
|
|
|
2011-08-03 21:20:01 +08:00
|
|
|
public void testRead() {
|
2012-04-11 20:37:24 +08:00
|
|
|
String filenameR = OpenCVTestRunner.getTempFileName("yml");
|
|
|
|
String filenameW = OpenCVTestRunner.getTempFileName("yml");
|
|
|
|
writeFile(filenameR, ymlParamsModified);
|
|
|
|
|
|
|
|
matcher.read(filenameR);
|
|
|
|
matcher.write(filenameW);
|
2012-07-03 20:44:32 +08:00
|
|
|
|
2012-04-11 20:37:24 +08:00
|
|
|
assertEquals(ymlParamsModified, readFile(filenameW));
|
2011-08-03 21:20:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testTrain() {
|
|
|
|
Mat train = getTrainDescriptors();
|
|
|
|
matcher.add(Arrays.asList(train));
|
|
|
|
matcher.train();
|
|
|
|
}
|
2011-08-05 16:39:28 +08:00
|
|
|
|
2011-08-03 21:20:01 +08:00
|
|
|
public void testTrainNoData() {
|
|
|
|
try {
|
|
|
|
matcher.train();
|
|
|
|
fail("Expected CvException - FlannBasedMatcher::train should fail on empty train set");
|
|
|
|
} catch (CvException cverr) {
|
|
|
|
// expected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testWrite() {
|
2011-08-04 22:13:07 +08:00
|
|
|
String filename = OpenCVTestRunner.getTempFileName("xml");
|
|
|
|
|
|
|
|
matcher.write(filename);
|
|
|
|
|
|
|
|
assertEquals(xmlParamsDefault, readFile(filename));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testWriteYml() {
|
2011-08-03 21:20:01 +08:00
|
|
|
String filename = OpenCVTestRunner.getTempFileName("yml");
|
|
|
|
|
|
|
|
matcher.write(filename);
|
|
|
|
|
2011-08-04 22:13:07 +08:00
|
|
|
assertEquals(ymlParamsDefault, readFile(filename));
|
2011-08-03 21:20:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|