Java API: fixed bugs; added tests for SURF and BRIEF descriptor extractors

This commit is contained in:
Andrey Kamaev 2011-08-03 09:27:50 +00:00
parent f81e6f82f5
commit 69588dfabf
7 changed files with 225 additions and 168 deletions

View File

@ -6,6 +6,7 @@ import android.test.InstrumentationTestRunner;
import android.util.Log;
import org.opencv.android.Utils;
import org.opencv.core.Mat;
import java.io.File;
import java.io.IOException;
@ -46,6 +47,10 @@ public class OpenCVTestRunner extends InstrumentationTestRunner {
static public void Log(String message) {
Log.e(TAG, message);
}
static public void Log(Mat m) {
Log.e(TAG, m + "\n" + m.dump());
}
@Override
public void onStart() {

View File

@ -0,0 +1,107 @@
package org.opencv.test.features2d;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.features2d.DescriptorExtractor;
import org.opencv.features2d.KeyPoint;
import org.opencv.test.OpenCVTestCase;
import org.opencv.test.OpenCVTestRunner;
import java.util.Arrays;
import java.util.List;
public class BRIEFDescriptorExtractorTest extends OpenCVTestCase {
DescriptorExtractor extractor;
int matSize;
private Mat getTestImg() {
Mat cross = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
Core.line(cross, new Point(20, matSize / 2), new Point(matSize - 21, matSize / 2), new Scalar(100), 2);
Core.line(cross, new Point(matSize / 2, 20), new Point(matSize / 2, matSize - 21), new Scalar(100), 2);
return cross;
}
@Override
protected void setUp() throws Exception {
extractor = DescriptorExtractor.create(DescriptorExtractor.BRIEF);
matSize = 100;
super.setUp();
}
public void testCompute() {
KeyPoint point = new KeyPoint(55.775577545166016f, 44.224422454833984f, 16, 9.754629f, 8617.863f, 1, -1);
List<KeyPoint> keypoints = Arrays.asList(point);
Mat img = getTestImg();
Mat descriptors = new Mat();
extractor.compute(img, keypoints, descriptors);
OpenCVTestRunner.Log(descriptors);
Mat truth = new Mat(1, 32, CvType.CV_8UC1) {
{
put(0, 0, 96, 0, 76, 24, 47, 182, 68, 137, 149, 195, 67, 16, 187, 224, 74, 8, 82, 169, 87, 70, 44, 4, 192, 56, 13, 128, 44, 106, 146, 72, 194,
245);
}
};
assertMatEqual(truth, descriptors, EPS);
}
public void testCreate() {
assertNotNull(extractor);
}
public void testDescriptorSize() {
assertEquals(32, extractor.descriptorSize());
}
public void testDescriptorType() {
assertEquals(CvType.CV_8U, extractor.descriptorType());
}
public void testEmpty() {
assertFalse(extractor.empty());
}
public void testRead() {
KeyPoint point = new KeyPoint(55.775577545166016f, 44.224422454833984f, 16, 9.754629f, 8617.863f, 1, -1);
List<KeyPoint> keypoints = Arrays.asList(point);
Mat img = getTestImg();
Mat descriptors = new Mat();
String filename = OpenCVTestRunner.getTempFileName("yml");
writeFile(filename, "%YAML:1.0\nnOctaves: 4\nnOctaveLayers: 2\nextended: 1\nupright: 0\n");
extractor.read(filename);
extractor.compute(img, keypoints, descriptors);
assertEquals(128, descriptors.cols());
}
public void testWrite() {
String filename = OpenCVTestRunner.getTempFileName("xml");
extractor.write(filename);
//OpenCVTestRunner.Log("!!!!!!!" + readFile(filename));
String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>!!!!\n</opencv_storage>\n";
assertEquals(truth, readFile(filename));
}
public void testWriteYml() {
String filename = OpenCVTestRunner.getTempFileName("yml");
extractor.write(filename);
//OpenCVTestRunner.Log("!!!!!!!" + readFile(filename));
String truth = "%YAML:1.0\n!!!";
assertEquals(truth, readFile(filename));
}
}

View File

@ -22,9 +22,7 @@ public class FASTFeatureDetectorTest extends OpenCVTestCase {
protected void setUp() throws Exception {
detector = FeatureDetector.create(FeatureDetector.FAST);
truth = new KeyPoint[] { new KeyPoint(32, 27, 6, -1, 254, 0, -1),
new KeyPoint(27, 32, 6, -1, 254, 0, -1),
new KeyPoint(73, 68, 6, -1, 254, 0, -1),
truth = new KeyPoint[] { new KeyPoint(32, 27, 6, -1, 254, 0, -1), new KeyPoint(27, 32, 6, -1, 254, 0, -1), new KeyPoint(73, 68, 6, -1, 254, 0, -1),
new KeyPoint(68, 73, 6, -1, 254, 0, -1) };
super.setUp();
@ -103,8 +101,7 @@ public class FASTFeatureDetectorTest extends OpenCVTestCase {
public void testReadYml() {
String filename = OpenCVTestRunner.getTempFileName("yml");
writeFile(
filename,
writeFile(filename,
"<?xml version=\"1.0\"?>\n<opencv_storage>\n<threshold>130</threshold>\n<nonmaxSuppression>1</nonmaxSuppression>\n</opencv_storage>\n");
detector.read(filename);
@ -112,8 +109,7 @@ public class FASTFeatureDetectorTest extends OpenCVTestCase {
detector.detect(grayChess, keypoints1);
writeFile(
filename,
writeFile(filename,
"<?xml version=\"1.0\"?>\n<opencv_storage>\n<threshold>150</threshold>\n<nonmaxSuppression>1</nonmaxSuppression>\n</opencv_storage>\n");
detector.read(filename);

View File

@ -0,0 +1,107 @@
package org.opencv.test.features2d;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.features2d.DescriptorExtractor;
import org.opencv.features2d.KeyPoint;
import org.opencv.test.OpenCVTestCase;
import org.opencv.test.OpenCVTestRunner;
import java.util.Arrays;
import java.util.List;
public class SURFDescriptorExtractorTest extends OpenCVTestCase {
DescriptorExtractor extractor;
int matSize;
private Mat getTestImg() {
Mat cross = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
Core.line(cross, new Point(20, matSize / 2), new Point(matSize - 21, matSize / 2), new Scalar(100), 2);
Core.line(cross, new Point(matSize / 2, 20), new Point(matSize / 2, matSize - 21), new Scalar(100), 2);
return cross;
}
@Override
protected void setUp() throws Exception {
extractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
matSize = 100;
super.setUp();
}
public void testCompute() {
KeyPoint point = new KeyPoint(55.775577545166016f, 44.224422454833984f, 16, 9.754629f, 8617.863f, 1, -1);
List<KeyPoint> keypoints = Arrays.asList(point);
Mat img = getTestImg();
Mat descriptors = new Mat();
extractor.compute(img, keypoints, descriptors);
Mat truth = new Mat(1, 64, CvType.CV_32FC1) {
{
put(0, 0, 0, 0, 0, 0, 0.011540107, 0.0029440077, 0.095483348, 0.018144149, 0.00014820647, 0, 0.00014820647, 0, 0, 0, 0, 0, 0, -0.00014820647,
0, 0.00014820647, 0.10196275, 0.0099145742, 0.57075155, 0.047922116, 0, 0, 0, 0, 0, 0, 0, 0, 0.0029440068, -0.011540107, 0.018144149,
0.095483348, 0.085385554, -0.054076977, 0.34105155, 0.47911066, 0.023395451, -0.11012388, 0.088196531, 0.50863767, 0.0031790689,
-0.019882837, 0.0089476965, 0.054817006, -0.0033560959, -0.0011770058, 0.0033560959, 0.0011770058, 0.019882834, 0.0031790687,
0.054817006, 0.0089476984, 0, 0, 0, 0, -0.0011770058, 0.0033560959, 0.0011770058, 0.0033560959);
}
};
assertMatEqual(truth, descriptors, EPS);
}
public void testCreate() {
assertNotNull(extractor);
}
public void testDescriptorSize() {
assertEquals(64, extractor.descriptorSize());
}
public void testDescriptorType() {
assertEquals(CvType.CV_32F, extractor.descriptorType());
}
public void testEmpty() {
assertFalse(extractor.empty());
}
public void testRead() {
KeyPoint point = new KeyPoint(55.775577545166016f, 44.224422454833984f, 16, 9.754629f, 8617.863f, 1, -1);
List<KeyPoint> keypoints = Arrays.asList(point);
Mat img = getTestImg();
Mat descriptors = new Mat();
String filename = OpenCVTestRunner.getTempFileName("yml");
writeFile(filename, "%YAML:1.0\nnOctaves: 4\nnOctaveLayers: 2\nextended: 1\nupright: 0\n");
extractor.read(filename);
extractor.compute(img, keypoints, descriptors);
assertEquals(128, descriptors.cols());
}
public void testWrite() {
String filename = OpenCVTestRunner.getTempFileName("xml");
extractor.write(filename);
String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n<nOctaves>4</nOctaves>\n<nOctaveLayers>2</nOctaveLayers>\n<extended>0</extended>\n<upright>0</upright>\n</opencv_storage>\n";
assertEquals(truth, readFile(filename));
}
public void testWriteYml() {
String filename = OpenCVTestRunner.getTempFileName("yml");
extractor.write(filename);
String truth = "%YAML:1.0\nnOctaves: 4\nnOctaveLayers: 2\nextended: 0\nupright: 0\n";
assertEquals(truth, readFile(filename));
}
}

View File

@ -1,158 +0,0 @@
package org.opencv.test.features2d;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.features2d.KeyPoint;
import org.opencv.features2d.SURF;
import org.opencv.test.OpenCVTestCase;
public class SURFTest extends OpenCVTestCase {
int matSize;
KeyPoint[] truth;
public void test_1() {
super.test_1("features2d.SURF");
}
@Override
protected void setUp() throws Exception {
matSize = 100;
truth = new KeyPoint[] { new KeyPoint(55.775577545166016f, 44.224422454833984f, 16, 9.754629f, 8617.863f, 1, -1),
new KeyPoint(44.224422454833984f, 44.224422454833984f, 16, 99.75463f, 8617.863f, 1, -1),
new KeyPoint(44.224422454833984f, 55.775577545166016f, 16, 189.7546f, 8617.863f, 1, -1),
new KeyPoint(55.775577545166016f, 55.775577545166016f, 16, 279.75464f, 8617.863f, 1, -1) };
super.setUp();
}
private Mat getCross() {
Mat cross = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
Core.line(cross, new Point(20, matSize / 2), new Point(matSize - 21, matSize / 2), new Scalar(100), 2);
Core.line(cross, new Point(matSize / 2, 20), new Point(matSize / 2, matSize - 21), new Scalar(100), 2);
return cross;
}
private void order(List<KeyPoint> points) {
Collections.sort(points, new Comparator<KeyPoint>() {
public int compare(KeyPoint p1, KeyPoint p2) {
if (p1.angle < p2.angle)
return -1;
if (p1.angle > p2.angle)
return 1;
return 0;
}
});
}
public void testDescriptorSize() {
SURF surf = new SURF(500.0, 4, 2, false);
assertEquals(64, surf.descriptorSize());
surf = new SURF(500.0, 4, 2, true);
assertEquals(128, surf.descriptorSize());
}
public void testDetectMatMatListOfKeyPoint_noPointsDetected() {
SURF surf = new SURF(8000);
List<KeyPoint> keypoints = new LinkedList<KeyPoint>();
Mat gray0 = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
surf.detect(gray0, new Mat(), keypoints);
assertEquals(0, keypoints.size());
}
public void testDetectMatMatListOfKeyPoint() {
SURF surf = new SURF(8000);
List<KeyPoint> keypoints = new LinkedList<KeyPoint>();
Mat cross = getCross();
surf.detect(cross, new Mat(), keypoints);
assertEquals(truth.length, keypoints.size());
order(keypoints);
for (int i = 0; i < truth.length; i++)
assertKeyPointEqual(truth[i], keypoints.get(i), EPS);
// for(KeyPoint kp : keypoints)
// OpenCVTestRunner.Log(kp.toString());
}
public void testDetectMatMatListOfKeyPointListOfFloat() {
SURF surf = new SURF(8000);
List<KeyPoint> keypoints = new LinkedList<KeyPoint>();
List<Float> descriptors = new LinkedList<Float>();
Mat cross = getCross();
surf.detect(cross, new Mat(), keypoints, descriptors);
assertEquals(truth.length, keypoints.size());
assertEquals(truth.length * surf.descriptorSize(), descriptors.size());
order(keypoints);
for (int i = 0; i < truth.length; i++)
assertKeyPointEqual(truth[i], (KeyPoint) keypoints.get(i), EPS);
}
public void testDetectMatMatListOfKeyPointListOfFloatBoolean() {
SURF surf = new SURF(8000);
List<KeyPoint> original_keypoints = Arrays.asList(truth);
List<KeyPoint> keypoints = new LinkedList<KeyPoint>(original_keypoints);
List<Float> descriptors = new LinkedList<Float>();
Mat gray255 = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
surf.detect(gray255, new Mat(), keypoints, descriptors, true);
// unmodified keypoints
assertEquals(original_keypoints.size(), keypoints.size());
for (int i = 0; i < keypoints.size(); i++)
assertKeyPointEqual(original_keypoints.get(i), keypoints.get(i), EPS);
// zero descriptors
assertEquals(surf.descriptorSize() * original_keypoints.size(), descriptors.size());
for (float d : descriptors)
assertTrue(Math.abs(d) < EPS);
}
public void testSURF() {
SURF surf = new SURF();
assertNotNull(surf);
}
public void testSURFDouble() {
SURF surf = new SURF(500.0);
assertNotNull(surf);
}
public void testSURFDoubleInt() {
SURF surf = new SURF(500.0, 4);
assertNotNull(surf);
}
public void testSURFDoubleIntInt() {
SURF surf = new SURF(500.0, 4, 2);
assertNotNull(surf);
}
public void testSURFDoubleIntIntBoolean() {
SURF surf = new SURF(500.0, 4, 2, false);
assertNotNull(surf);
}
public void testSURFDoubleIntIntBooleanBoolean() {
SURF surf = new SURF(500.0, 4, 2, false, false);
assertNotNull(surf);
}
}

View File

@ -12,7 +12,7 @@ class_ignore_list = (
#highgui
"VideoWriter", "VideoCapture",
#features2d
"KeyPoint", "MSER", "StarDetector",
"KeyPoint", "MSER", "StarDetector", "SURF",
)
const_ignore_list = (

View File

@ -230,8 +230,8 @@ public:
#if 0
CV_WRAP void compute( const Mat& image, vector<KeyPoint>& keypoints, Mat& descriptors ) const;
CV_WRAP void compute( const vector<Mat>& images, vector<vector<KeyPoint> >& keypoints, CV_OUT vector<Mat>& descriptors ) const;
CV_WRAP virtual int descriptorSize() const = 0;
CV_WRAP virtual int descriptorType() const = 0;
CV_WRAP virtual int descriptorSize() const;
CV_WRAP virtual int descriptorType() const;
CV_WRAP virtual bool empty() const;
#endif