Java API: added new tests for SURF and STAR feature detectors

This commit is contained in:
Andrey Kamaev 2011-08-02 16:10:58 +00:00
parent 257c0bf413
commit 1890a0ae9a
5 changed files with 273 additions and 89 deletions

View File

@ -1,31 +0,0 @@
package org.opencv.test.features2d;
import org.opencv.features2d.MSER;
import org.opencv.test.OpenCVTestCase;
public class MSERTest extends OpenCVTestCase {
private MSER mser;
@Override
protected void setUp() throws Exception {
super.setUp();
mser = null;
}
public void test_1() {
super.test_1("FEATURES2D.MSER");
}
public void testMSER() {
mser = new MSER();
assertTrue(null != mser);
}
public void testMSERIntIntIntDoubleDoubleIntDoubleDoubleInt() {
mser = new MSER(5, 60, 14400, .25f, .2f, 200, 1.01, .003, 5);
assertTrue(null != mser);
}
}

View File

@ -0,0 +1,130 @@
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.FeatureDetector;
import org.opencv.features2d.KeyPoint;
import org.opencv.test.OpenCVTestCase;
import org.opencv.test.OpenCVTestRunner;
import java.util.ArrayList;
import java.util.List;
public class STARFeatureDetectorTest extends OpenCVTestCase {
FeatureDetector detector;
KeyPoint[] truth;
int matSize;
protected void setUp() throws Exception {
detector = FeatureDetector.create(FeatureDetector.STAR);
matSize = 200;
truth = new KeyPoint[] {
new KeyPoint(95, 80, 22, -1, 31.595734f, 0, -1),
new KeyPoint(105, 80, 22, -1, 31.595734f, 0, -1),
new KeyPoint(80, 95, 22, -1, 31.595734f, 0, -1),
new KeyPoint(120, 95, 22, -1, 31.595734f, 0, -1),
new KeyPoint(100, 100, 8, -1, -219.90825f, 0, -1),
new KeyPoint(80, 105, 22, -1, 31.595734f, 0, -1),
new KeyPoint(120, 105, 22, -1, 31.595734f, 0, -1),
new KeyPoint(95, 120, 22, -1, 31.595734f, 0, -1),
new KeyPoint(105, 120, 22, -1, 31.595734f, 0, -1) };
super.setUp();
}
private Mat getTestImg() {
Scalar color = new Scalar(0);
int center = matSize / 2;
int radius = 6;
int offset = 40;
Mat img = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
Core.circle(img, new Point(center - offset, center), radius, color, -1);
Core.circle(img, new Point(center + offset, center), radius, color, -1);
Core.circle(img, new Point(center, center - offset), radius, color, -1);
Core.circle(img, new Point(center, center + offset), radius, color, -1);
Core.circle(img, new Point(center, center), radius, color, -1);
return img;
}
private Mat getMaskImg() {
Mat mask = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
Mat right = mask.submat(0, matSize, matSize / 2, matSize);
right.setTo(new Scalar(0));
return mask;
}
public void testCreate() {
assertNotNull(detector);
}
public void testDetectMatListOfKeyPointMat() {
Mat img = getTestImg();
Mat mask = getMaskImg();
List<KeyPoint> keypoints = new ArrayList<KeyPoint>();
detector.detect(img, keypoints, mask);
KeyPoint[] _truth = new KeyPoint[] { truth[0], truth[2], truth[5], truth[7] };
assertEquals(_truth.length, keypoints.size());
for (int i = 0; i < _truth.length; i++)
assertKeyPointEqual(_truth[i], keypoints.get(i), EPS);
}
public void testDetectMatListOfKeyPoint() {
Mat img = getTestImg();
List<KeyPoint> keypoints = new ArrayList<KeyPoint>();
detector.detect(img, keypoints);
assertEquals(truth.length, keypoints.size());
for (int i = 0; i < truth.length; i++)
assertKeyPointEqual(truth[i], keypoints.get(i), EPS);
}
public void testEmpty() {
assertFalse(detector.empty());
}
public void testRead() {
Mat img = getTestImg();
List<KeyPoint> keypoints1 = new ArrayList<KeyPoint>();
detector.detect(img, keypoints1);
String filename = OpenCVTestRunner.getTempFileName("yml");
writeFile(filename, "%YAML:1.0\nmaxSize: 45\nresponseThreshold: 150\nlineThresholdProjected: 10\nlineThresholdBinarized: 8\nsuppressNonmaxSize: 5\n");
detector.read(filename);
List<KeyPoint> keypoints2 = new ArrayList<KeyPoint>();
detector.detect(img, keypoints2);
assertTrue(keypoints2.size() <= keypoints1.size());
}
public void testWrite() {
String filename = OpenCVTestRunner.getTempFileName("xml");
detector.write(filename);
String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n<maxSize>45</maxSize>\n<responseThreshold>30</responseThreshold>\n<lineThresholdProjected>10</lineThresholdProjected>\n<lineThresholdBinarized>8</lineThresholdBinarized>\n<suppressNonmaxSize>5</suppressNonmaxSize>\n</opencv_storage>\n";
assertEquals(truth, readFile(filename));
}
public void testWriteYml() {
String filename = OpenCVTestRunner.getTempFileName("yml");
detector.write(filename);
String truth = "%YAML:1.0\nmaxSize: 45\nresponseThreshold: 30\nlineThresholdProjected: 10\nlineThresholdBinarized: 8\nsuppressNonmaxSize: 5\n";
assertEquals(truth, readFile(filename));
}
}

