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