1 package org.opencv.test.features2d;
3 import org.opencv.core.Core;
4 import org.opencv.core.CvType;
5 import org.opencv.core.Mat;
6 import org.opencv.core.MatOfKeyPoint;
7 import org.opencv.core.Point;
8 import org.opencv.core.Scalar;
9 import org.opencv.features2d.DescriptorExtractor;
10 import org.opencv.core.KeyPoint;
11 import org.opencv.test.OpenCVTestCase;
12 import org.opencv.test.OpenCVTestRunner;
13 import org.opencv.imgproc.Imgproc;
15 public class SIFTDescriptorExtractorTest extends OpenCVTestCase {
17 DescriptorExtractor extractor;
22 private Mat getTestImg() {
23 Mat cross = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
24 Imgproc.line(cross, new Point(20, matSize / 2), new Point(matSize - 21, matSize / 2), new Scalar(100), 2);
25 Imgproc.line(cross, new Point(matSize / 2, 20), new Point(matSize / 2, matSize - 21), new Scalar(100), 2);
31 protected void setUp() throws Exception {
33 extractor = DescriptorExtractor.create(DescriptorExtractor.SIFT);
34 keypoint = new KeyPoint(55.775577545166016f, 44.224422454833984f, 16, 9.754629f, 8617.863f, 1, -1);
36 truth = new Mat(1, 128, CvType.CV_32FC1) {
39 0, 0, 0, 1, 3, 0, 0, 0, 15, 23, 22, 20, 24, 2, 0, 0, 7, 8, 2, 0,
40 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 27, 16, 13, 2, 0, 0, 117,
41 86, 79, 68, 117, 42, 5, 5, 79, 60, 117, 25, 9, 2, 28, 19, 11, 13,
42 20, 2, 0, 0, 5, 8, 0, 0, 76, 58, 34, 31, 97, 16, 95, 49, 117, 92,
43 117, 112, 117, 76, 117, 54, 117, 25, 29, 22, 117, 117, 16, 11, 14,
44 1, 0, 0, 22, 26, 0, 0, 0, 0, 1, 4, 15, 2, 47, 8, 0, 0, 82, 56, 31,
45 17, 81, 12, 0, 0, 26, 23, 18, 23, 0, 0, 0, 0, 0, 0, 0, 0
51 public void testComputeListOfMatListOfListOfKeyPointListOfMat() {
52 fail("Not yet implemented");
55 public void testComputeMatListOfKeyPointMat() {
56 MatOfKeyPoint keypoints = new MatOfKeyPoint(keypoint);
57 Mat img = getTestImg();
58 Mat descriptors = new Mat();
60 extractor.compute(img, keypoints, descriptors);
62 assertMatEqual(truth, descriptors, EPS);
65 public void testCreate() {
66 assertNotNull(extractor);
69 public void testDescriptorSize() {
70 assertEquals(128, extractor.descriptorSize());
73 public void testDescriptorType() {
74 assertEquals(CvType.CV_32F, extractor.descriptorType());
77 public void testEmpty() {
78 assertFalse(extractor.empty());
81 public void testRead() {
82 fail("Not yet implemented");
85 public void testWrite() {
86 String filename = OpenCVTestRunner.getTempFileName("xml");
88 extractor.write(filename);
90 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";
91 String actual = readFile(filename);
92 actual = actual.replaceAll("e([+-])0(\\d\\d)", "e$1$2"); // NOTE: workaround for different platforms double representation
93 assertEquals(truth, actual);
96 public void testWriteYml() {
97 String filename = OpenCVTestRunner.getTempFileName("yml");
99 extractor.write(filename);
101 String truth = "%YAML:1.0\nname: \"Feature2D.SIFT\"\ncontrastThreshold: 4.0000000000000001e-02\nedgeThreshold: 10.\nnFeatures: 0\nnOctaveLayers: 3\nsigma: 1.6000000000000001e+00\n";
102 String actual = readFile(filename);
103 actual = actual.replaceAll("e([+-])0(\\d\\d)", "e$1$2"); // NOTE: workaround for different platforms double representation
104 assertEquals(truth, actual);