From 20b4d0fae922803e15cd153c37760d50b7fd11a3 Mon Sep 17 00:00:00 2001 From: Andrey Kamaev Date: Wed, 27 Jul 2011 15:03:16 +0000 Subject: [PATCH] Added vector_Point2f_to_Mat converter --- .../src/org/opencv/test/OpenCVTestCase.java | 1 - .../org/opencv/test/calib3d/calib3dTest.java | 25 +- .../src/org/opencv/test/core/coreTest.java | 39 +- modules/java/src/java/Converters.java | 426 +++++++++--------- 4 files changed, 281 insertions(+), 210 deletions(-) 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 de8d163f17..91f8fabd13 100644 --- a/modules/java/android_test/src/org/opencv/test/OpenCVTestCase.java +++ b/modules/java/android_test/src/org/opencv/test/OpenCVTestCase.java @@ -166,7 +166,6 @@ public class OpenCVTestCase extends TestCase { } Mat diff = new Mat(); Core.absdiff(expected, actual, diff); - OpenCVTestRunner.Log(diff + " \n " + diff.dump()); if(isEqualityMeasured) assertTrue("Max difference between expected and actiual values is bigger than " + eps, Core.checkRange(diff, true, new Point(), 0.0, eps)); diff --git a/modules/java/android_test/src/org/opencv/test/calib3d/calib3dTest.java b/modules/java/android_test/src/org/opencv/test/calib3d/calib3dTest.java index 47e9c01d5a..6358053105 100644 --- a/modules/java/android_test/src/org/opencv/test/calib3d/calib3dTest.java +++ b/modules/java/android_test/src/org/opencv/test/calib3d/calib3dTest.java @@ -1,5 +1,8 @@ package org.opencv.test.calib3d; +import android.util.Log; + +import org.opencv.Converters; import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.core.Point; @@ -8,6 +11,10 @@ import org.opencv.core.Size; import org.opencv.calib3d.Calib3d; import org.opencv.core.Core; import org.opencv.test.OpenCVTestCase; +import org.opencv.test.OpenCVTestRunner; + +import java.util.ArrayList; +import java.util.List; public class calib3dTest extends OpenCVTestCase { @@ -265,7 +272,23 @@ public class calib3dTest extends OpenCVTestCase { } public void testFindHomographyMatMat() { - fail("Not yet implemented"); + + List originalPoints = new ArrayList(); + List transformedPoints = new ArrayList(); + + for (int i = 0; i < 20; i++){ + double x = Math.random() * 100 - 50; + double y = Math.random() * 100 - 50; + originalPoints.add(new Point(x,y)); + transformedPoints.add(new Point(y,x)); + } + + Mat hmg = Calib3d.findHomography(Converters.vector_Point2f_to_Mat(originalPoints), Converters.vector_Point2f_to_Mat(transformedPoints)); + + truth = new Mat(3,3, CvType.CV_64F); + truth.put(0, 0, 0,1,0,1,0,0,0,0,1); + + assertMatEqual(truth, hmg, EPS); } public void testFindHomographyMatMatInt() { diff --git a/modules/java/android_test/src/org/opencv/test/core/coreTest.java b/modules/java/android_test/src/org/opencv/test/core/coreTest.java index 88782bbac2..9988acabe5 100644 --- a/modules/java/android_test/src/org/opencv/test/core/coreTest.java +++ b/modules/java/android_test/src/org/opencv/test/core/coreTest.java @@ -156,7 +156,7 @@ public class coreTest extends OpenCVTestCase { Mat outOfRange = new Mat(2, 2, CvType.CV_64F); outOfRange.put(0, 0, Double.NaN, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 0); - + assertTrue(Core.checkRange(grayRnd_32f)); assertTrue(Core.checkRange(new Mat())); assertFalse(Core.checkRange(outOfRange)); @@ -168,7 +168,7 @@ public class coreTest extends OpenCVTestCase { Double.POSITIVE_INFINITY, 0); assertFalse(Core.checkRange(outOfRange, true)); - + try { Core.checkRange(outOfRange, false); fail("Core.checkRange should throw the CvException"); @@ -1086,8 +1086,39 @@ public class coreTest extends OpenCVTestCase { } public void testPerspectiveTransform() { - // nice example - fail("Not yet implemented"); + Mat src = new Mat(matSize, matSize, CvType.CV_32FC2); + + Mat low = new Mat(1, 1, CvType.CV_32F, new Scalar(0)); + Mat high = new Mat(1, 1, CvType.CV_32F, new Scalar(256)); + Core.randu(src, low, high); + + //FIXME: use Mat.diag + Mat transformMatrix = new Mat(3, 3, CvType.CV_32F); + transformMatrix.put(0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1); + + Core.perspectiveTransform(src, dst, transformMatrix); + + assertMatEqual(src, dst, EPS); + } + + public void testPerspectiveTransform3D() { + Mat src = new Mat(matSize, matSize, CvType.CV_32FC3); + + Mat low = new Mat(1, 1, CvType.CV_32F, new Scalar(0)); + Mat high = new Mat(1, 1, CvType.CV_32F, new Scalar(256)); + Core.randu(src, low, high); + + //FIXME: use Mat.diag + Mat transformMatrix = new Mat(4, 4, CvType.CV_32F); + transformMatrix.put(0, 0, + 1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, 1, 0, + 0, 0, 0, 1); + + Core.perspectiveTransform(src, dst, transformMatrix); + + assertMatEqual(src, dst, EPS); } public void testPhaseMatMatMat() { diff --git a/modules/java/src/java/Converters.java b/modules/java/src/java/Converters.java index 53c677a406..177fb99a57 100644 --- a/modules/java/src/java/Converters.java +++ b/modules/java/src/java/Converters.java @@ -9,209 +9,227 @@ import org.opencv.core.Rect; import org.opencv.features2d.KeyPoint; public class Converters { - - public static Mat vector_Point_to_Mat(List pts) { - Mat res; - int count = (pts!=null) ? pts.size() : 0; - if(count>0){ - res = new Mat(1, count, CvType.CV_32SC2); - int[] buff = new int[count*2]; - for(int i=0; i pts) { - if(pts == null) - throw new java.lang.IllegalArgumentException(); - int cols = m.cols(); - if(CvType.CV_32SC2 != m.type() || m.rows()!=1 ) - throw new java.lang.IllegalArgumentException(); - - pts.clear(); - int[] buff = new int[2*cols]; - m.get(0, 0, buff); - for(int i=0; i mats) { - Mat res; - int count = (mats!=null) ? mats.size() : 0; - if(count>0){ - res = new Mat(1, count, CvType.CV_32SC2); - int[] buff = new int[count*2]; - for(int i=0; i> 32); - buff[i*2+1] = (int)(addr & 0xffffffff); - } - res.put(0, 0, buff); - } else { - res = new Mat(); - } - return res; - } - - public static void Mat_to_vector_Mat(Mat m, List mats) { - if(mats == null) - throw new java.lang.IllegalArgumentException(); - int cols = m.cols(); - if(CvType.CV_32SC2 != m.type() || m.rows()!=1 ) - throw new java.lang.IllegalArgumentException(); - - mats.clear(); - int[] buff = new int[cols*2]; - m.get(0, 0, buff); - for(int i=0; i kps) { - // TODO Auto-generated method stub - } - - public static Mat vector_float_to_Mat(List fs) { - Mat res; - int count = (fs!=null) ? fs.size() : 0; - if(count>0){ - res = new Mat(1, count, CvType.CV_32FC1); //Point can be saved into double[2] - float[] buff = new float[count]; - for(int i=0; i fs) { - if(fs == null) - throw new java.lang.IllegalArgumentException(); - int cols = m.cols(); - if(CvType.CV_32FC1 != m.type() || m.rows()!=1 ) - throw new java.lang.IllegalArgumentException(); - - fs.clear(); - float[] buff = new float[cols]; - m.get(0, 0, buff); - for(int i=0; i bs) { - Mat res; - int count = (bs!=null) ? bs.size() : 0; - if(count>0){ - res = new Mat(1, count, CvType.CV_8UC1); //Point can be saved into double[2] - byte[] buff = new byte[count]; - for(int i=0; i is) { - Mat res; - int count = (is!=null) ? is.size() : 0; - if(count>0){ - res = new Mat(1, count, CvType.CV_32SC1); //Point can be saved into double[2] - int[] buff = new int[count]; - for(int i=0; i is) { - if(is == null) - throw new java.lang.IllegalArgumentException(); - int cols = m.cols(); - if(CvType.CV_32SC1 != m.type() || m.rows()!=1 ) - throw new java.lang.IllegalArgumentException(); - - is.clear(); - int[] buff = new int[cols]; - m.get(0, 0, buff); - for(int i=0; i rs) { - Mat res; - int count = (rs!=null) ? rs.size() : 0; - if(count>0){ - res = new Mat(1, count, CvType.CV_32SC4); //Point can be saved into double[2] - int[] buff = new int[4*count]; - for(int i=0; i rs) { - if(rs == null) - throw new java.lang.IllegalArgumentException(); - int cols = m.cols(); - if(CvType.CV_32SC4 != m.type() || m.rows()!=1 ) - throw new java.lang.IllegalArgumentException(); - - rs.clear(); - int[] buff = new int[4*cols]; - m.get(0, 0, buff); - for(int i=0; i ds) { - Mat res; - int count = (ds!=null) ? ds.size() : 0; - if(count>0){ - res = new Mat(1, count, CvType.CV_64FC1); //Point can be saved into double[2] - double[] buff = new double[count]; - for(int i=0; i pts) { + Mat res; + int count = (pts!=null) ? pts.size() : 0; + if(count>0){ + res = new Mat(1, count, CvType.CV_32SC2); + int[] buff = new int[count*2]; + for(int i=0; i pts) { + Mat res; + int count = (pts!=null) ? pts.size() : 0; + if(count>0){ + res = new Mat(1, count, CvType.CV_32FC2); + float[] buff = new float[count*2]; + for(int i=0; i pts) { + if(pts == null) + throw new java.lang.IllegalArgumentException(); + int cols = m.cols(); + if(CvType.CV_32SC2 != m.type() || m.rows()!=1 ) + throw new java.lang.IllegalArgumentException(); + + pts.clear(); + int[] buff = new int[2*cols]; + m.get(0, 0, buff); + for(int i=0; i mats) { + Mat res; + int count = (mats!=null) ? mats.size() : 0; + if(count>0){ + res = new Mat(1, count, CvType.CV_32SC2); + int[] buff = new int[count*2]; + for(int i=0; i> 32); + buff[i*2+1] = (int)(addr & 0xffffffff); + } + res.put(0, 0, buff); + } else { + res = new Mat(); + } + return res; + } + + public static void Mat_to_vector_Mat(Mat m, List mats) { + if(mats == null) + throw new java.lang.IllegalArgumentException(); + int cols = m.cols(); + if(CvType.CV_32SC2 != m.type() || m.rows()!=1 ) + throw new java.lang.IllegalArgumentException(); + + mats.clear(); + int[] buff = new int[cols*2]; + m.get(0, 0, buff); + for(int i=0; i kps) { + // TODO Auto-generated method stub + } + + public static Mat vector_float_to_Mat(List fs) { + Mat res; + int count = (fs!=null) ? fs.size() : 0; + if(count>0){ + res = new Mat(1, count, CvType.CV_32FC1); //Point can be saved into double[2] + float[] buff = new float[count]; + for(int i=0; i fs) { + if(fs == null) + throw new java.lang.IllegalArgumentException(); + int cols = m.cols(); + if(CvType.CV_32FC1 != m.type() || m.rows()!=1 ) + throw new java.lang.IllegalArgumentException(); + + fs.clear(); + float[] buff = new float[cols]; + m.get(0, 0, buff); + for(int i=0; i bs) { + Mat res; + int count = (bs!=null) ? bs.size() : 0; + if(count>0){ + res = new Mat(1, count, CvType.CV_8UC1); //Point can be saved into double[2] + byte[] buff = new byte[count]; + for(int i=0; i is) { + Mat res; + int count = (is!=null) ? is.size() : 0; + if(count>0){ + res = new Mat(1, count, CvType.CV_32SC1); //Point can be saved into double[2] + int[] buff = new int[count]; + for(int i=0; i is) { + if(is == null) + throw new java.lang.IllegalArgumentException(); + int cols = m.cols(); + if(CvType.CV_32SC1 != m.type() || m.rows()!=1 ) + throw new java.lang.IllegalArgumentException(); + + is.clear(); + int[] buff = new int[cols]; + m.get(0, 0, buff); + for(int i=0; i rs) { + Mat res; + int count = (rs!=null) ? rs.size() : 0; + if(count>0){ + res = new Mat(1, count, CvType.CV_32SC4); //Point can be saved into double[2] + int[] buff = new int[4*count]; + for(int i=0; i rs) { + if(rs == null) + throw new java.lang.IllegalArgumentException(); + int cols = m.cols(); + if(CvType.CV_32SC4 != m.type() || m.rows()!=1 ) + throw new java.lang.IllegalArgumentException(); + + rs.clear(); + int[] buff = new int[4*cols]; + m.get(0, 0, buff); + for(int i=0; i ds) { + Mat res; + int count = (ds!=null) ? ds.size() : 0; + if(count>0){ + res = new Mat(1, count, CvType.CV_64FC1); //Point can be saved into double[2] + double[] buff = new double[count]; + for(int i=0; i