2011-08-04 14:21:17 +08:00
|
|
|
package org.opencv.test.features2d;
|
|
|
|
|
|
|
|
import org.opencv.core.Core;
|
|
|
|
import org.opencv.core.CvType;
|
|
|
|
import org.opencv.core.Mat;
|
2012-04-09 16:20:23 +08:00
|
|
|
import org.opencv.core.MatOfKeyPoint;
|
2011-08-04 14:21:17 +08:00
|
|
|
import org.opencv.core.Point;
|
|
|
|
import org.opencv.core.Scalar;
|
2013-03-26 18:58:09 +08:00
|
|
|
import org.opencv.core.KeyPoint;
|
2016-10-18 01:53:59 +08:00
|
|
|
import org.opencv.features2d.ORB;
|
2011-08-04 14:21:17 +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;
|
2011-08-04 14:21:17 +08:00
|
|
|
|
|
|
|
public class ORBDescriptorExtractorTest extends OpenCVTestCase {
|
|
|
|
|
2016-10-18 01:53:59 +08:00
|
|
|
ORB extractor;
|
2011-08-04 14:21:17 +08:00
|
|
|
int matSize;
|
|
|
|
|
2012-04-24 14:51:21 +08:00
|
|
|
public static void assertDescriptorsClose(Mat expected, Mat actual, int allowedDistance) {
|
|
|
|
double distance = Core.norm(expected, actual, Core.NORM_HAMMING);
|
|
|
|
assertTrue("expected:<" + allowedDistance + "> but was:<" + distance + ">", distance <= allowedDistance);
|
|
|
|
}
|
|
|
|
|
2011-08-04 14:21:17 +08:00
|
|
|
private Mat getTestImg() {
|
|
|
|
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-04 14:21:17 +08:00
|
|
|
|
|
|
|
return cross;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void setUp() throws Exception {
|
2012-12-28 21:03:35 +08:00
|
|
|
super.setUp();
|
2016-10-18 01:53:59 +08:00
|
|
|
extractor = ORB.create();
|
2011-08-04 14:21:17 +08:00
|
|
|
matSize = 100;
|
|
|
|
}
|
|
|
|
|
2011-08-05 16:39:28 +08:00
|
|
|
public void testComputeListOfMatListOfListOfKeyPointListOfMat() {
|
|
|
|
fail("Not yet implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testComputeMatListOfKeyPointMat() {
|
2011-08-04 14:21:17 +08:00
|
|
|
KeyPoint point = new KeyPoint(55.775577545166016f, 44.224422454833984f, 16, 9.754629f, 8617.863f, 1, -1);
|
2012-04-09 16:20:23 +08:00
|
|
|
MatOfKeyPoint keypoints = new MatOfKeyPoint(point);
|
2011-08-04 14:21:17 +08:00
|
|
|
Mat img = getTestImg();
|
|
|
|
Mat descriptors = new Mat();
|
|
|
|
|
|
|
|
extractor.compute(img, keypoints, descriptors);
|
|
|
|
|
|
|
|
Mat truth = new Mat(1, 32, CvType.CV_8UC1) {
|
|
|
|
{
|
2012-04-11 03:17:44 +08:00
|
|
|
put(0, 0,
|
2019-11-18 06:39:27 +08:00
|
|
|
6, 74, 6, 129, 2, 130, 56, 0, 44, 132, 66, 165, 172, 6, 3, 72, 102, 61, 171, 214, 0, 144, 65, 232, 4, 32, 138, 131, 4, 21, 37, 217);
|
2011-08-04 14:21:17 +08:00
|
|
|
}
|
|
|
|
};
|
2012-04-24 14:51:21 +08:00
|
|
|
assertDescriptorsClose(truth, descriptors, 1);
|
2011-08-04 14:21:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testCreate() {
|
|
|
|
assertNotNull(extractor);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testDescriptorSize() {
|
|
|
|
assertEquals(32, extractor.descriptorSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testDescriptorType() {
|
|
|
|
assertEquals(CvType.CV_8U, extractor.descriptorType());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testEmpty() {
|
2016-10-18 01:53:59 +08:00
|
|
|
// assertFalse(extractor.empty());
|
|
|
|
fail("Not yet implemented"); // ORB does not override empty() method
|
2011-08-04 14:21:17 +08:00
|
|
|
}
|
|
|
|
|
2022-12-21 21:03:00 +08:00
|
|
|
public void testReadYml() {
|
2011-08-04 14:21:17 +08:00
|
|
|
KeyPoint point = new KeyPoint(55.775577545166016f, 44.224422454833984f, 16, 9.754629f, 8617.863f, 1, -1);
|
2012-04-09 16:20:23 +08:00
|
|
|
MatOfKeyPoint keypoints = new MatOfKeyPoint(point);
|
2011-08-04 14:21:17 +08:00
|
|
|
Mat img = getTestImg();
|
|
|
|
Mat descriptors = new Mat();
|
2011-08-05 16:39:28 +08:00
|
|
|
|
2022-12-21 21:03:00 +08:00
|
|
|
String filename = OpenCVTestRunner.getTempFileName("yml");
|
|
|
|
writeFile(filename, "%YAML:1.0\n---\nnfeatures: 500\nscaleFactor: 1.1\nnlevels: 3\nedgeThreshold: 31\nfirstLevel: 0\nwta_k: 2\nscoreType: 0\npatchSize: 31\nfastThreshold: 20\n");
|
|
|
|
extractor.read(filename);
|
|
|
|
|
|
|
|
assertEquals(500, extractor.getMaxFeatures());
|
|
|
|
assertEquals(1.1, extractor.getScaleFactor());
|
|
|
|
assertEquals(3, extractor.getNLevels());
|
|
|
|
assertEquals(31, extractor.getEdgeThreshold());
|
|
|
|
assertEquals(0, extractor.getFirstLevel());
|
|
|
|
assertEquals(2, extractor.getWTA_K());
|
|
|
|
assertEquals(0, extractor.getScoreType());
|
|
|
|
assertEquals(31, extractor.getPatchSize());
|
|
|
|
assertEquals(20, extractor.getFastThreshold());
|
2011-08-05 16:39:28 +08:00
|
|
|
|
2011-08-04 14:21:17 +08:00
|
|
|
extractor.compute(img, keypoints, descriptors);
|
|
|
|
|
|
|
|
Mat truth = new Mat(1, 32, CvType.CV_8UC1) {
|
|
|
|
{
|
2012-04-11 03:17:44 +08:00
|
|
|
put(0, 0,
|
2019-11-18 06:39:27 +08:00
|
|
|
6, 10, 22, 5, 2, 130, 56, 0, 44, 164, 66, 165, 140, 6, 1, 72, 38, 61, 163, 210, 0, 208, 1, 104, 4, 32, 74, 131, 0, 37, 37, 67);
|
2011-08-04 14:21:17 +08:00
|
|
|
}
|
|
|
|
};
|
2012-04-24 14:51:21 +08:00
|
|
|
assertDescriptorsClose(truth, descriptors, 1);
|
2011-08-04 14:21:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testWriteYml() {
|
|
|
|
String filename = OpenCVTestRunner.getTempFileName("yml");
|
|
|
|
|
|
|
|
extractor.write(filename);
|
|
|
|
|
2022-12-21 21:03:00 +08:00
|
|
|
String truth = "%YAML:1.0\n---\nname: \"Feature2D.ORB\"\nnfeatures: 500\nscaleFactor: 1.2000000476837158e+00\nnlevels: 8\nedgeThreshold: 31\nfirstLevel: 0\nwta_k: 2\nscoreType: 0\npatchSize: 31\nfastThreshold: 20\n";
|
|
|
|
// String truth = "%YAML:1.0\n---\n";
|
2013-01-11 13:13:25 +08:00
|
|
|
String actual = readFile(filename);
|
|
|
|
actual = actual.replaceAll("e\\+000", "e+00"); // NOTE: workaround for different platforms double representation
|
|
|
|
assertEquals(truth, actual);
|
2011-08-04 14:21:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|