Path: blob/master/modules/features2d/misc/java/test/SIFTDescriptorExtractorTest.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 SIFTDescriptorExtractorTest extends OpenCVTestCase {1415Feature2D extractor;16KeyPoint keypoint;17int matSize;18Mat truth;1920private Mat getTestImg() {21Mat cross = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));22Imgproc.line(cross, new Point(20, matSize / 2), new Point(matSize - 21, matSize / 2), new Scalar(100), 2);23Imgproc.line(cross, new Point(matSize / 2, 20), new Point(matSize / 2, matSize - 21), new Scalar(100), 2);2425return cross;26}2728@Override29protected void setUp() throws Exception {30super.setUp();31extractor = createClassInstance(XFEATURES2D+"SIFT", DEFAULT_FACTORY, null, null);32keypoint = new KeyPoint(55.775577545166016f, 44.224422454833984f, 16, 9.754629f, 8617.863f, 1, -1);33matSize = 100;34truth = new Mat(1, 128, CvType.CV_32FC1) {35{36put(0, 0,370, 0, 0, 1, 3, 0, 0, 0, 15, 23, 22, 20, 24, 2, 0, 0, 7, 8, 2, 0,380, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 27, 16, 13, 2, 0, 0, 117,3986, 79, 68, 117, 42, 5, 5, 79, 60, 117, 25, 9, 2, 28, 19, 11, 13,4020, 2, 0, 0, 5, 8, 0, 0, 76, 58, 34, 31, 97, 16, 95, 49, 117, 92,41117, 112, 117, 76, 117, 54, 117, 25, 29, 22, 117, 117, 16, 11, 14,421, 0, 0, 22, 26, 0, 0, 0, 0, 1, 4, 15, 2, 47, 8, 0, 0, 82, 56, 31,4317, 81, 12, 0, 0, 26, 23, 18, 23, 0, 0, 0, 0, 0, 0, 0, 044);45}46};47}4849public void testComputeListOfMatListOfListOfKeyPointListOfMat() {50fail("Not yet implemented");51}5253public void testComputeMatListOfKeyPointMat() {54MatOfKeyPoint keypoints = new MatOfKeyPoint(keypoint);55Mat img = getTestImg();56Mat descriptors = new Mat();5758extractor.compute(img, keypoints, descriptors);5960assertMatEqual(truth, descriptors, EPS);61}6263public void testCreate() {64assertNotNull(extractor);65}6667public void testDescriptorSize() {68assertEquals(128, extractor.descriptorSize());69}7071public void testDescriptorType() {72assertEquals(CvType.CV_32F, extractor.descriptorType());73}7475public void testEmpty() {76// assertFalse(extractor.empty());77fail("Not yet implemented"); //SIFT does not override empty() method78}7980public void testRead() {81fail("Not yet implemented");82}8384public void testWrite() {85String filename = OpenCVTestRunner.getTempFileName("xml");8687extractor.write(filename);8889// String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n<name>Feature2D.SIFT</name>\n<contrastThreshold>4.0000000000000001e-02</contrastThreshold>\n<edgeThreshold>10.</edgeThreshold>\n<nFeatures>0</nFeatures>\n<nOctaveLayers>3</nOctaveLayers>\n<sigma>1.6000000000000001e+00</sigma>\n</opencv_storage>\n";90String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n</opencv_storage>\n";91String actual = readFile(filename);92actual = actual.replaceAll("e([+-])0(\\d\\d)", "e$1$2"); // NOTE: workaround for different platforms double representation93assertEquals(truth, actual);94}9596public void testWriteYml() {97String filename = OpenCVTestRunner.getTempFileName("yml");9899extractor.write(filename);100101// String truth = "%YAML:1.0\n---\nname: \"Feature2D.SIFT\"\ncontrastThreshold: 4.0000000000000001e-02\nedgeThreshold: 10.\nnFeatures: 0\nnOctaveLayers: 3\nsigma: 1.6000000000000001e+00\n";102String truth = "%YAML:1.0\n---\n";103String actual = readFile(filename);104actual = actual.replaceAll("e([+-])0(\\d\\d)", "e$1$2"); // NOTE: workaround for different platforms double representation105assertEquals(truth, actual);106}107108}109110111