Path: blob/master/modules/features2d/misc/java/test/SURFDescriptorExtractorTest.java
16354 views
package org.opencv.test.features2d;12import org.opencv.core.CvType;3import org.opencv.core.Mat;4import org.opencv.core.MatOfKeyPoint;5import org.opencv.core.Point;6import org.opencv.core.Scalar;7import org.opencv.core.KeyPoint;8import org.opencv.test.OpenCVTestCase;9import org.opencv.test.OpenCVTestRunner;10import org.opencv.imgproc.Imgproc;11import org.opencv.features2d.Feature2D;1213public class SURFDescriptorExtractorTest extends OpenCVTestCase {1415Feature2D extractor;16int matSize;1718private Mat getTestImg() {19Mat cross = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));20Imgproc.line(cross, new Point(20, matSize / 2), new Point(matSize - 21, matSize / 2), new Scalar(100), 2);21Imgproc.line(cross, new Point(matSize / 2, 20), new Point(matSize / 2, matSize - 21), new Scalar(100), 2);2223return cross;24}2526@Override27protected void setUp() throws Exception {28super.setUp();2930Class[] cParams = {double.class, int.class, int.class, boolean.class, boolean.class};31Object[] oValues = {100, 2, 4, true, false};32extractor = createClassInstance(XFEATURES2D+"SURF", DEFAULT_FACTORY, cParams, oValues);3334matSize = 100;35}3637public void testComputeListOfMatListOfListOfKeyPointListOfMat() {38fail("Not yet implemented");39}4041public void testComputeMatListOfKeyPointMat() {42KeyPoint point = new KeyPoint(55.775577545166016f, 44.224422454833984f, 16, 9.754629f, 8617.863f, 1, -1);43MatOfKeyPoint keypoints = new MatOfKeyPoint(point);44Mat img = getTestImg();45Mat descriptors = new Mat();4647extractor.compute(img, keypoints, descriptors);4849Mat truth = new Mat(1, 128, CvType.CV_32FC1) {50{51put(0, 0,520, 0, 0, 0, 0, 0, 0, 0, 0.058821894, 0.058821894, -0.045962855, 0.046261817, 0.0085156476,530.0085754395, -0.0064509804, 0.0064509804, 0.00044069235, 0.00044069235, 0, 0, 0.00025723741,540.00025723741, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.00025723741, 0.00025723741, -0.00044069235,550.00044069235, 0, 0, 0.36278215, 0.36278215, -0.24688604, 0.26173124, 0.052068226, 0.052662034,56-0.032815345, 0.032815345, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.0064523756,570.0064523756, 0.0082002236, 0.0088908644, -0.059001274, 0.059001274, 0.045789491, 0.04648013,580.11961588, 0.22789426, -0.01322381, 0.18291828, -0.14042182, 0.23973691, 0.073782086, 0.23769434,59-0.027880307, 0.027880307, 0.049587864, 0.049587864, -0.33991757, 0.33991757, 0.21437603, 0.21437603,60-0.0020763327, 0.0020763327, 0.006245892, 0.006245892, -0.04067041, 0.04067041, 0.019361559,610.019361559, 0, 0, -0.0035977389, 0.0035977389, 0, 0, -0.00099993451, 0.00099993451, 0.040670406,620.040670406, -0.019361559, 0.019361559, 0.006245892, 0.006245892, -0.0020763327, 0.0020763327,63-0.00034532088, 0.00034532088, 0, 0, 0, 0, 0.00034532088, 0.00034532088, -0.00099993451,640.00099993451, 0, 0, 0, 0, 0.0035977389, 0.003597738965);66}67};6869assertMatEqual(truth, descriptors, EPS);70}7172public void testCreate() {73assertNotNull(extractor);74}7576public void testDescriptorSize() {77assertEquals(128, extractor.descriptorSize());78}7980public void testDescriptorType() {81assertEquals(CvType.CV_32F, extractor.descriptorType());82}8384public void testEmpty() {85// assertFalse(extractor.empty());86fail("Not yet implemented");87}8889public void testRead() {90String filename = OpenCVTestRunner.getTempFileName("yml");91writeFile(filename, "%YAML:1.0\n---\nnOctaves: 4\nnOctaveLayers: 2\nextended: 1\nupright: 0\n");9293extractor.read(filename);9495assertEquals(128, extractor.descriptorSize());96}9798public void testWrite() {99String filename = OpenCVTestRunner.getTempFileName("xml");100101extractor.write(filename);102103// String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n<name>Feature2D.SURF</name>\n<extended>1</extended>\n<hessianThreshold>100.</hessianThreshold>\n<nOctaveLayers>2</nOctaveLayers>\n<nOctaves>4</nOctaves>\n<upright>0</upright>\n</opencv_storage>\n";104String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n</opencv_storage>\n";105assertEquals(truth, readFile(filename));106}107108public void testWriteYml() {109String filename = OpenCVTestRunner.getTempFileName("yml");110111extractor.write(filename);112113// String truth = "%YAML:1.0\n---\nname: \"Feature2D.SURF\"\nextended: 1\nhessianThreshold: 100.\nnOctaveLayers: 2\nnOctaves: 4\nupright: 0\n";114String truth = "%YAML:1.0\n---\n";115assertEquals(truth, readFile(filename));116}117118}119120121