Path: blob/master/modules/features2d/misc/java/test/SURFFeatureDetectorTest.java
16354 views
package org.opencv.test.features2d;12import java.util.ArrayList;3import java.util.Arrays;4import java.util.Collections;5import java.util.Comparator;6import java.util.List;78import org.opencv.core.CvType;9import org.opencv.core.Mat;10import org.opencv.core.MatOfKeyPoint;11import org.opencv.core.Point;12import org.opencv.core.Scalar;13import org.opencv.core.KeyPoint;14import org.opencv.test.OpenCVTestCase;15import org.opencv.test.OpenCVTestRunner;16import org.opencv.imgproc.Imgproc;17import org.opencv.features2d.Feature2D;1819public class SURFFeatureDetectorTest extends OpenCVTestCase {2021Feature2D detector;22int matSize;23KeyPoint[] truth;2425private Mat getMaskImg() {26Mat mask = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));27Mat right = mask.submat(0, matSize, matSize / 2, matSize);28right.setTo(new Scalar(0));29return mask;30}3132private Mat getTestImg() {33Mat cross = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));34Imgproc.line(cross, new Point(20, matSize / 2), new Point(matSize - 21, matSize / 2), new Scalar(100), 2);35Imgproc.line(cross, new Point(matSize / 2, 20), new Point(matSize / 2, matSize - 21), new Scalar(100), 2);3637return cross;38}3940private void order(List<KeyPoint> points) {41Collections.sort(points, new Comparator<KeyPoint>() {42public int compare(KeyPoint p1, KeyPoint p2) {43if (p1.angle < p2.angle)44return -1;45if (p1.angle > p2.angle)46return 1;47return 0;48}49});50}5152@Override53protected void setUp() throws Exception {54super.setUp();55detector = createClassInstance(XFEATURES2D+"SURF", DEFAULT_FACTORY, null, null);56matSize = 100;57truth = new KeyPoint[] {58new KeyPoint(55.775578f, 55.775578f, 16, 80.245735f, 8617.8633f, 0, -1),59new KeyPoint(44.224422f, 55.775578f, 16, 170.24574f, 8617.8633f, 0, -1),60new KeyPoint(44.224422f, 44.224422f, 16, 260.24573f, 8617.8633f, 0, -1),61new KeyPoint(55.775578f, 44.224422f, 16, 350.24573f, 8617.8633f, 0, -1)62};63}6465public void testCreate() {66assertNotNull(detector);67}6869public void testDetectListOfMatListOfListOfKeyPoint() {7071setProperty(detector, "hessianThreshold", "double", 8000);72setProperty(detector, "nOctaves", "int", 3);73setProperty(detector, "nOctaveLayers", "int", 4);74setProperty(detector, "upright", "boolean", false);7576List<MatOfKeyPoint> keypoints = new ArrayList<MatOfKeyPoint>();77Mat cross = getTestImg();78List<Mat> crosses = new ArrayList<Mat>(3);79crosses.add(cross);80crosses.add(cross);81crosses.add(cross);8283detector.detect(crosses, keypoints);8485assertEquals(3, keypoints.size());8687for (MatOfKeyPoint mkp : keypoints) {88List<KeyPoint> lkp = mkp.toList();89order(lkp);90assertListKeyPointEquals(Arrays.asList(truth), lkp, EPS);91}92}9394public void testDetectListOfMatListOfListOfKeyPointListOfMat() {95fail("Not yet implemented");96}9798public void testDetectMatListOfKeyPoint() {99100setProperty(detector, "hessianThreshold", "double", 8000);101setProperty(detector, "nOctaves", "int", 3);102setProperty(detector, "nOctaveLayers", "int", 4);103setProperty(detector, "upright", "boolean", false);104105MatOfKeyPoint keypoints = new MatOfKeyPoint();106Mat cross = getTestImg();107108detector.detect(cross, keypoints);109110List<KeyPoint> lkp = keypoints.toList();111order(lkp);112assertListKeyPointEquals(Arrays.asList(truth), lkp, EPS);113}114115public void testDetectMatListOfKeyPointMat() {116117setProperty(detector, "hessianThreshold", "double", 8000);118setProperty(detector, "nOctaves", "int", 3);119setProperty(detector, "nOctaveLayers", "int", 4);120setProperty(detector, "upright", "boolean", false);121122Mat img = getTestImg();123Mat mask = getMaskImg();124MatOfKeyPoint keypoints = new MatOfKeyPoint();125126detector.detect(img, keypoints, mask);127128List<KeyPoint> lkp = keypoints.toList();129order(lkp);130assertListKeyPointEquals(Arrays.asList(truth[1], truth[2]), lkp, EPS);131}132133public void testEmpty() {134// assertFalse(detector.empty());135fail("Not yet implemented");136}137138public void testRead() {139Mat cross = getTestImg();140141MatOfKeyPoint keypoints1 = new MatOfKeyPoint();142detector.detect(cross, keypoints1);143144String filename = OpenCVTestRunner.getTempFileName("yml");145writeFile(filename, "%YAML:1.0\n---\nhessianThreshold: 8000.\noctaves: 3\noctaveLayers: 4\nupright: 0\n");146detector.read(filename);147148MatOfKeyPoint keypoints2 = new MatOfKeyPoint();149detector.detect(cross, keypoints2);150151assertTrue(keypoints2.total() <= keypoints1.total());152}153154public void testWrite() {155String filename = OpenCVTestRunner.getTempFileName("xml");156157detector.write(filename);158159// String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n<name>Feature2D.SURF</name>\n<extended>0</extended>\n<hessianThreshold>100.</hessianThreshold>\n<nOctaveLayers>3</nOctaveLayers>\n<nOctaves>4</nOctaves>\n<upright>0</upright>\n</opencv_storage>\n";160String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n</opencv_storage>\n";161assertEquals(truth, readFile(filename));162}163164public void testWriteYml() {165String filename = OpenCVTestRunner.getTempFileName("yml");166167detector.write(filename);168169// String truth = "%YAML:1.0\n---\nname: \"Feature2D.SURF\"\nextended: 0\nhessianThreshold: 100.\nnOctaveLayers: 3\nnOctaves: 4\nupright: 0\n";170String truth = "%YAML:1.0\n---\n";171assertEquals(truth, readFile(filename));172}173174}175176177