Path: blob/master/modules/features2d/misc/java/test/STARFeatureDetectorTest.java
16354 views
package org.opencv.test.features2d;12import java.util.Arrays;34import org.opencv.core.CvType;5import org.opencv.core.Mat;6import org.opencv.core.MatOfKeyPoint;7import org.opencv.core.Point;8import org.opencv.core.Scalar;9import org.opencv.core.KeyPoint;10import org.opencv.test.OpenCVTestCase;11import org.opencv.test.OpenCVTestRunner;12import org.opencv.imgproc.Imgproc;13import org.opencv.features2d.Feature2D;1415public class STARFeatureDetectorTest extends OpenCVTestCase {1617Feature2D detector;18int matSize;19KeyPoint[] truth;2021private Mat getMaskImg() {22Mat mask = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));23Mat right = mask.submat(0, matSize, matSize / 2, matSize);24right.setTo(new Scalar(0));25return mask;26}2728private Mat getTestImg() {29Scalar color = new Scalar(0);30int center = matSize / 2;31int radius = 6;32int offset = 40;3334Mat img = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));35Imgproc.circle(img, new Point(center - offset, center), radius, color, -1);36Imgproc.circle(img, new Point(center + offset, center), radius, color, -1);37Imgproc.circle(img, new Point(center, center - offset), radius, color, -1);38Imgproc.circle(img, new Point(center, center + offset), radius, color, -1);39Imgproc.circle(img, new Point(center, center), radius, color, -1);40return img;41}4243protected void setUp() throws Exception {44super.setUp();45detector = createClassInstance(XFEATURES2D+"StarDetector", DEFAULT_FACTORY, null, null);46matSize = 200;47truth = new KeyPoint[] {48new KeyPoint( 95, 80, 22, -1, 31.5957f, 0, -1),49new KeyPoint(105, 80, 22, -1, 31.5957f, 0, -1),50new KeyPoint( 80, 95, 22, -1, 31.5957f, 0, -1),51new KeyPoint(120, 95, 22, -1, 31.5957f, 0, -1),52new KeyPoint(100, 100, 8, -1, 30.f, 0, -1),53new KeyPoint( 80, 105, 22, -1, 31.5957f, 0, -1),54new KeyPoint(120, 105, 22, -1, 31.5957f, 0, -1),55new KeyPoint( 95, 120, 22, -1, 31.5957f, 0, -1),56new KeyPoint(105, 120, 22, -1, 31.5957f, 0, -1)57};58}5960public void testCreate() {61assertNotNull(detector);62}6364public void testDetectListOfMatListOfListOfKeyPoint() {65fail("Not yet implemented");66}6768public void testDetectListOfMatListOfListOfKeyPointListOfMat() {69fail("Not yet implemented");70}7172public void testDetectMatListOfKeyPoint() {73Mat img = getTestImg();74MatOfKeyPoint keypoints = new MatOfKeyPoint();7576detector.detect(img, keypoints);7778assertListKeyPointEquals(Arrays.asList(truth), keypoints.toList(), EPS);79}8081public void testDetectMatListOfKeyPointMat() {82Mat img = getTestImg();83Mat mask = getMaskImg();84MatOfKeyPoint keypoints = new MatOfKeyPoint();8586detector.detect(img, keypoints, mask);8788assertListKeyPointEquals(Arrays.asList(truth[0], truth[2], truth[5], truth[7]), keypoints.toList(), EPS);89}9091public void testEmpty() {92// assertFalse(detector.empty());93fail("Not yet implemented");94}9596public void testRead() {97Mat img = getTestImg();9899MatOfKeyPoint keypoints1 = new MatOfKeyPoint();100detector.detect(img, keypoints1);101102String filename = OpenCVTestRunner.getTempFileName("yml");103writeFile(filename, "%YAML:1.0\n---\nmaxSize: 45\nresponseThreshold: 150\nlineThresholdProjected: 10\nlineThresholdBinarized: 8\nsuppressNonmaxSize: 5\n");104detector.read(filename);105106MatOfKeyPoint keypoints2 = new MatOfKeyPoint();107detector.detect(img, keypoints2);108109assertTrue(keypoints2.total() <= keypoints1.total());110}111112public void testWrite() {113String filename = OpenCVTestRunner.getTempFileName("xml");114115detector.write(filename);116117// String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n<name>Feature2D.STAR</name>\n<lineThresholdBinarized>8</lineThresholdBinarized>\n<lineThresholdProjected>10</lineThresholdProjected>\n<maxSize>45</maxSize>\n<responseThreshold>30</responseThreshold>\n<suppressNonmaxSize>5</suppressNonmaxSize>\n</opencv_storage>\n";118String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n</opencv_storage>\n";119assertEquals(truth, readFile(filename));120}121122public void testWriteYml() {123String filename = OpenCVTestRunner.getTempFileName("yml");124125detector.write(filename);126127// String truth = "%YAML:1.0\n---\nname: \"Feature2D.STAR\"\nlineThresholdBinarized: 8\nlineThresholdProjected: 10\nmaxSize: 45\nresponseThreshold: 30\nsuppressNonmaxSize: 5\n";128String truth = "%YAML:1.0\n---\n";129assertEquals(truth, readFile(filename));130}131132}133134135