mirror of https://github.com/opencv/opencv.git
Open Source Computer Vision Library
https://opencv.org/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
367 lines
11 KiB
367 lines
11 KiB
13 years ago
|
package org.opencv.test.features2d;
|
||
|
|
||
13 years ago
|
import java.util.Arrays;
|
||
|
import java.util.List;
|
||
|
|
||
13 years ago
|
import org.opencv.core.Core;
|
||
|
import org.opencv.core.CvException;
|
||
|
import org.opencv.core.CvType;
|
||
|
import org.opencv.core.Mat;
|
||
13 years ago
|
import org.opencv.core.MatOfDMatch;
|
||
|
import org.opencv.core.MatOfKeyPoint;
|
||
13 years ago
|
import org.opencv.core.Point;
|
||
|
import org.opencv.core.Scalar;
|
||
12 years ago
|
import org.opencv.core.DMatch;
|
||
13 years ago
|
import org.opencv.features2d.DescriptorExtractor;
|
||
|
import org.opencv.features2d.DescriptorMatcher;
|
||
|
import org.opencv.features2d.FeatureDetector;
|
||
12 years ago
|
import org.opencv.core.KeyPoint;
|
||
13 years ago
|
import org.opencv.test.OpenCVTestCase;
|
||
|
import org.opencv.test.OpenCVTestRunner;
|
||
10 years ago
|
import org.opencv.imgproc.Imgproc;
|
||
13 years ago
|
|
||
|
public class FlannBasedDescriptorMatcherTest extends OpenCVTestCase {
|
||
|
|
||
13 years ago
|
static final String xmlParamsDefault = "<?xml version=\"1.0\"?>\n"
|
||
|
+ "<opencv_storage>\n"
|
||
|
+ "<indexParams>\n"
|
||
|
+ " <_>\n"
|
||
|
+ " <name>algorithm</name>\n"
|
||
|
+ " <type>23</type>\n"
|
||
|
+ " <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"
|
||
|
+ " <type>15</type>\n"
|
||
|
+ " <value>1</value></_></searchParams>\n"
|
||
|
+ "</opencv_storage>\n";
|
||
|
static final String ymlParamsDefault = "%YAML:1.0\n"
|
||
|
+ "indexParams:\n"
|
||
|
+ " -\n"
|
||
|
+ " name: algorithm\n"
|
||
|
+ " type: 23\n"
|
||
|
+ " 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"
|
||
|
+ " type: 15\n"
|
||
|
+ " value: 1\n";
|
||
|
static final String ymlParamsModified = "%YAML:1.0\n"
|
||
|
+ "indexParams:\n"
|
||
|
+ " -\n"
|
||
|
+ " name: algorithm\n"
|
||
|
+ " type: 23\n"
|
||
|
+ " 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"
|
||
13 years ago
|
+ " value: 4.\n"// this line is changed!
|
||
13 years ago
|
+ " -\n"
|
||
|
+ " name: sorted\n"
|
||
|
+ " type: 15\n"
|
||
|
+ " value: 1\n";
|
||
|
|
||
13 years ago
|
DescriptorMatcher matcher;
|
||
13 years ago
|
|
||
13 years ago
|
int matSize;
|
||
13 years ago
|
|
||
13 years ago
|
DMatch[] truth;
|
||
13 years ago
|
|
||
13 years ago
|
private Mat getMaskImg() {
|
||
|
return new Mat(5, 2, CvType.CV_8U, new Scalar(0)) {
|
||
|
{
|
||
|
put(0, 0, 1, 1, 1, 1);
|
||
|
}
|
||
|
};
|
||
13 years ago
|
}
|
||
|
|
||
|
private Mat getQueryDescriptors() {
|
||
|
Mat img = getQueryImg();
|
||
13 years ago
|
MatOfKeyPoint keypoints = new MatOfKeyPoint();
|
||
13 years ago
|
Mat descriptors = new Mat();
|
||
|
|
||
|
FeatureDetector detector = FeatureDetector.create(FeatureDetector.SURF);
|
||
|
DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
|
||
|
|
||
|
String filename = OpenCVTestRunner.getTempFileName("yml");
|
||
|
writeFile(filename, "%YAML:1.0\nhessianThreshold: 8000.\noctaves: 3\noctaveLayers: 4\nupright: 0\n");
|
||
|
detector.read(filename);
|
||
|
|
||
|
detector.detect(img, keypoints);
|
||
|
extractor.compute(img, keypoints, descriptors);
|
||
|
|
||
|
return descriptors;
|
||
|
}
|
||
|
|
||
13 years ago
|
private Mat getQueryImg() {
|
||
|
Mat cross = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
|
||
10 years ago
|
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);
|
||
13 years ago
|
|
||
|
return cross;
|
||
|
}
|
||
|
|
||
13 years ago
|
private Mat getTrainDescriptors() {
|
||
|
Mat img = getTrainImg();
|
||
13 years ago
|
MatOfKeyPoint keypoints = new MatOfKeyPoint(new KeyPoint(50, 50, 16, 0, 20000, 1, -1), new KeyPoint(42, 42, 16, 160, 10000, 1, -1));
|
||
13 years ago
|
Mat descriptors = new Mat();
|
||
|
|
||
|
DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
|
||
|
|
||
|
extractor.compute(img, keypoints, descriptors);
|
||
|
|
||
|
return descriptors;
|
||
|
}
|
||
|
|
||
13 years ago
|
private Mat getTrainImg() {
|
||
|
Mat cross = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
|
||
10 years ago
|
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);
|
||
13 years ago
|
|
||
|
return cross;
|
||
13 years ago
|
}
|
||
|
|
||
13 years ago
|
protected void setUp() throws Exception {
|
||
12 years ago
|
super.setUp();
|
||
13 years ago
|
matcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);
|
||
|
matSize = 100;
|
||
|
truth = new DMatch[] {
|
||
12 years ago
|
new DMatch(0, 0, 0, 0.6211397f),
|
||
|
new DMatch(1, 1, 0, 0.9177120f),
|
||
|
new DMatch(2, 1, 0, 0.3112163f),
|
||
|
new DMatch(3, 1, 0, 0.2925075f),
|
||
|
new DMatch(4, 1, 0, 0.9309179f)
|
||
13 years ago
|
};
|
||
13 years ago
|
}
|
||
13 years ago
|
|
||
|
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
|
||
|
}
|
||
|
}
|
||
|
|
||
13 years ago
|
public void testCloneBoolean() {
|
||
|
matcher.add(Arrays.asList(new Mat()));
|
||
|
|
||
|
DescriptorMatcher cloned = matcher.clone(true);
|
||
|
|
||
|
assertNotNull(cloned);
|
||
|
assertTrue(cloned.empty());
|
||
|
}
|
||
|
|
||
13 years ago
|
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());
|
||
|
}
|
||
|
|
||
13 years ago
|
public void testKnnMatchMatListOfListOfDMatchInt() {
|
||
|
fail("Not yet implemented");
|
||
|
}
|
||
13 years ago
|
|
||
13 years ago
|
public void testKnnMatchMatListOfListOfDMatchIntListOfMat() {
|
||
|
fail("Not yet implemented");
|
||
13 years ago
|
}
|
||
|
|
||
13 years ago
|
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() {
|
||
13 years ago
|
Mat train = getTrainDescriptors();
|
||
|
Mat query = getQueryDescriptors();
|
||
13 years ago
|
MatOfDMatch matches = new MatOfDMatch();
|
||
13 years ago
|
matcher.add(Arrays.asList(train));
|
||
|
matcher.train();
|
||
|
|
||
|
matcher.match(query, matches);
|
||
13 years ago
|
|
||
13 years ago
|
assertArrayDMatchEquals(truth, matches.toArray(), EPS);
|
||
13 years ago
|
}
|
||
|
|
||
|
public void testMatchMatListOfDMatchListOfMat() {
|
||
|
Mat train = getTrainDescriptors();
|
||
|
Mat query = getQueryDescriptors();
|
||
|
Mat mask = getMaskImg();
|
||
13 years ago
|
MatOfDMatch matches = new MatOfDMatch();
|
||
13 years ago
|
matcher.add(Arrays.asList(train));
|
||
|
matcher.train();
|
||
|
|
||
|
matcher.match(query, matches, Arrays.asList(mask));
|
||
|
|
||
13 years ago
|
assertArrayDMatchEquals(truth, matches.toArray(), EPS);
|
||
13 years ago
|
}
|
||
|
|
||
13 years ago
|
public void testMatchMatMatListOfDMatch() {
|
||
13 years ago
|
Mat train = getTrainDescriptors();
|
||
|
Mat query = getQueryDescriptors();
|
||
13 years ago
|
MatOfDMatch matches = new MatOfDMatch();
|
||
13 years ago
|
|
||
|
matcher.match(query, train, matches);
|
||
|
|
||
13 years ago
|
assertArrayDMatchEquals(truth, matches.toArray(), EPS);
|
||
13 years ago
|
|
||
13 years ago
|
// OpenCVTestRunner.Log(matches.toString());
|
||
|
// OpenCVTestRunner.Log(matches);
|
||
13 years ago
|
}
|
||
|
|
||
|
public void testMatchMatMatListOfDMatchMat() {
|
||
|
Mat train = getTrainDescriptors();
|
||
|
Mat query = getQueryDescriptors();
|
||
|
Mat mask = getMaskImg();
|
||
13 years ago
|
MatOfDMatch matches = new MatOfDMatch();
|
||
13 years ago
|
|
||
|
matcher.match(query, train, matches, mask);
|
||
|
|
||
13 years ago
|
assertListDMatchEquals(Arrays.asList(truth), matches.toList(), EPS);
|
||
13 years ago
|
}
|
||
|
|
||
13 years ago
|
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");
|
||
|
}
|
||
|
|
||
13 years ago
|
public void testRead() {
|
||
13 years ago
|
String filenameR = OpenCVTestRunner.getTempFileName("yml");
|
||
|
String filenameW = OpenCVTestRunner.getTempFileName("yml");
|
||
|
writeFile(filenameR, ymlParamsModified);
|
||
|
|
||
|
matcher.read(filenameR);
|
||
|
matcher.write(filenameW);
|
||
13 years ago
|
|
||
13 years ago
|
assertEquals(ymlParamsModified, readFile(filenameW));
|
||
13 years ago
|
}
|
||
|
|
||
|
public void testTrain() {
|
||
|
Mat train = getTrainDescriptors();
|
||
|
matcher.add(Arrays.asList(train));
|
||
|
matcher.train();
|
||
|
}
|
||
13 years ago
|
|
||
13 years ago
|
public void testTrainNoData() {
|
||
|
try {
|
||
|
matcher.train();
|
||
|
fail("Expected CvException - FlannBasedMatcher::train should fail on empty train set");
|
||
|
} catch (CvException cverr) {
|
||
|
// expected
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void testWrite() {
|
||
13 years ago
|
String filename = OpenCVTestRunner.getTempFileName("xml");
|
||
|
|
||
|
matcher.write(filename);
|
||
|
|
||
|
assertEquals(xmlParamsDefault, readFile(filename));
|
||
|
}
|
||
|
|
||
|
public void testWriteYml() {
|
||
13 years ago
|
String filename = OpenCVTestRunner.getTempFileName("yml");
|
||
|
|
||
|
matcher.write(filename);
|
||
|
|
||
13 years ago
|
assertEquals(ymlParamsDefault, readFile(filename));
|
||
13 years ago
|
}
|
||
|
|
||
|
}
|