View File

@ -0,0 +1,142 @@
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.FeatureDetector;
import org.opencv.features2d.KeyPoint;
import org.opencv.test.OpenCVTestCase;
import org.opencv.test.OpenCVTestRunner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class SURFFeatureDetectorTest extends OpenCVTestCase {
FeatureDetector detector;
KeyPoint[] truth;
int matSize;
@Override
protected void setUp() throws Exception {
detector = FeatureDetector.create(FeatureDetector.SURF);
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 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;
}
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;
}
});
}
private Mat getMaskImg() {
Mat mask = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(255));
Mat right = mask.submat(0, matSize, matSize / 2, matSize);
right.setTo(new Scalar(0));
return mask;
}
public void testCreate() {
assertNotNull(detector);
}
public void testDetectMatListOfKeyPointMat() {
String filename = OpenCVTestRunner.getTempFileName("yml");
writeFile(filename, "%YAML:1.0\nhessianThreshold: 8000.\noctaves: 3\noctaveLayers: 4\nupright: 0\n");
detector.read(filename);
Mat img = getTestImg();
Mat mask = getMaskImg();
List<KeyPoint> keypoints = new ArrayList<KeyPoint>();
detector.detect(img, keypoints, mask);
KeyPoint[] _truth = new KeyPoint[] { truth[1], truth[2] };
assertEquals(_truth.length, keypoints.size());
order(keypoints);
for (int i = 0; i < _truth.length; i++)
assertKeyPointEqual(_truth[i], keypoints.get(i), EPS);
}
public void testDetectMatListOfKeyPoint() {
String filename = OpenCVTestRunner.getTempFileName("yml");
writeFile(filename, "%YAML:1.0\nhessianThreshold: 8000.\noctaves: 3\noctaveLayers: 4\nupright: 0\n");
detector.read(filename);
List<KeyPoint> keypoints = new ArrayList<KeyPoint>();
Mat cross = getTestImg();
detector.detect(cross, keypoints);
assertEquals(truth.length, keypoints.size());
order(keypoints);
for (int i = 0; i < truth.length; i++)
assertKeyPointEqual(truth[i], keypoints.get(i), EPS);
}
public void testEmpty() {
assertFalse(detector.empty());
}
public void testRead() {
Mat cross = getTestImg();
List<KeyPoint> keypoints1 = new ArrayList<KeyPoint>();
detector.detect(cross, keypoints1);
String filename = OpenCVTestRunner.getTempFileName("yml");
writeFile(filename, "%YAML:1.0\nhessianThreshold: 8000.\noctaves: 3\noctaveLayers: 4\nupright: 0\n");
detector.read(filename);
List<KeyPoint> keypoints2 = new ArrayList<KeyPoint>();
detector.detect(cross, keypoints2);
assertTrue(keypoints2.size() <= keypoints1.size());
}
public void testWrite() {
String filename = OpenCVTestRunner.getTempFileName("xml");
detector.write(filename);
String truth = "<?xml version=\"1.0\"?>\n<opencv_storage>\n<hessianThreshold>400.</hessianThreshold>\n<octaves>3</octaves>\n<octaveLayers>4</octaveLayers>\n<upright>0</upright>\n</opencv_storage>\n";
assertEquals(truth, readFile(filename));
}
public void testWriteYml() {
String filename = OpenCVTestRunner.getTempFileName("yml");
detector.write(filename);
String truth = "%YAML:1.0\nhessianThreshold: 400.\noctaves: 3\noctaveLayers: 4\nupright: 0\n";
assertEquals(truth, readFile(filename));
}
}

View File

@ -1,58 +0,0 @@
package org.opencv.test.features2d;
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.StarDetector;
import org.opencv.test.OpenCVTestCase;
public class StarDetectorTest extends OpenCVTestCase {
public void test_1() {
super.test_1("FEATURES2D.StarDetector");
}
private Mat getStarImg() {
Scalar color = new Scalar(0);
int center = 100;
int radius = 5;
int offset = 40;
Mat img = new Mat(200, 200, CvType.CV_8U, new Scalar(255));
Core.circle(img, new Point(center - offset, center), radius, color, -1);
Core.circle(img, new Point(center + offset, center), radius, color, -1);
Core.circle(img, new Point(center, center - offset), radius, color, -1);
Core.circle(img, new Point(center, center + offset), radius, color, -1);
Core.circle(img, new Point(center, center), radius, color, -1);
return img;
}
public void testDetect() {
Mat img = getStarImg();
List<KeyPoint> keypoints = new LinkedList<KeyPoint>();
StarDetector star = new StarDetector();
star.detect(img, keypoints);
KeyPoint truth = new KeyPoint(100, 100, 8, -1, -223.40334f, 0, -1);
assertEquals(1, keypoints.size());
assertKeyPointEqual(truth, keypoints.get(0), EPS);
}
public void testStarDetector() {
StarDetector star = new StarDetector();
assertNotNull(star);
}
public void testStarDetectorIntIntIntIntInt() {
StarDetector star = new StarDetector(45, 30, 10, 8, 5);
assertNotNull(star);
}
}

View File

@ -14,6 +14,7 @@ class_ignore_list = (
#features2d
"KeyPoint",
"MSER",
"StarDetector",
)
const_ignore_list = (