2011-08-04 03:46:30 +08:00
|
|
|
package org.opencv.test.features2d;
|
|
|
|
|
2012-04-09 16:07:18 +08:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
|
2011-08-04 03:46:30 +08:00
|
|
|
import org.opencv.core.Core;
|
|
|
|
import org.opencv.core.CvType;
|
|
|
|
import org.opencv.core.Mat;
|
2012-04-09 16:07:18 +08:00
|
|
|
import org.opencv.core.MatOfDMatch;
|
|
|
|
import org.opencv.core.MatOfKeyPoint;
|
2011-08-04 03:46:30 +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-04 03:46:30 +08:00
|
|
|
import org.opencv.features2d.DescriptorExtractor;
|
|
|
|
import org.opencv.features2d.DescriptorMatcher;
|
|
|
|
import org.opencv.features2d.FeatureDetector;
|
|
|
|
import org.opencv.test.OpenCVTestCase;
|
|
|
|
import org.opencv.test.OpenCVTestRunner;
|
2014-09-08 21:35:55 +08:00
|
|
|
import org.opencv.imgproc.Imgproc;
|
2011-08-04 03:46:30 +08:00
|
|
|
|
|
|
|
public class BruteForceHammingLUTDescriptorMatcherTest extends OpenCVTestCase {
|
|
|
|
|
|
|
|
DescriptorMatcher matcher;
|
|
|
|
int matSize;
|
|
|
|
DMatch[] truth;
|
|
|
|
|
2011-08-05 16:39:28 +08:00
|
|
|
private Mat getMaskImg() {
|
|
|
|
return new Mat(4, 4, CvType.CV_8U, new Scalar(0)) {
|
|
|
|
{
|
|
|
|
put(0, 0, 1, 1, 1, 1, 1, 1, 1, 1);
|
|
|
|
}
|
|
|
|
};
|
2011-08-04 03:46:30 +08:00
|
|
|
}
|
|
|
|
|
2011-08-05 16:39:28 +08:00
|
|
|
private Mat getQueryDescriptors() {
|
|
|
|
return getTestDescriptors(getQueryImg());
|
2011-08-04 03:46:30 +08:00
|
|
|
}
|
2011-08-05 16:39:28 +08:00
|
|
|
|
2011-08-04 03:46:30 +08:00
|
|
|
private Mat getQueryImg() {
|
|
|
|
Mat img = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
|
2014-08-14 17:48:52 +08:00
|
|
|
Imgproc.line(img, new Point(40, matSize - 40), new Point(matSize - 50, 50), new Scalar(0), 8);
|
2011-08-04 03:46:30 +08:00
|
|
|
return img;
|
|
|
|
}
|
2011-08-05 16:39:28 +08:00
|
|
|
|
2011-08-04 03:46:30 +08:00
|
|
|
private Mat getTestDescriptors(Mat img) {
|
2012-04-09 16:07:18 +08:00
|
|
|
MatOfKeyPoint keypoints = new MatOfKeyPoint();
|
2011-08-04 03:46:30 +08:00
|
|
|
Mat descriptors = new Mat();
|
|
|
|
|
|
|
|
FeatureDetector detector = FeatureDetector.create(FeatureDetector.FAST);
|
|
|
|
DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.BRIEF);
|
|
|
|
|
|
|
|
detector.detect(img, keypoints);
|
|
|
|
extractor.compute(img, keypoints, descriptors);
|
2011-08-05 16:39:28 +08:00
|
|
|
|
2011-08-04 03:46:30 +08:00
|
|
|
return descriptors;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Mat getTrainDescriptors() {
|
|
|
|
return getTestDescriptors(getTrainImg());
|
|
|
|
}
|
|
|
|
|
2011-08-05 16:39:28 +08:00
|
|
|
private Mat getTrainImg() {
|
|
|
|
Mat img = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
|
2014-08-14 17:48:52 +08:00
|
|
|
Imgproc.line(img, new Point(40, 40), new Point(matSize - 40, matSize - 40), new Scalar(0), 8);
|
2011-08-05 16:39:28 +08:00
|
|
|
return img;
|
|
|
|
}
|
|
|
|
|
|
|
|
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.BRUTEFORCE_HAMMINGLUT);
|
|
|
|
matSize = 100;
|
|
|
|
|
|
|
|
truth = new DMatch[] {
|
|
|
|
new DMatch(0, 0, 0, 51),
|
|
|
|
new DMatch(1, 2, 0, 42),
|
|
|
|
new DMatch(2, 1, 0, 40),
|
|
|
|
new DMatch(3, 3, 0, 53) };
|
2011-08-04 03:46:30 +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));
|
|
|
|
Mat truth = train.clone();
|
|
|
|
matcher.add(Arrays.asList(train));
|
|
|
|
|
|
|
|
DescriptorMatcher cloned = matcher.clone();
|
|
|
|
|
|
|
|
assertNotNull(cloned);
|
|
|
|
|
|
|
|
List<Mat> descriptors = cloned.getTrainDescriptors();
|
|
|
|
assertEquals(1, descriptors.size());
|
|
|
|
assertMatEqual(truth, descriptors.get(0));
|
|
|
|
}
|
|
|
|
|
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-04 03:46:30 +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() {
|
|
|
|
assertTrue(matcher.isMaskSupported());
|
|
|
|
}
|
|
|
|
|
2011-08-05 16:39:28 +08:00
|
|
|
public void testKnnMatchMatListOfListOfDMatchInt() {
|
|
|
|
fail("Not yet implemented");
|
|
|
|
}
|
2011-08-04 03:46:30 +08:00
|
|
|
|
2011-08-05 16:39:28 +08:00
|
|
|
public void testKnnMatchMatListOfListOfDMatchIntListOfMat() {
|
|
|
|
fail("Not yet implemented");
|
2011-08-04 03:46:30 +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-04 03:46:30 +08:00
|
|
|
Mat train = getTrainDescriptors();
|
|
|
|
Mat query = getQueryDescriptors();
|
2012-04-09 16:07:18 +08:00
|
|
|
MatOfDMatch matches = new MatOfDMatch();
|
2011-08-05 16:39:28 +08:00
|
|
|
matcher.add(Arrays.asList(train));
|
|
|
|
|
|
|
|
matcher.match(query, matches);
|
|
|
|
|
2012-04-09 16:07:18 +08:00
|
|
|
assertArrayDMatchEquals(truth, matches.toArray(), EPS);
|
2011-08-04 03:46:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testMatchMatListOfDMatchListOfMat() {
|
|
|
|
Mat train = getTrainDescriptors();
|
|
|
|
Mat query = getQueryDescriptors();
|
|
|
|
Mat mask = getMaskImg();
|
2012-04-09 16:07:18 +08:00
|
|
|
MatOfDMatch matches = new MatOfDMatch();
|
2011-08-04 03:46:30 +08:00
|
|
|
matcher.add(Arrays.asList(train));
|
|
|
|
|
|
|
|
matcher.match(query, matches, Arrays.asList(mask));
|
|
|
|
|
2012-04-09 16:07:18 +08:00
|
|
|
assertListDMatchEquals(Arrays.asList(truth[0], truth[1]), matches.toList(), EPS);
|
2011-08-04 03:46:30 +08:00
|
|
|
}
|
|
|
|
|
2011-08-05 16:39:28 +08:00
|
|
|
public void testMatchMatMatListOfDMatch() {
|
2011-08-04 03:46:30 +08:00
|
|
|
Mat train = getTrainDescriptors();
|
|
|
|
Mat query = getQueryDescriptors();
|
2012-04-09 16:07:18 +08:00
|
|
|
MatOfDMatch matches = new MatOfDMatch();
|
2011-08-05 16:39:28 +08:00
|
|
|
|
|
|
|
matcher.match(query, train, matches);
|
|
|
|
|
2012-04-19 22:21:56 +08:00
|
|
|
/*
|
2011-08-05 16:39:28 +08:00
|
|
|
OpenCVTestRunner.Log("matches found: " + matches.size());
|
2012-04-09 16:07:18 +08:00
|
|
|
for (DMatch m : matches.toArray())
|
2011-08-05 16:39:28 +08:00
|
|
|
OpenCVTestRunner.Log(m.toString());
|
2012-04-19 22:21:56 +08:00
|
|
|
*/
|
2011-08-05 16:39:28 +08:00
|
|
|
|
2012-04-09 16:07:18 +08:00
|
|
|
assertArrayDMatchEquals(truth, matches.toArray(), EPS);
|
2011-08-04 03:46:30 +08:00
|
|
|
}
|
|
|
|
|
2011-08-05 16:39:28 +08:00
|
|
|
public void testMatchMatMatListOfDMatchMat() {
|
|
|
|
Mat train = getTrainDescriptors();
|
|
|
|
Mat query = getQueryDescriptors();
|
|
|
|
Mat mask = getMaskImg();
|
2012-04-09 16:07:18 +08:00
|
|
|
MatOfDMatch matches = new MatOfDMatch();
|
2011-08-05 16:39:28 +08:00
|
|
|
|
|
|
|
matcher.match(query, train, matches, mask);
|
|
|
|
|
2012-04-09 16:07:18 +08:00
|
|
|
assertListDMatchEquals(Arrays.asList(truth[0], truth[1]), matches.toList(), EPS);
|
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-04 03:46:30 +08:00
|
|
|
public void testRead() {
|
|
|
|
String filename = OpenCVTestRunner.getTempFileName("yml");
|
|
|
|
writeFile(filename, "%YAML:1.0\n");
|
|
|
|
|
|
|
|
matcher.read(filename);
|
|
|
|
assertTrue(true);// BruteforceMatcher has no settings
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testTrain() {
|
|
|
|
matcher.train();// BruteforceMatcher does not need to train
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testWrite() {
|
|
|
|
String filename = OpenCVTestRunner.getTempFileName("yml");
|
|
|
|
|
|
|
|
matcher.write(filename);
|
|
|
|
|
|
|
|
String truth = "%YAML:1.0\n";
|
|
|
|
assertEquals(truth, readFile(filename));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|