Path: blob/master/modules/features2d/misc/java/test/FASTFeatureDetectorTest.java
16354 views
package org.opencv.test.features2d;12import java.util.Arrays;34import org.opencv.core.Core;5import org.opencv.core.CvType;6import org.opencv.core.Mat;7import org.opencv.core.MatOfKeyPoint;8import org.opencv.core.Point;9import org.opencv.core.Scalar;10import org.opencv.features2d.Feature2D;11import org.opencv.features2d.FastFeatureDetector;12import org.opencv.core.KeyPoint;13import org.opencv.test.OpenCVTestCase;14import org.opencv.test.OpenCVTestRunner;15import org.opencv.imgproc.Imgproc;1617public class FASTFeatureDetectorTest extends OpenCVTestCase {1819Feature2D detector;20KeyPoint[] truth;2122private Mat getMaskImg() {23Mat mask = new Mat(100, 100, CvType.CV_8U, new Scalar(255));24Mat right = mask.submat(0, 100, 50, 100);25right.setTo(new Scalar(0));26return mask;27}2829private Mat getTestImg() {30Mat img = new Mat(100, 100, CvType.CV_8U, new Scalar(255));31Imgproc.line(img, new Point(30, 30), new Point(70, 70), new Scalar(0), 8);32return img;33}3435@Override36protected void setUp() throws Exception {37super.setUp();38detector = FastFeatureDetector.create();39truth = new KeyPoint[] { new KeyPoint(32, 27, 7, -1, 254, 0, -1), new KeyPoint(27, 32, 7, -1, 254, 0, -1), new KeyPoint(73, 68, 7, -1, 254, 0, -1),40new KeyPoint(68, 73, 7, -1, 254, 0, -1) };41}4243public void testCreate() {44assertNotNull(detector);45}4647public void testDetectListOfMatListOfListOfKeyPoint() {48fail("Not yet implemented");49}5051public void testDetectListOfMatListOfListOfKeyPointListOfMat() {52fail("Not yet implemented");53}5455public void testDetectMatListOfKeyPoint() {56Mat img = getTestImg();57MatOfKeyPoint keypoints = new MatOfKeyPoint();5859detector.detect(img, keypoints);6061assertListKeyPointEquals(Arrays.asList(truth), keypoints.toList(), EPS);6263// OpenCVTestRunner.Log("points found: " + keypoints.size());64// for (KeyPoint kp : keypoints)65// OpenCVTestRunner.Log(kp.toString());66}6768public void testDetectMatListOfKeyPointMat() {69Mat img = getTestImg();70Mat mask = getMaskImg();71MatOfKeyPoint keypoints = new MatOfKeyPoint();7273detector.detect(img, keypoints, mask);7475assertListKeyPointEquals(Arrays.asList(truth[0], truth[1]), keypoints.toList(), EPS);76}7778public void testEmpty() {79// assertFalse(detector.empty());80fail("Not yet implemented"); //FAST does not override empty() method81}8283public void testRead() {84String filename = OpenCVTestRunner.getTempFileName("yml");8586writeFile(filename, "%YAML:1.0\n---\nthreshold: 130\nnonmaxSuppression: 1\n");87detector.read(filename);8889MatOfKeyPoint keypoints1 = new MatOfKeyPoint();9091detector.detect(grayChess, keypoints1);9293writeFile(filename, "%YAML:1.0\n---\nthreshold: 150\nnonmaxSuppression: 1\n");94detector.read(filename);9596MatOfKeyPoint keypoints2 = new MatOfKeyPoint();9798detector.detect(grayChess, keypoints2);99100assertTrue(keypoints2.total() <= keypoints1.total());101}102103public void testReadYml() {104String filename = OpenCVTestRunner.getTempFileName("yml");105106writeFile(filename,107"<?xml version=\"1.0\"?>\n<opencv_storage>\n<threshold>130</threshold>\n<nonmaxSuppression>1</nonmaxSuppression>\n</opencv_storage>\n");108detector.read(filename);109110MatOfKeyPoint keypoints1 = new MatOfKeyPoint();111112detector.detect(grayChess, keypoints1);113114writeFile(filename,115"<?xml version=\"1.0\"?>\n<opencv_storage>\n<threshold>150</threshold>\n<nonmaxSuppression>1</nonmaxSuppression>\n</opencv_storage>\n");116detector.read(filename);117118MatOfKeyPoint keypoints2 = new MatOfKeyPoint();119120detector.detect(grayChess, keypoints2);121122assertTrue(keypoints2.total() <= keypoints1.total());123}124125public void testWrite() {126String filename = OpenCVTestRunner.getTempFileName("xml");127128detector.write(filename);129130// String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n<name>Feature2D.FAST</name>\n<nonmaxSuppression>1</nonmaxSuppression>\n<threshold>10</threshold>\n<type>2</type>\n</opencv_storage>\n";131String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n</opencv_storage>\n";132String data = readFile(filename);133//Log.d("qqq", "\"" + data + "\"");134assertEquals(truth, data);135}136137public void testWriteYml() {138String filename = OpenCVTestRunner.getTempFileName("yml");139140detector.write(filename);141142// String truth = "%YAML:1.0\n---\nname: \"Feature2D.FAST\"\nnonmaxSuppression: 1\nthreshold: 10\ntype: 2\n";143String truth = "%YAML:1.0\n---\n";144String data = readFile(filename);145146//Log.d("qqq", "\"" + data + "\"");147assertEquals(truth, data);148}149}150151152