1 package org.opencv.test.features2d; 2 3 import java.util.ArrayList; 4 import java.util.Arrays; 5 import java.util.Collections; 6 import java.util.Comparator; 7 import java.util.List; 8 9 import org.opencv.core.Core; 10 import org.opencv.core.CvType; 11 import org.opencv.core.Mat; 12 import org.opencv.core.MatOfKeyPoint; 13 import org.opencv.core.Point; 14 import org.opencv.core.Scalar; 15 import org.opencv.features2d.FeatureDetector; 16 import org.opencv.core.KeyPoint; 17 import org.opencv.test.OpenCVTestCase; 18 import org.opencv.test.OpenCVTestRunner; 19 import org.opencv.imgproc.Imgproc; 20 21 public class SURFFeatureDetectorTest extends OpenCVTestCase { 22 23 FeatureDetector detector; 24 int matSize; 25 KeyPoint[] truth; 26 getMaskImg()27 private Mat getMaskImg() { 28 Mat mask = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255)); 29 Mat right = mask.submat(0, matSize, matSize / 2, matSize); 30 right.setTo(new Scalar(0)); 31 return mask; 32 } 33 getTestImg()34 private Mat getTestImg() { 35 Mat cross = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255)); 36 Imgproc.line(cross, new Point(20, matSize / 2), new Point(matSize - 21, matSize / 2), new Scalar(100), 2); 37 Imgproc.line(cross, new Point(matSize / 2, 20), new Point(matSize / 2, matSize - 21), new Scalar(100), 2); 38 39 return cross; 40 } 41 order(List<KeyPoint> points)42 private void order(List<KeyPoint> points) { 43 Collections.sort(points, new Comparator<KeyPoint>() { 44 public int compare(KeyPoint p1, KeyPoint p2) { 45 if (p1.angle < p2.angle) 46 return -1; 47 if (p1.angle > p2.angle) 48 return 1; 49 return 0; 50 } 51 }); 52 } 53 54 @Override setUp()55 protected void setUp() throws Exception { 56 super.setUp(); 57 detector = FeatureDetector.create(FeatureDetector.SURF); 58 matSize = 100; 59 truth = new KeyPoint[] { 60 new KeyPoint(55.775578f, 55.775578f, 16, 80.245735f, 8617.8633f, 0, -1), 61 new KeyPoint(44.224422f, 55.775578f, 16, 170.24574f, 8617.8633f, 0, -1), 62 new KeyPoint(44.224422f, 44.224422f, 16, 260.24573f, 8617.8633f, 0, -1), 63 new KeyPoint(55.775578f, 44.224422f, 16, 350.24573f, 8617.8633f, 0, -1) 64 }; 65 } 66 testCreate()67 public void testCreate() { 68 assertNotNull(detector); 69 } 70 testDetectListOfMatListOfListOfKeyPoint()71 public void testDetectListOfMatListOfListOfKeyPoint() { 72 String filename = OpenCVTestRunner.getTempFileName("yml"); 73 writeFile(filename, "%YAML:1.0\nhessianThreshold: 8000.\noctaves: 3\noctaveLayers: 4\nupright: 0\n"); 74 detector.read(filename); 75 76 List<MatOfKeyPoint> keypoints = new ArrayList<MatOfKeyPoint>(); 77 Mat cross = getTestImg(); 78 List<Mat> crosses = new ArrayList<Mat>(3); 79 crosses.add(cross); 80 crosses.add(cross); 81 crosses.add(cross); 82 83 detector.detect(crosses, keypoints); 84 85 assertEquals(3, keypoints.size()); 86 87 for (MatOfKeyPoint mkp : keypoints) { 88 List<KeyPoint> lkp = mkp.toList(); 89 order(lkp); 90 assertListKeyPointEquals(Arrays.asList(truth), lkp, EPS); 91 } 92 } 93 testDetectListOfMatListOfListOfKeyPointListOfMat()94 public void testDetectListOfMatListOfListOfKeyPointListOfMat() { 95 fail("Not yet implemented"); 96 } 97 testDetectMatListOfKeyPoint()98 public void testDetectMatListOfKeyPoint() { 99 String filename = OpenCVTestRunner.getTempFileName("yml"); 100 writeFile(filename, "%YAML:1.0\nhessianThreshold: 8000.\noctaves: 3\noctaveLayers: 4\nupright: 0\n"); 101 detector.read(filename); 102 103 MatOfKeyPoint keypoints = new MatOfKeyPoint(); 104 Mat cross = getTestImg(); 105 106 detector.detect(cross, keypoints); 107 108 List<KeyPoint> lkp = keypoints.toList(); 109 order(lkp); 110 assertListKeyPointEquals(Arrays.asList(truth), lkp, EPS); 111 } 112 testDetectMatListOfKeyPointMat()113 public void testDetectMatListOfKeyPointMat() { 114 String filename = OpenCVTestRunner.getTempFileName("yml"); 115 writeFile(filename, "%YAML:1.0\nhessianThreshold: 8000.\noctaves: 3\noctaveLayers: 4\nupright: 0\n"); 116 detector.read(filename); 117 118 Mat img = getTestImg(); 119 Mat mask = getMaskImg(); 120 MatOfKeyPoint keypoints = new MatOfKeyPoint(); 121 122 detector.detect(img, keypoints, mask); 123 124 List<KeyPoint> lkp = keypoints.toList(); 125 order(lkp); 126 assertListKeyPointEquals(Arrays.asList(truth[1], truth[2]), lkp, EPS); 127 } 128 testEmpty()129 public void testEmpty() { 130 assertFalse(detector.empty()); 131 } 132 testRead()133 public void testRead() { 134 Mat cross = getTestImg(); 135 136 MatOfKeyPoint keypoints1 = new MatOfKeyPoint(); 137 detector.detect(cross, keypoints1); 138 139 String filename = OpenCVTestRunner.getTempFileName("yml"); 140 writeFile(filename, "%YAML:1.0\nhessianThreshold: 8000.\noctaves: 3\noctaveLayers: 4\nupright: 0\n"); 141 detector.read(filename); 142 143 MatOfKeyPoint keypoints2 = new MatOfKeyPoint(); 144 detector.detect(cross, keypoints2); 145 146 assertTrue(keypoints2.total() <= keypoints1.total()); 147 } 148 testWrite()149 public void testWrite() { 150 String filename = OpenCVTestRunner.getTempFileName("xml"); 151 152 detector.write(filename); 153 154 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"; 155 assertEquals(truth, readFile(filename)); 156 } 157 testWriteYml()158 public void testWriteYml() { 159 String filename = OpenCVTestRunner.getTempFileName("yml"); 160 161 detector.write(filename); 162 163 String truth = "%YAML:1.0\nname: \"Feature2D.SURF\"\nextended: 0\nhessianThreshold: 100.\nnOctaveLayers: 3\nnOctaves: 4\nupright: 0\n"; 164 assertEquals(truth, readFile(filename)); 165 } 166 167 } 168