Path: blob/master/modules/features2d/misc/java/test/FlannBasedDescriptorMatcherTest.java
16354 views
package org.opencv.test.features2d;12import java.util.Arrays;3import java.util.List;45import org.opencv.core.CvException;6import org.opencv.core.CvType;7import org.opencv.core.Mat;8import org.opencv.core.MatOfDMatch;9import org.opencv.core.MatOfKeyPoint;10import org.opencv.core.Point;11import org.opencv.core.Scalar;12import org.opencv.core.DMatch;13import org.opencv.features2d.DescriptorMatcher;14import org.opencv.core.KeyPoint;15import org.opencv.test.OpenCVTestCase;16import org.opencv.test.OpenCVTestRunner;17import org.opencv.imgproc.Imgproc;18import org.opencv.features2d.Feature2D;1920public class FlannBasedDescriptorMatcherTest extends OpenCVTestCase {2122static final String xmlParamsDefault = "<?xml version=\"1.0\"?>\n"23+ "<opencv_storage>\n"24+ "<format>3</format>\n"25+ "<indexParams>\n"26+ " <_>\n"27+ " <name>algorithm</name>\n"28+ " <type>9</type>\n" // FLANN_INDEX_TYPE_ALGORITHM29+ " <value>1</value></_>\n"30+ " <_>\n"31+ " <name>trees</name>\n"32+ " <type>4</type>\n"33+ " <value>4</value></_></indexParams>\n"34+ "<searchParams>\n"35+ " <_>\n"36+ " <name>checks</name>\n"37+ " <type>4</type>\n"38+ " <value>32</value></_>\n"39+ " <_>\n"40+ " <name>eps</name>\n"41+ " <type>5</type>\n"42+ " <value>0.</value></_>\n"43+ " <_>\n"44+ " <name>sorted</name>\n"45+ " <type>8</type>\n" // FLANN_INDEX_TYPE_BOOL46+ " <value>1</value></_></searchParams>\n"47+ "</opencv_storage>\n";48static final String ymlParamsDefault = "%YAML:1.0\n---\n"49+ "format: 3\n"50+ "indexParams:\n"51+ " -\n"52+ " name: algorithm\n"53+ " type: 9\n" // FLANN_INDEX_TYPE_ALGORITHM54+ " value: 1\n"55+ " -\n"56+ " name: trees\n"57+ " type: 4\n"58+ " value: 4\n"59+ "searchParams:\n"60+ " -\n"61+ " name: checks\n"62+ " type: 4\n"63+ " value: 32\n"64+ " -\n"65+ " name: eps\n"66+ " type: 5\n"67+ " value: 0.\n"68+ " -\n"69+ " name: sorted\n"70+ " type: 8\n" // FLANN_INDEX_TYPE_BOOL71+ " value: 1\n";72static final String ymlParamsModified = "%YAML:1.0\n---\n"73+ "format: 3\n"74+ "indexParams:\n"75+ " -\n"76+ " name: algorithm\n"77+ " type: 9\n" // FLANN_INDEX_TYPE_ALGORITHM78+ " value: 6\n"// this line is changed!79+ " -\n"80+ " name: trees\n"81+ " type: 4\n"82+ " value: 4\n"83+ "searchParams:\n"84+ " -\n"85+ " name: checks\n"86+ " type: 4\n"87+ " value: 32\n"88+ " -\n"89+ " name: eps\n"90+ " type: 5\n"91+ " value: 4.\n"// this line is changed!92+ " -\n"93+ " name: sorted\n"94+ " type: 8\n" // FLANN_INDEX_TYPE_BOOL95+ " value: 1\n";9697DescriptorMatcher matcher;9899int matSize;100101DMatch[] truth;102103private Mat getMaskImg() {104return new Mat(5, 2, CvType.CV_8U, new Scalar(0)) {105{106put(0, 0, 1, 1, 1, 1);107}108};109}110111private Mat getQueryDescriptors() {112Mat img = getQueryImg();113MatOfKeyPoint keypoints = new MatOfKeyPoint();114Mat descriptors = new Mat();115116Feature2D detector = createClassInstance(XFEATURES2D+"SURF", DEFAULT_FACTORY, null, null);117Feature2D extractor = createClassInstance(XFEATURES2D+"SURF", DEFAULT_FACTORY, null, null);118119setProperty(detector, "hessianThreshold", "double", 8000);120setProperty(detector, "nOctaves", "int", 3);121setProperty(detector, "upright", "boolean", false);122123detector.detect(img, keypoints);124extractor.compute(img, keypoints, descriptors);125126return descriptors;127}128129private Mat getQueryImg() {130Mat cross = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));131Imgproc.line(cross, new Point(30, matSize / 2), new Point(matSize - 31, matSize / 2), new Scalar(100), 3);132Imgproc.line(cross, new Point(matSize / 2, 30), new Point(matSize / 2, matSize - 31), new Scalar(100), 3);133134return cross;135}136137private Mat getTrainDescriptors() {138Mat img = getTrainImg();139MatOfKeyPoint keypoints = new MatOfKeyPoint(new KeyPoint(50, 50, 16, 0, 20000, 1, -1), new KeyPoint(42, 42, 16, 160, 10000, 1, -1));140Mat descriptors = new Mat();141142Feature2D extractor = createClassInstance(XFEATURES2D+"SURF", DEFAULT_FACTORY, null, null);143144extractor.compute(img, keypoints, descriptors);145146return descriptors;147}148149private Mat getTrainImg() {150Mat cross = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));151Imgproc.line(cross, new Point(20, matSize / 2), new Point(matSize - 21, matSize / 2), new Scalar(100), 2);152Imgproc.line(cross, new Point(matSize / 2, 20), new Point(matSize / 2, matSize - 21), new Scalar(100), 2);153154return cross;155}156157protected void setUp() throws Exception {158super.setUp();159matcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);160matSize = 100;161truth = new DMatch[] {162new DMatch(0, 0, 0, 0.6211397f),163new DMatch(1, 1, 0, 0.9177120f),164new DMatch(2, 1, 0, 0.3112163f),165new DMatch(3, 1, 0, 0.2925075f),166new DMatch(4, 1, 0, 0.9309179f)167};168}169170public void testAdd() {171matcher.add(Arrays.asList(new Mat()));172assertFalse(matcher.empty());173}174175public void testClear() {176matcher.add(Arrays.asList(new Mat()));177178matcher.clear();179180assertTrue(matcher.empty());181}182183public void testClone() {184Mat train = new Mat(1, 1, CvType.CV_8U, new Scalar(123));185matcher.add(Arrays.asList(train));186187try {188matcher.clone();189fail("Expected CvException (CV_StsNotImplemented)");190} catch (CvException cverr) {191// expected192}193}194195public void testCloneBoolean() {196matcher.add(Arrays.asList(new Mat()));197198DescriptorMatcher cloned = matcher.clone(true);199200assertNotNull(cloned);201assertTrue(cloned.empty());202}203204public void testCreate() {205assertNotNull(matcher);206}207208public void testEmpty() {209assertTrue(matcher.empty());210}211212public void testGetTrainDescriptors() {213Mat train = new Mat(1, 1, CvType.CV_8U, new Scalar(123));214Mat truth = train.clone();215matcher.add(Arrays.asList(train));216217List<Mat> descriptors = matcher.getTrainDescriptors();218219assertEquals(1, descriptors.size());220assertMatEqual(truth, descriptors.get(0));221}222223public void testIsMaskSupported() {224assertFalse(matcher.isMaskSupported());225}226227public void testKnnMatchMatListOfListOfDMatchInt() {228fail("Not yet implemented");229}230231public void testKnnMatchMatListOfListOfDMatchIntListOfMat() {232fail("Not yet implemented");233}234235public void testKnnMatchMatListOfListOfDMatchIntListOfMatBoolean() {236fail("Not yet implemented");237}238239public void testKnnMatchMatMatListOfListOfDMatchInt() {240fail("Not yet implemented");241}242243public void testKnnMatchMatMatListOfListOfDMatchIntMat() {244fail("Not yet implemented");245}246247public void testKnnMatchMatMatListOfListOfDMatchIntMatBoolean() {248fail("Not yet implemented");249}250251public void testMatchMatListOfDMatch() {252Mat train = getTrainDescriptors();253Mat query = getQueryDescriptors();254MatOfDMatch matches = new MatOfDMatch();255matcher.add(Arrays.asList(train));256matcher.train();257258matcher.match(query, matches);259260assertArrayDMatchEquals(truth, matches.toArray(), EPS);261}262263public void testMatchMatListOfDMatchListOfMat() {264Mat train = getTrainDescriptors();265Mat query = getQueryDescriptors();266Mat mask = getMaskImg();267MatOfDMatch matches = new MatOfDMatch();268matcher.add(Arrays.asList(train));269matcher.train();270271matcher.match(query, matches, Arrays.asList(mask));272273assertArrayDMatchEquals(truth, matches.toArray(), EPS);274}275276public void testMatchMatMatListOfDMatch() {277Mat train = getTrainDescriptors();278Mat query = getQueryDescriptors();279MatOfDMatch matches = new MatOfDMatch();280281matcher.match(query, train, matches);282283assertArrayDMatchEquals(truth, matches.toArray(), EPS);284285// OpenCVTestRunner.Log(matches.toString());286// OpenCVTestRunner.Log(matches);287}288289public void testMatchMatMatListOfDMatchMat() {290Mat train = getTrainDescriptors();291Mat query = getQueryDescriptors();292Mat mask = getMaskImg();293MatOfDMatch matches = new MatOfDMatch();294295matcher.match(query, train, matches, mask);296297assertListDMatchEquals(Arrays.asList(truth), matches.toList(), EPS);298}299300public void testRadiusMatchMatListOfListOfDMatchFloat() {301fail("Not yet implemented");302}303304public void testRadiusMatchMatListOfListOfDMatchFloatListOfMat() {305fail("Not yet implemented");306}307308public void testRadiusMatchMatListOfListOfDMatchFloatListOfMatBoolean() {309fail("Not yet implemented");310}311312public void testRadiusMatchMatMatListOfListOfDMatchFloat() {313fail("Not yet implemented");314}315316public void testRadiusMatchMatMatListOfListOfDMatchFloatMat() {317fail("Not yet implemented");318}319320public void testRadiusMatchMatMatListOfListOfDMatchFloatMatBoolean() {321fail("Not yet implemented");322}323324public void testRead() {325String filenameR = OpenCVTestRunner.getTempFileName("yml");326String filenameW = OpenCVTestRunner.getTempFileName("yml");327writeFile(filenameR, ymlParamsModified);328329matcher.read(filenameR);330matcher.write(filenameW);331332assertEquals(ymlParamsModified, readFile(filenameW));333}334335public void testTrain() {336Mat train = getTrainDescriptors();337matcher.add(Arrays.asList(train));338matcher.train();339}340341public void testTrainNoData() {342try {343matcher.train();344fail("Expected CvException - FlannBasedMatcher::train should fail on empty train set");345} catch (CvException cverr) {346// expected347}348}349350public void testWrite() {351String filename = OpenCVTestRunner.getTempFileName("xml");352353matcher.write(filename);354355assertEquals(xmlParamsDefault, readFile(filename));356}357358public void testWriteYml() {359String filename = OpenCVTestRunner.getTempFileName("yml");360361matcher.write(filename);362363assertEquals(ymlParamsDefault, readFile(filename));364}365366}367368369