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