diff --git a/CMakeLists.txt b/CMakeLists.txt index 8bd233df5..de37a81a9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -829,7 +829,7 @@ if (BUILD_JAVA_SUPPORT) endif() if(CAN_BUILD_ANDROID_PROJECTS) - option(BUILD_ANDROID_EXAMPLES "Build examples for Android platform" TRUE) + SET(BUILD_ANDROID_EXAMPLES TRUE CACHE BOOL "Build examples for Android platform") endif() #YV diff --git a/modules/java/android_test/src/org/opencv/test/OpenCVTestCase.java b/modules/java/android_test/src/org/opencv/test/OpenCVTestCase.java index 9a57055c4..9cf80fa5c 100644 --- a/modules/java/android_test/src/org/opencv/test/OpenCVTestCase.java +++ b/modules/java/android_test/src/org/opencv/test/OpenCVTestCase.java @@ -1,18 +1,25 @@ package org.opencv.test; -import java.util.List; - -import junit.framework.TestCase; - +import org.opencv.core.Core; import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.core.Point; import org.opencv.core.Rect; import org.opencv.core.Scalar; -import org.opencv.core.Core; import org.opencv.features2d.KeyPoint; import org.opencv.highgui.Highgui; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.MappedByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.charset.Charset; +import java.util.List; + +import junit.framework.TestCase; + public class OpenCVTestCase extends TestCase { protected static int matSize = 10; @@ -156,7 +163,7 @@ public class OpenCVTestCase extends TestCase { super.tearDown(); } - + public static void assertListIntegerEquals(List list1, List list2) { if (list1.size() != list2.size()) { throw new UnsupportedOperationException(); @@ -296,4 +303,46 @@ public class OpenCVTestCase extends TestCase { OpenCVTestRunner.Log("================================================"); OpenCVTestRunner.Log("=============== " + label); } + + protected static String readFile(String path) { + FileInputStream stream = null; + try { + stream = new FileInputStream(new File(path)); + FileChannel fc = stream.getChannel(); + MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, + fc.size()); + return Charset.defaultCharset().decode(bb).toString(); + } catch (IOException e) { + OpenCVTestRunner.Log("Failed to read file \"" + path + + "\". Exception is thrown: " + e); + return null; + } finally { + if (stream != null) + try { + stream.close(); + } catch (IOException e) { + OpenCVTestRunner.Log("Exception is thrown: " + e); + } + } + } + + protected static void writeFile(String path, String content) { + FileOutputStream stream = null; + try { + stream = new FileOutputStream(new File(path)); + FileChannel fc = stream.getChannel(); + fc.write(Charset.defaultCharset().encode(content)); + } catch (IOException e) { + OpenCVTestRunner.Log("Failed to write file \"" + path + + "\". Exception is thrown: " + e); + } finally { + if (stream != null) + try { + stream.close(); + } catch (IOException e) { + OpenCVTestRunner.Log("Exception is thrown: " + e); + } + } + } + } diff --git a/modules/java/android_test/src/org/opencv/test/OpenCVTestRunner.java b/modules/java/android_test/src/org/opencv/test/OpenCVTestRunner.java index 9db758a87..8f6425b4c 100644 --- a/modules/java/android_test/src/org/opencv/test/OpenCVTestRunner.java +++ b/modules/java/android_test/src/org/opencv/test/OpenCVTestRunner.java @@ -7,6 +7,9 @@ import android.util.Log; import org.opencv.Android; +import java.io.File; +import java.io.IOException; + /** * This only class is Android specific. The original idea about test order * randomization is from marek.defecinski blog. @@ -23,6 +26,22 @@ public class OpenCVTestRunner extends InstrumentationTestRunner { private AndroidTestRunner androidTestRunner; private static String TAG = "opencv_test_java"; + + public static String getTempFileName(String extension) + { + File cache = context.getCacheDir(); + if (!extension.startsWith(".")) + extension = "." + extension; + try { + File tmp = File.createTempFile("OpenCV", extension, cache); + String path = tmp.getAbsolutePath(); + tmp.delete(); + return path; + } catch (IOException e) { + Log("Failed to get temp file name. Exception is thrown: " + e); + } + return null; + } static public void Log(String message) { Log.e(TAG, message); diff --git a/modules/java/android_test/src/org/opencv/test/features2d/FASTFeatureDetectorTest.java b/modules/java/android_test/src/org/opencv/test/features2d/FASTFeatureDetectorTest.java new file mode 100644 index 000000000..aa8244357 --- /dev/null +++ b/modules/java/android_test/src/org/opencv/test/features2d/FASTFeatureDetectorTest.java @@ -0,0 +1,145 @@ +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 FASTFeatureDetectorTest extends OpenCVTestCase { + + FeatureDetector detector; + KeyPoint[] truth; + + @Override + 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), + new KeyPoint(68, 73, 6, -1, 254, 0, -1) }; + + super.setUp(); + } + + private Mat getTestImg() { + Mat img = new Mat(100, 100, CvType.CV_8U, new Scalar(255)); + Core.line(img, new Point(30, 30), new Point(70, 70), new Scalar(0), 8); + return img; + } + + private Mat getMaskImg() { + Mat mask = new Mat(100, 100, CvType.CV_8U, new Scalar(255)); + Mat right = mask.submat(0, 100, 50, 100); + right.setTo(new Scalar(0)); + return mask; + } + + public void testCreate() { + assertNotNull(detector); + } + + public void testDetectMatListOfKeyPointMat() { + Mat img = getTestImg(); + Mat mask = getMaskImg(); + List keypoints = new ArrayList(); + + detector.detect(img, keypoints, mask); + + KeyPoint[] _truth = new KeyPoint[] { truth[0], truth[1] }; + + 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 keypoints = new ArrayList(); + + detector.detect(img, keypoints); + + assertEquals(truth.length, keypoints.size()); + for (int i = 0; i < truth.length; i++) + assertKeyPointEqual(truth[i], keypoints.get(i), EPS); + + // OpenCVTestRunner.Log("points found: " + keypoints.size()); + // for (KeyPoint kp : keypoints) + // OpenCVTestRunner.Log(kp.toString()); + } + + public void testEmpty() { + assertFalse(detector.empty()); + } + + public void testRead() { + String filename = OpenCVTestRunner.getTempFileName("yml"); + + writeFile(filename, "%YAML:1.0\nthreshold: 130\nnonmaxSuppression: 1\n"); + detector.read(filename); + + List keypoints1 = new ArrayList(); + + detector.detect(grayChess, keypoints1); + + writeFile(filename, "%YAML:1.0\nthreshold: 150\nnonmaxSuppression: 1\n"); + detector.read(filename); + + List keypoints2 = new ArrayList(); + + detector.detect(grayChess, keypoints2); + + assertTrue(keypoints2.size() <= keypoints1.size()); + } + + public void testReadYml() { + String filename = OpenCVTestRunner.getTempFileName("yml"); + + writeFile( + filename, + "\n\n130\n1\n\n"); + detector.read(filename); + + List keypoints1 = new ArrayList(); + + detector.detect(grayChess, keypoints1); + + writeFile( + filename, + "\n\n150\n1\n\n"); + detector.read(filename); + + List keypoints2 = new ArrayList(); + + detector.detect(grayChess, keypoints2); + + assertTrue(keypoints2.size() <= keypoints1.size()); + } + + public void testWrite() { + String filename = OpenCVTestRunner.getTempFileName("xml"); + + detector.write(filename); + + String truth = "\n\n10\n1\n\n"; + assertEquals(truth, readFile(filename)); + } + + public void testWriteYml() { + String filename = OpenCVTestRunner.getTempFileName("yml"); + + detector.write(filename); + + String truth = "%YAML:1.0\nthreshold: 10\nnonmaxSuppression: 1\n"; + assertEquals(truth, readFile(filename)); + } + +} diff --git a/modules/java/gen_java.py b/modules/java/gen_java.py index e2a2af5eb..0a33146eb 100644 --- a/modules/java/gen_java.py +++ b/modules/java/gen_java.py @@ -13,6 +13,7 @@ class_ignore_list = ( "VideoWriter", "VideoCapture", #features2d "KeyPoint", + "MSER", ) const_ignore_list = ( diff --git a/modules/java/src/cpp/features2d_manual.hpp b/modules/java/src/cpp/features2d_manual.hpp index 9812fcfad..6c882927e 100644 --- a/modules/java/src/cpp/features2d_manual.hpp +++ b/modules/java/src/cpp/features2d_manual.hpp @@ -10,8 +10,8 @@ class CV_EXPORTS_AS(FeatureDetector) javaFeatureDetector : public FeatureDetecto { public: #if 0 - CV_WRAP void detect( const Mat& image, vector& keypoints, const Mat& mask=Mat() ) const; - CV_WRAP void detect( const vector& images, vector >& keypoints, const vector& masks=vector() ) const; + CV_WRAP void detect( const Mat& image, CV_OUT vector& keypoints, const Mat& mask=Mat() ) const; + CV_WRAP void detect( const vector& images, CV_OUT vector >& keypoints, const vector& masks=vector() ) const; CV_WRAP virtual bool empty() const; #endif @@ -145,18 +145,18 @@ public: CV_WRAP virtual bool empty() const; CV_WRAP virtual void train(); CV_WRAP void match( const Mat& queryDescriptors, const Mat& trainDescriptors, - vector& matches, const Mat& mask=Mat() ) const; + CV_OUT vector& matches, const Mat& mask=Mat() ) const; CV_WRAP void knnMatch( const Mat& queryDescriptors, const Mat& trainDescriptors, - vector >& matches, int k, + CV_OUT vector >& matches, int k, const Mat& mask=Mat(), bool compactResult=false ) const; CV_WRAP void radiusMatch( const Mat& queryDescriptors, const Mat& trainDescriptors, - vector >& matches, float maxDistance, + CV_OUT vector >& matches, float maxDistance, const Mat& mask=Mat(), bool compactResult=false ) const; - CV_WRAP void match( const Mat& queryDescriptors, vector& matches, + CV_WRAP void match( const Mat& queryDescriptors, CV_OUT vector& matches, const vector& masks=vector() ); - CV_WRAP void knnMatch( const Mat& queryDescriptors, vector >& matches, int k, + CV_WRAP void knnMatch( const Mat& queryDescriptors, CV_OUT vector >& matches, int k, const vector& masks=vector(), bool compactResult=false ); - CV_WRAP void radiusMatch( const Mat& queryDescriptors, vector >& matches, float maxDistance, + CV_WRAP void radiusMatch( const Mat& queryDescriptors, CV_OUT vector >& matches, float maxDistance, const vector& masks=vector(), bool compactResult=false ); #endif @@ -229,7 +229,7 @@ class CV_EXPORTS_AS(DescriptorExtractor) javaDescriptorExtractor : public Descri public: #if 0 CV_WRAP void compute( const Mat& image, vector& keypoints, Mat& descriptors ) const; - CV_WRAP void compute( const vector& images, vector >& keypoints, vector& descriptors ) const; + CV_WRAP void compute( const vector& images, vector >& keypoints, CV_OUT vector& descriptors ) const; CV_WRAP virtual int descriptorSize() const = 0; CV_WRAP virtual int descriptorType() const = 0;