1 package org.opencv.test.features2d; 2 3 import java.util.Arrays; 4 5 import org.opencv.core.Core; 6 import org.opencv.core.CvType; 7 import org.opencv.core.Mat; 8 import org.opencv.core.MatOfKeyPoint; 9 import org.opencv.core.Point; 10 import org.opencv.core.Scalar; 11 import org.opencv.features2d.FeatureDetector; 12 import org.opencv.core.KeyPoint; 13 import org.opencv.test.OpenCVTestCase; 14 import org.opencv.test.OpenCVTestRunner; 15 import org.opencv.imgproc.Imgproc; 16 17 public class FASTFeatureDetectorTest extends OpenCVTestCase { 18 19 FeatureDetector detector; 20 KeyPoint[] truth; 21 22 private Mat getMaskImg() { 23 Mat mask = new Mat(100, 100, CvType.CV_8U, new Scalar(255)); 24 Mat right = mask.submat(0, 100, 50, 100); 25 right.setTo(new Scalar(0)); 26 return mask; 27 } 28 29 private Mat getTestImg() { 30 Mat img = new Mat(100, 100, CvType.CV_8U, new Scalar(255)); 31 Imgproc.line(img, new Point(30, 30), new Point(70, 70), new Scalar(0), 8); 32 return img; 33 } 34 35 @Override 36 protected void setUp() throws Exception { 37 super.setUp(); 38 detector = FeatureDetector.create(FeatureDetector.FAST); 39 truth = 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), 40 new KeyPoint(68, 73, 7, -1, 254, 0, -1) }; 41 } 42 43 public void testCreate() { 44 assertNotNull(detector); 45 } 46 47 public void testDetectListOfMatListOfListOfKeyPoint() { 48 fail("Not yet implemented"); 49 } 50 51 public void testDetectListOfMatListOfListOfKeyPointListOfMat() { 52 fail("Not yet implemented"); 53 } 54 55 public void testDetectMatListOfKeyPoint() { 56 Mat img = getTestImg(); 57 MatOfKeyPoint keypoints = new MatOfKeyPoint(); 58 59 detector.detect(img, keypoints); 60 61 assertListKeyPointEquals(Arrays.asList(truth), keypoints.toList(), EPS); 62 63 // OpenCVTestRunner.Log("points found: " + keypoints.size()); 64 // for (KeyPoint kp : keypoints) 65 // OpenCVTestRunner.Log(kp.toString()); 66 } 67 68 public void testDetectMatListOfKeyPointMat() { 69 Mat img = getTestImg(); 70 Mat mask = getMaskImg(); 71 MatOfKeyPoint keypoints = new MatOfKeyPoint(); 72 73 detector.detect(img, keypoints, mask); 74 75 assertListKeyPointEquals(Arrays.asList(truth[0], truth[1]), keypoints.toList(), EPS); 76 } 77 78 public void testEmpty() { 79 assertFalse(detector.empty()); 80 } 81 82 public void testRead() { 83 String filename = OpenCVTestRunner.getTempFileName("yml"); 84 85 writeFile(filename, "%YAML:1.0\nthreshold: 130\nnonmaxSuppression: 1\n"); 86 detector.read(filename); 87 88 MatOfKeyPoint keypoints1 = new MatOfKeyPoint(); 89 90 detector.detect(grayChess, keypoints1); 91 92 writeFile(filename, "%YAML:1.0\nthreshold: 150\nnonmaxSuppression: 1\n"); 93 detector.read(filename); 94 95 MatOfKeyPoint keypoints2 = new MatOfKeyPoint(); 96 97 detector.detect(grayChess, keypoints2); 98 99 assertTrue(keypoints2.total() <= keypoints1.total()); 100 } 101 102 public void testReadYml() { 103 String filename = OpenCVTestRunner.getTempFileName("yml"); 104 105 writeFile(filename, 106 "<?xml version=\"1.0\"?>\n<opencv_storage>\n<threshold>130</threshold>\n<nonmaxSuppression>1</nonmaxSuppression>\n</opencv_storage>\n"); 107 detector.read(filename); 108 109 MatOfKeyPoint keypoints1 = new MatOfKeyPoint(); 110 111 detector.detect(grayChess, keypoints1); 112 113 writeFile(filename, 114 "<?xml version=\"1.0\"?>\n<opencv_storage>\n<threshold>150</threshold>\n<nonmaxSuppression>1</nonmaxSuppression>\n</opencv_storage>\n"); 115 detector.read(filename); 116 117 MatOfKeyPoint keypoints2 = new MatOfKeyPoint(); 118 119 detector.detect(grayChess, keypoints2); 120 121 assertTrue(keypoints2.total() <= keypoints1.total()); 122 } 123 124 public void testWrite() { 125 String filename = OpenCVTestRunner.getTempFileName("xml"); 126 127 detector.write(filename); 128 129 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"; 130 String data = readFile(filename); 131 //Log.d("qqq", "\"" + data + "\""); 132 assertEquals(truth, data); 133 } 134 135 public void testWriteYml() { 136 String filename = OpenCVTestRunner.getTempFileName("yml"); 137 138 detector.write(filename); 139 140 String truth = "%YAML:1.0\nname: \"Feature2D.FAST\"\nnonmaxSuppression: 1\nthreshold: 10\ntype: 2\n"; 141 String data = readFile(filename); 142 143 //Log.d("qqq", "\"" + data + "\""); 144 assertEquals(truth, data); 145 } 146 } 147