1 package org.opencv.test.features2d; 2 3 import java.util.Arrays; 4 import java.util.List; 5 6 import org.opencv.core.Core; 7 import org.opencv.core.CvType; 8 import org.opencv.core.Mat; 9 import org.opencv.core.MatOfDMatch; 10 import org.opencv.core.MatOfKeyPoint; 11 import org.opencv.core.Point; 12 import org.opencv.core.Scalar; 13 import org.opencv.core.DMatch; 14 import org.opencv.features2d.DescriptorExtractor; 15 import org.opencv.features2d.DescriptorMatcher; 16 import org.opencv.features2d.FeatureDetector; 17 import org.opencv.core.KeyPoint; 18 import org.opencv.test.OpenCVTestCase; 19 import org.opencv.test.OpenCVTestRunner; 20 import org.opencv.imgproc.Imgproc; 21 22 public class BruteForceL1DescriptorMatcherTest extends OpenCVTestCase { 23 24 DescriptorMatcher matcher; 25 int matSize; 26 DMatch[] truth; 27 getMaskImg()28 private Mat getMaskImg() { 29 return new Mat(5, 2, CvType.CV_8U, new Scalar(0)) { 30 { 31 put(0, 0, 1, 1, 1, 1); 32 } 33 }; 34 } 35 36 private Mat getQueryDescriptors() { 37 Mat img = getQueryImg(); 38 MatOfKeyPoint keypoints = new MatOfKeyPoint(); 39 Mat descriptors = new Mat(); 40 41 FeatureDetector detector = FeatureDetector.create(FeatureDetector.SURF); 42 DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SURF); 43 44 String filename = OpenCVTestRunner.getTempFileName("yml"); 45 //writeFile(filename, "%YAML:1.0\nhessianThreshold: 8000.\noctaves: 3\noctaveLayers: 4\nupright: 0\n"); 46 writeFile(filename, "%YAML:1.0\nname: \"Feature2D.SURF\"\nextended: 1\nhessianThreshold: 8000.\nnOctaveLayers: 2\nnOctaves: 3\nupright: 0\n"); 47 detector.read(filename); 48 49 detector.detect(img, keypoints); 50 extractor.compute(img, keypoints, descriptors); 51 52 return descriptors; 53 } 54 55 private Mat getQueryImg() { 56 Mat cross = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255)); 57 Imgproc.line(cross, new Point(30, matSize / 2), new Point(matSize - 31, matSize / 2), new Scalar(100), 3); 58 Imgproc.line(cross, new Point(matSize / 2, 30), new Point(matSize / 2, matSize - 31), new Scalar(100), 3); 59 60 return cross; 61 } 62 63 private Mat getTrainDescriptors() { 64 Mat img = getTrainImg(); 65 MatOfKeyPoint keypoints = new MatOfKeyPoint(new KeyPoint(50, 50, 16, 0, 20000, 1, -1), new KeyPoint(42, 42, 16, 160, 10000, 1, -1)); 66 Mat descriptors = new Mat(); 67 68 DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SURF); 69 70 extractor.compute(img, keypoints, descriptors); 71 72 return descriptors; 73 } 74 75 private Mat getTrainImg() { 76 Mat cross = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255)); 77 Imgproc.line(cross, new Point(20, matSize / 2), new Point(matSize - 21, matSize / 2), new Scalar(100), 2); 78 Imgproc.line(cross, new Point(matSize / 2, 20), new Point(matSize / 2, matSize - 21), new Scalar(100), 2); 79 80 return cross; 81 } 82 83 protected void setUp() throws Exception { 84 super.setUp(); 85 matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_L1); 86 matSize = 100; 87 88 truth = new DMatch[] { 89 new DMatch(0, 0, 0, 3.0975165f), 90 new DMatch(1, 1, 0, 3.5680308f), 91 new DMatch(2, 1, 0, 1.3722466f), 92 new DMatch(3, 1, 0, 1.3041023f), 93 new DMatch(4, 1, 0, 3.5970376f) 94 }; 95 } 96 97 public void testAdd() { 98 matcher.add(Arrays.asList(new Mat())); 99 assertFalse(matcher.empty()); 100 } 101 102 public void testClear() { 103 matcher.add(Arrays.asList(new Mat())); 104 105 matcher.clear(); 106 107 assertTrue(matcher.empty()); 108 } 109 110 public void testClone() { 111 Mat train = new Mat(1, 1, CvType.CV_8U, new Scalar(123)); 112 Mat truth = train.clone(); 113 matcher.add(Arrays.asList(train)); 114 115 DescriptorMatcher cloned = matcher.clone(); 116 117 assertNotNull(cloned); 118 119 List<Mat> descriptors = cloned.getTrainDescriptors(); 120 assertEquals(1, descriptors.size()); 121 assertMatEqual(truth, descriptors.get(0)); 122 } 123 124 public void testCloneBoolean() { 125 matcher.add(Arrays.asList(new Mat())); 126 127 DescriptorMatcher cloned = matcher.clone(true); 128 129 assertNotNull(cloned); 130 assertTrue(cloned.empty()); 131 } 132 133 public void testCreate() { 134 assertNotNull(matcher); 135 } 136 137 public void testEmpty() { 138 assertTrue(matcher.empty()); 139 } 140 141 public void testGetTrainDescriptors() { 142 Mat train = new Mat(1, 1, CvType.CV_8U, new Scalar(123)); 143 Mat truth = train.clone(); 144 matcher.add(Arrays.asList(train)); 145 146 List<Mat> descriptors = matcher.getTrainDescriptors(); 147 148 assertEquals(1, descriptors.size()); 149 assertMatEqual(truth, descriptors.get(0)); 150 } 151 152 public void testIsMaskSupported() { 153 assertTrue(matcher.isMaskSupported()); 154 } 155 156 public void testKnnMatchMatListOfListOfDMatchInt() { 157 fail("Not yet implemented"); 158 } 159 160 public void testKnnMatchMatListOfListOfDMatchIntListOfMat() { 161 fail("Not yet implemented"); 162 } 163 164 public void testKnnMatchMatListOfListOfDMatchIntListOfMatBoolean() { 165 fail("Not yet implemented"); 166 } 167 168 public void testKnnMatchMatMatListOfListOfDMatchInt() { 169 fail("Not yet implemented"); 170 } 171 172 public void testKnnMatchMatMatListOfListOfDMatchIntMat() { 173 fail("Not yet implemented"); 174 } 175 176 public void testKnnMatchMatMatListOfListOfDMatchIntMatBoolean() { 177 fail("Not yet implemented"); 178 } 179 180 public void testMatchMatListOfDMatch() { 181 Mat train = getTrainDescriptors(); 182 Mat query = getQueryDescriptors(); 183 MatOfDMatch matches = new MatOfDMatch(); 184 matcher.add(Arrays.asList(train)); 185 186 matcher.match(query, matches); 187 188 assertArrayDMatchEquals(truth, matches.toArray(), EPS); 189 } 190 191 public void testMatchMatListOfDMatchListOfMat() { 192 Mat train = getTrainDescriptors(); 193 Mat query = getQueryDescriptors(); 194 Mat mask = getMaskImg(); 195 MatOfDMatch matches = new MatOfDMatch(); 196 matcher.add(Arrays.asList(train)); 197 198 matcher.match(query, matches, Arrays.asList(mask)); 199 200 assertListDMatchEquals(Arrays.asList(truth[0], truth[1]), matches.toList(), EPS); 201 } 202 203 public void testMatchMatMatListOfDMatch() { 204 Mat train = getTrainDescriptors(); 205 Mat query = getQueryDescriptors(); 206 MatOfDMatch matches = new MatOfDMatch(); 207 208 matcher.match(query, train, matches); 209 210 assertArrayDMatchEquals(truth, matches.toArray(), EPS); 211 } 212 213 public void testMatchMatMatListOfDMatchMat() { 214 Mat train = getTrainDescriptors(); 215 Mat query = getQueryDescriptors(); 216 Mat mask = getMaskImg(); 217 MatOfDMatch matches = new MatOfDMatch(); 218 219 matcher.match(query, train, matches, mask); 220 221 assertListDMatchEquals(Arrays.asList(truth[0], truth[1]), matches.toList(), EPS); 222 } 223 224 public void testRadiusMatchMatListOfListOfDMatchFloat() { 225 fail("Not yet implemented"); 226 } 227 228 public void testRadiusMatchMatListOfListOfDMatchFloatListOfMat() { 229 fail("Not yet implemented"); 230 } 231 232 public void testRadiusMatchMatListOfListOfDMatchFloatListOfMatBoolean() { 233 fail("Not yet implemented"); 234 } 235 236 public void testRadiusMatchMatMatListOfListOfDMatchFloat() { 237 fail("Not yet implemented"); 238 } 239 240 public void testRadiusMatchMatMatListOfListOfDMatchFloatMat() { 241 fail("Not yet implemented"); 242 } 243 244 public void testRadiusMatchMatMatListOfListOfDMatchFloatMatBoolean() { 245 fail("Not yet implemented"); 246 } 247 248 public void testRead() { 249 String filename = OpenCVTestRunner.getTempFileName("yml"); 250 writeFile(filename, "%YAML:1.0\n"); 251 252 matcher.read(filename); 253 assertTrue(true);// BruteforceMatcher has no settings 254 } 255 256 public void testTrain() { 257 matcher.train();// BruteforceMatcher does not need to train 258 } 259 260 public void testWrite() { 261 String filename = OpenCVTestRunner.getTempFileName("yml"); 262 263 matcher.write(filename); 264 265 String truth = "%YAML:1.0\n"; 266 assertEquals(truth, readFile(filename)); 267 } 268 269 } 270