From 51338ec7c3fa2dc8bed9aae78886757f27e58f86 Mon Sep 17 00:00:00 2001 From: Andrey Pavlenko Date: Wed, 4 Apr 2012 14:26:43 +0000 Subject: [PATCH] Java API: fixing Core tests; minor enchantments in CvVector-s API --- .../src/org/opencv/test/OpenCVTestCase.java | 28 +++++++ .../src/org/opencv/test/core/CoreTest.java | 81 ++++++++++--------- modules/java/src/java/core+CvVectorByte.java | 4 + .../java/src/java/core+CvVectorDMatch.java | 4 +- .../java/src/java/core+CvVectorDouble.java | 4 + modules/java/src/java/core+CvVectorFloat.java | 4 + modules/java/src/java/core+CvVectorInt.java | 4 + .../java/src/java/core+CvVectorKeyPoint.java | 4 +- modules/java/src/java/core+CvVectorPoint.java | 4 +- .../java/src/java/core+CvVectorPoint2f.java | 4 +- .../java/src/java/core+CvVectorPoint3.java | 4 +- .../java/src/java/core+CvVectorPoint3f.java | 4 +- modules/java/src/java/core+CvVectorRect.java | 4 +- 13 files changed, 101 insertions(+), 52 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 fc009152b9..3a8b2683c4 100644 --- a/modules/java/android_test/src/org/opencv/test/OpenCVTestCase.java +++ b/modules/java/android_test/src/org/opencv/test/OpenCVTestCase.java @@ -205,6 +205,26 @@ public class OpenCVTestCase extends TestCase { assertTrue(Math.abs(list1.get(i).doubleValue() - list2.get(i).doubleValue()) <= epsilon); } + public static void assertArrayEquals(E[] ar1, E[] ar2, double epsilon) { + if (ar1.length != ar2.length) { + fail("Arrays have different sizes."); + } + + for (int i = 0; i < ar1.length; i++) + assertEquals(ar1[i].doubleValue(), ar2[i].doubleValue(), epsilon); + //assertTrue(Math.abs(ar1[i].doubleValue() - ar2[i].doubleValue()) <= epsilon); + } + + public static void assertArrayEquals(double[] ar1, double[] ar2, double epsilon) { + if (ar1.length != ar2.length) { + fail("Arrays have different sizes."); + } + + for (int i = 0; i < ar1.length; i++) + assertEquals(ar1[i], ar2[i], epsilon); + //assertTrue(Math.abs(ar1[i].doubleValue() - ar2[i].doubleValue()) <= epsilon); + } + public static void assertListMatEquals(List list1, List list2, double epsilon) { if (list1.size() != list2.size()) { throw new UnsupportedOperationException(); @@ -223,6 +243,14 @@ public class OpenCVTestCase extends TestCase { assertPointEquals(list1.get(i), list2.get(i), epsilon); } + public static void assertArrayPointsEquals(Point[] vp1, Point[] vp2, double epsilon) { + if (vp1.length != vp2.length) { + fail("Arrays have different sizes."); + } + + for (int i = 0; i < vp1.length; i++) + assertPointEquals(vp1[i], vp2[i], epsilon); + } public static void assertListPoint3Equals(List list1, List list2, double epsilon) { if (list1.size() != list2.size()) { throw new UnsupportedOperationException(); 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 bbcb97adf3..39ba4e50d6 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 @@ -4,6 +4,9 @@ import org.opencv.core.Core; import org.opencv.core.Core.MinMaxLocResult; import org.opencv.core.CvException; import org.opencv.core.CvType; +import org.opencv.core.CvVectorDouble; +import org.opencv.core.CvVectorInt; +import org.opencv.core.CvVectorPoint; import org.opencv.core.Mat; import org.opencv.core.Point; import org.opencv.core.Rect; @@ -482,11 +485,11 @@ public class CoreTest extends OpenCVTestCase { int arcStart = 30; int arcEnd = 60; int delta = 2; - List pts = new ArrayList(); + CvVectorPoint pts = new CvVectorPoint(); Core.ellipse2Poly(center, axes, angle, arcStart, arcEnd, delta, pts); - List truth = Arrays.asList( + Point truth[] = { new Point(5, 6), new Point(5, 6), new Point(5, 6), @@ -502,8 +505,9 @@ public class CoreTest extends OpenCVTestCase { new Point(4, 6), new Point(4, 6), new Point(4, 6), - new Point(4, 6)); - assertListPointEquals(truth, pts, EPS); + new Point(4, 6) + }; + assertArrayPointsEquals(truth, pts.toArray(new Point[0]), EPS); } public void testEllipseMatPointSizeDoubleDoubleDoubleScalar() { @@ -617,7 +621,7 @@ public class CoreTest extends OpenCVTestCase { } public void testFillConvexPolyMatListOfPointScalar() { - List polyline = Arrays.asList(new Point(1, 1), new Point(5, 0), new Point(6, 8), new Point(0, 9)); + CvVectorPoint polyline = new CvVectorPoint(new Point[]{new Point(1, 1), new Point(5, 0), new Point(6, 8), new Point(0, 9)}); Core.fillConvexPoly(gray0, polyline, new Scalar(150)); @@ -626,8 +630,8 @@ public class CoreTest extends OpenCVTestCase { } public void testFillConvexPolyMatListOfPointScalarIntInt() { - List polyline1 = Arrays.asList(new Point(2, 1), new Point(5, 1), new Point(5, 7), new Point(2, 7)); - List polyline2 = Arrays.asList(new Point(4, 2), new Point(10, 2), new Point(10, 14), new Point(4, 14)); + CvVectorPoint polyline1 = new CvVectorPoint(new Point(2, 1), new Point(5, 1), new Point(5, 7), new Point(2, 7)); + CvVectorPoint polyline2 = new CvVectorPoint(new Point(4, 2), new Point(10, 2), new Point(10, 14), new Point(4, 14)); // current implementation of fixed-point version of fillConvexPoly // requires image to be at least 2-pixel wider in each direction than @@ -645,8 +649,8 @@ public class CoreTest extends OpenCVTestCase { public void testFillPolyMatListOfListOfPointScalar() { int matSize = 10; Mat gray0 = Mat.zeros(matSize, matSize, CvType.CV_8U); - List polyline = Arrays.asList(new Point(1, 4), new Point(1, 8), new Point(4, 1), new Point(7, 8), new Point(7, 4)); - List> polylines = new ArrayList>(); + CvVectorPoint polyline = new CvVectorPoint(new Point(1, 4), new Point(1, 8), new Point(4, 1), new Point(7, 8), new Point(7, 4)); + List polylines = new ArrayList(); polylines.add(polyline); Core.fillPoly(gray0, polylines, new Scalar(1)); @@ -671,13 +675,13 @@ public class CoreTest extends OpenCVTestCase { } public void testFillPolyMatListOfListOfPointScalarIntIntPoint() { - List polyline1 = Arrays.asList(new Point(1, 4), new Point(1, 8), new Point(4, 1), new Point(7, 8), new Point(7, 4)); - List polyline2 = Arrays.asList(new Point(0, 3), new Point(0, 7), new Point(3, 0), new Point(6, 7), new Point(6, 3)); + CvVectorPoint polyline1 = new CvVectorPoint(new Point(1, 4), new Point(1, 8), new Point(4, 1), new Point(7, 8), new Point(7, 4)); + CvVectorPoint polyline2 = new CvVectorPoint(new Point(0, 3), new Point(0, 7), new Point(3, 0), new Point(6, 7), new Point(6, 3)); - List> polylines1 = new ArrayList>(); + List polylines1 = new ArrayList(); polylines1.add(polyline1); - List> polylines2 = new ArrayList>(); + List polylines2 = new ArrayList(); polylines2.add(polyline2); Core.fillPoly(gray0, polylines1, new Scalar(1), Core.LINE_8, 0, new Point(0, 0)); @@ -1190,18 +1194,18 @@ public class CoreTest extends OpenCVTestCase { } public void testMeanStdDevMatMatMat() { - List mean = new ArrayList(); - List stddev = new ArrayList(); + CvVectorDouble mean = new CvVectorDouble(); + CvVectorDouble stddev = new CvVectorDouble(); Core.meanStdDev(rgbLena, mean, stddev); - List expectedMean = Arrays.asList( new Double[] - {105.3989906311035, 99.56269836425781, 179.7303047180176} ); - List expectedDev = Arrays.asList( new Double[] - {33.74205485167219, 52.8734582803278, 49.01569488056406} ); + double expectedMean[] = new double[] + {105.3989906311035, 99.56269836425781, 179.7303047180176}; + double expectedDev[] = new double[] + {33.74205485167219, 52.8734582803278, 49.01569488056406}; - assertListEquals(expectedMean, mean, EPS); - assertListEquals(expectedDev, stddev, EPS); + assertArrayEquals(expectedMean, mean.toArray(null), EPS); + assertArrayEquals(expectedDev, stddev.toArray(null), EPS); } public void testMeanStdDevMatMatMatMat() { @@ -1210,16 +1214,16 @@ public class CoreTest extends OpenCVTestCase { Mat mask = gray0.clone(); submat = mask.submat(0, mask.rows() / 2, 0, mask.cols() / 2); submat.setTo(new Scalar(1)); - List mean = new ArrayList(); - List stddev = new ArrayList(); + CvVectorDouble mean = new CvVectorDouble(); + CvVectorDouble stddev = new CvVectorDouble(); Core.meanStdDev(grayRnd, mean, stddev, mask); - List expectedMean = Arrays.asList( new Double[] {33d} ); - List expectedDev = Arrays.asList( new Double[] {0d} ); + double expectedMean[] = new double[] {33d}; + double expectedDev[] = new double[] {0d}; - assertListEquals(expectedMean, mean, EPS); - assertListEquals(expectedDev, stddev, EPS); + assertArrayEquals(expectedMean, mean.toArray(null), EPS); + assertArrayEquals(expectedDev, stddev.toArray(null), EPS); } public void testMerge() { @@ -1280,14 +1284,15 @@ public class CoreTest extends OpenCVTestCase { rgba0.setTo(new Scalar(10, 20, 30, 40)); List src = Arrays.asList(rgba0); List dst = Arrays.asList(gray3, gray2, gray1, gray0, getMat(CvType.CV_8UC3, 0, 0, 0)); - List fromTo = Arrays.asList( - 3, 0, + CvVectorInt fromTo = new CvVectorInt(1, new int[] + { 3, 0, 3, 1, 2, 2, 0, 3, 2, 4, 1, 5, - 0, 6); + 0, 6 } + ); Core.mixChannels(src, dst, fromTo); @@ -1740,8 +1745,8 @@ public class CoreTest extends OpenCVTestCase { public void testPolylinesMatListOfListOfPointBooleanScalar() { Mat img = gray0; - List> polyline = new ArrayList>(); - polyline.add(Arrays.asList(new Point(1, 1), new Point(7, 1), new Point(7, 6), new Point(1, 6))); + List polyline = new ArrayList(); + polyline.add(new CvVectorPoint(new Point(1, 1), new Point(7, 1), new Point(7, 6), new Point(1, 6))); Core.polylines(img, polyline, true, new Scalar(100)); @@ -1754,8 +1759,8 @@ public class CoreTest extends OpenCVTestCase { public void testPolylinesMatListOfListOfPointBooleanScalarInt() { Mat img = gray0; - List> polyline = new ArrayList>(); - polyline.add(Arrays.asList(new Point(1, 1), new Point(7, 1), new Point(7, 6), new Point(1, 6))); + List polyline = new ArrayList(); + polyline.add(new CvVectorPoint(new Point(1, 1), new Point(7, 1), new Point(7, 6), new Point(1, 6))); Core.polylines(img, polyline, true, new Scalar(100), 2); @@ -1764,10 +1769,10 @@ public class CoreTest extends OpenCVTestCase { public void testPolylinesMatListOfListOfPointBooleanScalarIntIntInt() { Mat img = gray0; - List> polyline1 = new ArrayList>(); - polyline1.add(Arrays.asList(new Point(1, 1), new Point(7, 1), new Point(7, 6), new Point(1, 6))); - List> polyline2 = new ArrayList>(); - polyline2.add(Arrays.asList(new Point(2, 2), new Point(14, 2), new Point(14, 12), new Point(2, 12))); + List polyline1 = new ArrayList(); + polyline1.add(new CvVectorPoint(new Point(1, 1), new Point(7, 1), new Point(7, 6), new Point(1, 6))); + List polyline2 = new ArrayList(); + polyline2.add(new CvVectorPoint(new Point(2, 2), new Point(14, 2), new Point(14, 12), new Point(2, 12))); Core.polylines(img, polyline1, true, new Scalar(100), 2, Core.LINE_8, 0); diff --git a/modules/java/src/java/core+CvVectorByte.java b/modules/java/src/java/core+CvVectorByte.java index 656eb9fe2e..fdcb7ecb1d 100644 --- a/modules/java/src/java/core+CvVectorByte.java +++ b/modules/java/src/java/core+CvVectorByte.java @@ -7,6 +7,10 @@ public class CvVectorByte extends CvVector { super(_d, ch); } + public CvVectorByte() { + super(_d, 1); + } + public CvVectorByte(int ch, long addr) { super(_d, ch, addr); } diff --git a/modules/java/src/java/core+CvVectorDMatch.java b/modules/java/src/java/core+CvVectorDMatch.java index 5e4743659b..338782c2da 100644 --- a/modules/java/src/java/core+CvVectorDMatch.java +++ b/modules/java/src/java/core+CvVectorDMatch.java @@ -17,9 +17,9 @@ public class CvVectorDMatch extends CvVectorFloat { super(_ch, m); } - public CvVectorDMatch(DMatch[] a) { + public CvVectorDMatch(DMatch...a) { super(_ch); - if(a==null) + if(a==null || a.length==0) return; int cnt = a.length; create(cnt); diff --git a/modules/java/src/java/core+CvVectorDouble.java b/modules/java/src/java/core+CvVectorDouble.java index 6721359b14..a1a20c1bb2 100644 --- a/modules/java/src/java/core+CvVectorDouble.java +++ b/modules/java/src/java/core+CvVectorDouble.java @@ -7,6 +7,10 @@ public class CvVectorDouble extends CvVector { super(_d, ch); } + public CvVectorDouble() { + super(_d, 1); + } + public CvVectorDouble(int ch, long addr) { super(_d, ch, addr); } diff --git a/modules/java/src/java/core+CvVectorFloat.java b/modules/java/src/java/core+CvVectorFloat.java index a0a509113c..e8e5b88c7a 100644 --- a/modules/java/src/java/core+CvVectorFloat.java +++ b/modules/java/src/java/core+CvVectorFloat.java @@ -7,6 +7,10 @@ public class CvVectorFloat extends CvVector { super(_d, ch); } + public CvVectorFloat() { + super(_d, 1); + } + public CvVectorFloat(int ch, long addr) { super(_d, ch, addr); } diff --git a/modules/java/src/java/core+CvVectorInt.java b/modules/java/src/java/core+CvVectorInt.java index 5993ae2fe2..c9ae89a9c7 100644 --- a/modules/java/src/java/core+CvVectorInt.java +++ b/modules/java/src/java/core+CvVectorInt.java @@ -8,6 +8,10 @@ public class CvVectorInt extends CvVector { super(_d, ch); } + public CvVectorInt() { + super(_d, 1); + } + public CvVectorInt(int ch, long addr) { super(_d, ch, addr); } diff --git a/modules/java/src/java/core+CvVectorKeyPoint.java b/modules/java/src/java/core+CvVectorKeyPoint.java index 36b87deb66..657301adfa 100644 --- a/modules/java/src/java/core+CvVectorKeyPoint.java +++ b/modules/java/src/java/core+CvVectorKeyPoint.java @@ -17,9 +17,9 @@ public class CvVectorKeyPoint extends CvVectorFloat { super(_ch, m); } - public CvVectorKeyPoint(KeyPoint[] a) { + public CvVectorKeyPoint(KeyPoint...a) { super(_ch); - if(a==null) + if(a==null || a.length==0) return; int cnt = a.length; create(cnt); diff --git a/modules/java/src/java/core+CvVectorPoint.java b/modules/java/src/java/core+CvVectorPoint.java index b8c9e26f4c..cde14576a6 100644 --- a/modules/java/src/java/core+CvVectorPoint.java +++ b/modules/java/src/java/core+CvVectorPoint.java @@ -15,9 +15,9 @@ public class CvVectorPoint extends CvVectorInt { super(_ch, m); } - public CvVectorPoint(Point[] a) { + public CvVectorPoint(Point...a) { super(_ch); - if(a==null) + if(a==null || a.length==0) return; int cnt = a.length; create(cnt); diff --git a/modules/java/src/java/core+CvVectorPoint2f.java b/modules/java/src/java/core+CvVectorPoint2f.java index 1b1ad1167c..73f96da1b1 100644 --- a/modules/java/src/java/core+CvVectorPoint2f.java +++ b/modules/java/src/java/core+CvVectorPoint2f.java @@ -15,9 +15,9 @@ public class CvVectorPoint2f extends CvVectorFloat { super(_ch, m); } - public CvVectorPoint2f(Point[] a) { + public CvVectorPoint2f(Point...a) { super(_ch); - if(a==null) + if(a==null || a.length==0) return; int cnt = a.length; create(cnt); diff --git a/modules/java/src/java/core+CvVectorPoint3.java b/modules/java/src/java/core+CvVectorPoint3.java index 64cd4627b9..827debb22f 100644 --- a/modules/java/src/java/core+CvVectorPoint3.java +++ b/modules/java/src/java/core+CvVectorPoint3.java @@ -15,9 +15,9 @@ public class CvVectorPoint3 extends CvVectorInt { super(_ch, m); } - public CvVectorPoint3(Point3[] a) { + public CvVectorPoint3(Point3...a) { super(_ch); - if(a==null) + if(a==null || a.length==0) return; int cnt = a.length; create(cnt); diff --git a/modules/java/src/java/core+CvVectorPoint3f.java b/modules/java/src/java/core+CvVectorPoint3f.java index c3132b69d7..d26854c3b0 100644 --- a/modules/java/src/java/core+CvVectorPoint3f.java +++ b/modules/java/src/java/core+CvVectorPoint3f.java @@ -15,9 +15,9 @@ public class CvVectorPoint3f extends CvVectorFloat { super(_ch, m); } - public CvVectorPoint3f(Point3[] a) { + public CvVectorPoint3f(Point3...a) { super(_ch); - if(a==null) + if(a==null || a.length==0) return; int cnt = a.length; create(cnt); diff --git a/modules/java/src/java/core+CvVectorRect.java b/modules/java/src/java/core+CvVectorRect.java index e6e36889eb..32e9b4c408 100644 --- a/modules/java/src/java/core+CvVectorRect.java +++ b/modules/java/src/java/core+CvVectorRect.java @@ -16,9 +16,9 @@ public class CvVectorRect extends CvVectorInt { super(_ch, m); } - public CvVectorRect(Rect[] a) { + public CvVectorRect(Rect...a) { super(_ch); - if(a==null) + if(a==null || a.length==0) return; int cnt = a.length; create(cnt);